Lauren Green 6 år sedan
förälder
incheckning
ce3c6d1b31

+ 0
- 1
src/main/java/io/zipcoder/casino/CardGame.java Visa fil

@@ -32,7 +32,6 @@ public abstract class CardGame {
32 32
 
33 33
     }
34 34
 
35
-
36 35
     public Deck getDeck() {
37 36
         return deck;
38 37
     }

+ 0
- 1
src/main/java/io/zipcoder/casino/Game.java Visa fil

@@ -1,7 +1,6 @@
1 1
 package io.zipcoder.casino;
2 2
 
3 3
 public interface Game {
4
-
5 4
      void startGame();
6 5
      void startRound();
7 6
 }

+ 19
- 25
src/main/java/io/zipcoder/casino/Printer.java Visa fil

@@ -4,6 +4,10 @@ import java.util.ArrayList;
4 4
 
5 5
 public class Printer {
6 6
 
7
+    public static void printMessage(String string) {
8
+        System.out.println(string);
9
+    }
10
+
7 11
     public static void noMatchingGameName(ArrayList<String> gameNames){
8 12
 
9 13
         String games = "";
@@ -13,44 +17,34 @@ public class Printer {
13 17
         }
14 18
         games = games.trim();
15 19
 
16
-        System.out.println("Sorry, there is no game with that name, try one of: " + games);
17
-    }
18
-
19
-    public static void getBet(String phrase){
20
-        System.out.println("What is the " + phrase + " at the table you're looking for?");
21
-    }
22
-
23
-    public static void unacceptableMaxBet(int minBet){
24
-        System.out.println("Your bet must be above " + minBet);
25
-    }
26
-
27
-    public static void unacceptableMinBet(){
28
-        System.out.println("Your bet must be above $0");
29
-    }
30
-
31
-    public static void studHandsDealt(){
32
-        System.out.println("Each player Dealt 3 cards");
20
+        printMessage("Sorry, there is no game with that name, try one of: " + games);
33 21
     }
34 22
 
35 23
     public static void showCard(Player player, Card card){
36
-        System.out.println(player.getName() + " shows a " + card.getName());
24
+        printMessage(player.getName() + " shows a " + card.getName());
37 25
     }
38 26
 
39 27
     public static void pickGameMsg(){
40
-        System.out.println("Please choose a game to play!");
28
+        printMessage("Please choose a game to play!");
41 29
     }
42 30
 
43 31
     public static void closeGameMsg(){
44
-        System.out.println("Thanks for your money chump!");
45
-    }
46
-
47
-    public static void printMessage(String string) {
48
-        System.out.println(string);
32
+        printMessage("Thanks for your money chump!");
49 33
     }
50 34
 
51 35
     public static void pleaseEnterNum(){
52
-        System.out.println("Please enter a number");
36
+        printMessage("Please enter a number");
37
+    }
38
+    
39
+    public static void welcomeTo(String gameName){
40
+        printMessage("Welcome to " + gameName + "!");
53 41
     }
54 42
 
43
+    public static void printWarTurnResult(String name, String cardName, int handSize){
44
+        printMessage(name + " has played " + cardName + " and has " + handSize + " cards left.");
45
+    }
55 46
 
47
+    public static void playedFaceDown(String name){
48
+        printMessage(name + " has played a card face down.");
49
+    }
56 50
 }

+ 5
- 30
src/main/java/io/zipcoder/casino/SlotMachine.java Visa fil

@@ -13,7 +13,6 @@ public class SlotMachine implements Game, Gamble {
13 13
         this.betAmount = betAmount;
14 14
     }
15 15
 
16
-    // implemented from gamble
17 16
     @Override
18 17
     public void bet(int betAmount) {
19 18
         this.betAmount= betAmount;
@@ -22,23 +21,18 @@ public class SlotMachine implements Game, Gamble {
22 21
 
23 22
     @Override
24 23
     public void payout(){
25
-        System.out.println("Your payout amount for slot machine is: $" +payout +"\n");
26
-
24
+        Printer.printMessage("Your payout amount for slot machine is: $" + payout + "\n");
27 25
     }
28 26
 
29
-
30
-    // implementd from game
31
-
32
-
33 27
     @Override
34 28
     public void startGame() {
35
-        System.out.println("You are all set to play a new slot game..zrrr..!"+"\n");
29
+        Printer.printMessage("You are all set to play a new slot game..zrrr..! \n");
36 30
         try {
37 31
             Thread.sleep(3000);
38 32
         } catch (InterruptedException e) {
39 33
             e.printStackTrace();
40 34
         }
41
-        System.out.println("Your slot is in progress"+"\n");
35
+        Printer.printMessage("Your slot is in progress"+"\n");
42 36
 
43 37
         try {
44 38
             Thread.sleep(3000);
@@ -107,8 +101,6 @@ public class SlotMachine implements Game, Gamble {
107 101
                 }
108 102
             }
109 103
             outputword= "[ " + word1+ " ]" + "   " + "[ " + word2 + " ]" + "   "+ "[ " + word3 + " ]" + "\n" ;
110
-            //System.out.println(outputword);
111
-
112 104
 
113 105
             if(((!word1.equals(word2)) )&& ((!word1.equals(word3))) && ((!word2.equals(word3)))){
114 106
 
@@ -129,35 +121,18 @@ public class SlotMachine implements Game, Gamble {
129 121
                 payout=betAmount*3;
130 122
             }
131 123
 
132
-            System.out.println(( outputword + "\n" ));
124
+            Printer.printMessage(( outputword + "\n" ));
133 125
 
134 126
         try {
135 127
             Thread.sleep(2000);
136 128
         } catch (InterruptedException e) {
137 129
             e.printStackTrace();
138 130
         }
139
-
140
-
141
-
142
-        }
143
-
144
-
145
-
146
-
147
-
131
+    }
148 132
 
149 133
     @Override
150 134
     public void startRound() {
151 135
 
152 136
     }
153
-
154
-   /* public  void main(String[] args) {
155
-
156
-
157
-        //System.out.println("Enter the amount of money you wan to enter into the slot machine");
158
-        //Scanner scanner = new Scanner(System.in);
159
-
160
-
161
-    }*/
162 137
 }
163 138
 

+ 51
- 99
src/main/java/io/zipcoder/casino/War.java Visa fil

@@ -11,55 +11,50 @@ public class War extends CardGame implements Gamble, Game {
11 11
 
12 12
     private ArrayList<Card> tableCards = new ArrayList<Card>();
13 13
     private ArrayList<CardPlayer> warMembers = new ArrayList<CardPlayer>();
14
-    private Scanner scanner = new Scanner(System.in);
15
-    private boolean war = false;
16
-
17
-    War(int ante) {
18
-        super(ante);
19
-    }
14
+    private Console console = new Console();
20 15
 
16
+    War(int ante) { super(ante); }
21 17
 
22
-    /**
23
-     * Specific to war methods
24
-     */
25 18
     public void playCard(boolean cardFace){
26
-        //if the player has cards in their hand
27 19
         if(super.getPlayersTurn().getHand().size() > 0) {
28
-            //pull out a card to play
29
-            Card card = super.getPlayersTurn().getHand().get(0);
30
-            //play the card face up, on the table
31
-            card.setVisibility(cardFace);
32
-            tableCards.add(card);
33
-            //store the last played card in the players wrapper class
34
-            super.getPlayersTurn().setPlayedCard(card);
35
-            //remove this card from their hand
36
-            super.getPlayersTurn().getHand().remove(card);
37
-            //print the card that was played
38
-            if(cardFace == true) {
39
-                System.out.println(super.getPlayersTurn().getPlayer().getName() + " has played " + card.getName() + " and has " + super.getPlayersTurn().getHand().size() + " cards left.");
40
-            } else {
41
-                System.out.println(super.getPlayersTurn().getPlayer().getName() + " has played a card face down.");
42
-            }
43
-        //if the player has not cards in their hand but has cards in their discard, pickup their discard and play a card
20
+            playCardInHand(cardFace);
44 21
         } else if(super.getPlayersTurn().getHand().size() == 0 && super.getPlayersTurn().getDiscard().size() > 0) {
45
-            System.out.println(super.getPlayersTurn().getPlayer().getName() + " ran out of cards and picked up their discard pile.");
46
-            super.getPlayersTurn().getHand().addAll(super.getPlayersTurn().getDiscard());
47
-            super.getPlayersTurn().setDiscard(new ArrayList<Card>());
48
-            playCard(true);
49
-        //if the person has no cards in their hand, and no cards in discard they lose.
22
+            playCardFromPile(cardFace);
50 23
         } else if(super.getPlayersTurn().getHand().size() == 0 && super.getPlayersTurn().getDiscard().size() == 0){
51 24
             super.setLoser(super.getPlayersTurn().getPlayer());
52
-            System.out.println(super.getPlayersTurn().getPlayer().getName() + " has lost the match!");
25
+            Printer.printMessage(super.getPlayersTurn().getPlayer().getName() + " has lost the match!");
53 26
         }
54 27
     }
55 28
 
56
-    public CardPlayer warMethod(){
57
-        System.out.println("War!");
29
+    public void playCardInHand(boolean cardFace){
30
+        Card card = getCardFromHand(cardFace);
31
+        if(cardFace) {
32
+            Printer.printWarTurnResult(super.getPlayersTurn().getPlayer().getName(), card.getName(), super.getPlayersTurn().getHand().size());
33
+        } else {
34
+            Printer.playedFaceDown(super.getPlayersTurn().getPlayer().getName());
35
+        }
36
+    }
58 37
 
38
+    public Card getCardFromHand(boolean cardFace){
39
+        Card card = super.getPlayersTurn().getHand().get(0);
40
+        card.setVisibility(cardFace);
41
+        tableCards.add(card);
42
+        super.getPlayersTurn().setPlayedCard(card);
43
+        super.getPlayersTurn().getHand().remove(card);
44
+        return card;
45
+    }
46
+
47
+    public void playCardFromPile(boolean cardFace){
48
+        Printer.printMessage(super.getPlayersTurn().getPlayer().getName() + " ran out of cards and picked up their discard pile.");
49
+        super.getPlayersTurn().getHand().addAll(super.getPlayersTurn().getDiscard());
50
+        super.getPlayersTurn().setDiscard(new ArrayList<Card>());
51
+        playCard(cardFace);
52
+    }
53
+
54
+    public CardPlayer warMethod(){
55
+        Printer.printMessage("War!");
59 56
         int max = 0;
60 57
         CardPlayer winner = null;
61
-
62
-        //each player plays 3 cards
63 58
         for(int i = 0; i < warMembers.size(); i ++){
64 59
             for(int m = 0; m < 2; m ++){
65 60
                 playCard(false);
@@ -67,36 +62,31 @@ public class War extends CardGame implements Gamble, Game {
67 62
             playCard(true);
68 63
             super.chooseNextTurn();
69 64
         }
70
-
71
-        //find the player with the highest value
72 65
         winner = determineWinner(warMembers);
73 66
         warMembers = new ArrayList<>();
74 67
         return winner;
75 68
     }
76 69
 
77 70
     public CardPlayer determineWinner(ArrayList<CardPlayer> playerList){
78
-
79 71
         int max = 0;
80 72
         CardPlayer winner = null;
81 73
         boolean war = false;
82
-
83
-        //loop through and get the max card value
84 74
         for(int i = 0; i < playerList.size(); i ++){
85 75
             CardPlayer player = playerList.get(i);
86
-            //if the players card is greater than the current max
87 76
             if(player.getPlayedCard().getCardValue() > max)
88 77
             {
89
-                //set their value as max
90 78
                 max = player.getPlayedCard().getCardValue();
91
-                //make them the winner
92 79
                 winner = player;
93
-                //set war to false
94 80
                 war = false;
95 81
             }  else if (player.getPlayedCard().getCardValue() == max){
96 82
                 warMembers.add(player);
97 83
                 war = true;
98 84
             }
99 85
         }
86
+        return checkWar(war, winner);
87
+    }
88
+
89
+    public CardPlayer checkWar(boolean war, CardPlayer winner){
100 90
         if(war)
101 91
         {
102 92
             warMembers.add(winner);
@@ -104,25 +94,16 @@ public class War extends CardGame implements Gamble, Game {
104 94
             return winner;
105 95
         } else if(!war)
106 96
         {
107
-            System.out.println("The winner is " + winner.getPlayer().getName());
97
+            Printer.printMessage("The winner is " + winner.getPlayer().getName());
108 98
             return winner;
109 99
         }
110 100
         return null;
111 101
     }
112 102
 
113
-    /**
114
-     * Below 3 Implemented from Gamble
115
-     */
116
-    public void bet(int betAmount) {
117
-        super.changeTablePot(betAmount);
118
-    }
119
-
103
+    public void bet(int betAmount) { super.changeTablePot(betAmount); }
120 104
 
121 105
     public void payout() {
122
-        if(super.getWinner() != null)
123
-        {
124
-            super.getWinner().changeBalance(super.getTablePot());
125
-        }
106
+        if(super.getWinner() != null) { super.getWinner().changeBalance(super.getTablePot()); }
126 107
     }
127 108
 
128 109
     public void payAnte() {
@@ -133,13 +114,7 @@ public class War extends CardGame implements Gamble, Game {
133 114
         }
134 115
     }
135 116
 
136
-    /**
137
-     * Below 3 Implemented from Game
138
-     */
139
-
140
-
141
-    public void startGame() {
142
-        System.out.println("Welcome to war!");
117
+        Printer.welcomeTo("War");
143 118
         super.chooseStatingPlayer();
144 119
         payAnte();
145 120
         deal();
@@ -148,59 +123,36 @@ public class War extends CardGame implements Gamble, Game {
148 123
 
149 124
     public void startRound() {
150 125
         while(super.getLoser() == null) {
151
-
152
-            String input = getCommand();
153
-
154
-            if (input.equals("play")) {
155
-                //each player
156
-                for (CardPlayer player : super.getPlayers()) {
157
-                    //plays a card, then
158
-                    playCard(true);
159
-                    //the turn updates to be the next players.
160
-                    super.chooseNextTurn();
161
-                }
162
-                //determine the winner once all players play a card
126
+            String input = console.getCMDFromUser("Type 'FLIP' to play the card at the top of your pile");
127
+            if (input.equals("flip")) {
128
+                eachPlayerPlayCard();
163 129
                 CardPlayer winner = determineWinner(super.getPlayers());
164
-                System.out.println(winner.getPlayer().getName() + " has been rewarded " + tableCards.size() + " cards.");
165
-                //add all the table cards to the players discard
130
+                Printer.printMessage(winner.getPlayer().getName() + " has been rewarded " + tableCards.size() + " cards.");
166 131
                 winner.addDiscard(tableCards);
167
-                //clear the table cards pile
168 132
                 tableCards = new ArrayList<Card>();
169
-                //if the user does not type play
170 133
             } else {
171
-                //display a message
172
-                System.out.println("Sorry, I don't understand that command.");
134
+                Printer.printMessage("Sorry, I don't understand that command.");
173 135
             }
174 136
         }
175
-
176 137
     }
177 138
 
178
-    public String getCommand(){
179
-        System.out.println("Type play to play the top card from your pile.");
180
-        String input = scanner.next();
181
-        input = input.toLowerCase().trim();
182
-        return input;
139
+    public void eachPlayerPlayCard(){
140
+        for (CardPlayer player : super.getPlayers()) {
141
+            playCard(true);
142
+            super.chooseNextTurn();
143
+        }
183 144
     }
184 145
 
185 146
     public void deal() {
186
-        //while there are cards in the deck
187 147
         while(super.getDeck().size() != 0){
188
-            //for each player playing the game
189 148
             for(int i = 0; i < super.getPlayers().size(); i ++)
190 149
             {
191
-                //grab the card from the top (last added) to the deck
192 150
                 Card card = super.getDeck().get(super.getDeck().size() - 1);
193
-                //get the player whos hand we are adding the card to
194 151
                 CardPlayer player = super.getPlayers().get(i);
195
-                //add the card to their hand
196 152
                 player.getHand().add(card);
197
-                //remove the card from the deck
198 153
                 super.getDeck().remove(card);
199 154
             }
200 155
         }
201
-
202
-        System.out.println(super.getPlayersTurn().getPlayer().getName() +
203
-                "has: " + super.getPlayersTurn().getHand().size() + " cards.");
204
-
156
+        Printer.printMessage(super.getPlayersTurn().getPlayer().getName() + "has: " + super.getPlayersTurn().getHand().size() + " cards.");
205 157
     }
206
-}
158
+}

+ 0
- 6
src/test/java/io/zipcoder/casino/DiceTest.java Visa fil

@@ -24,10 +24,4 @@ public class DiceTest {
24 24
 
25 25
         Assert.assertEquals(expected,x);
26 26
     }
27
-
28
-    @Test
29
-    public void test()
30
-    {
31
-
32
-    }
33 27
 }