Bladeren bron

refactored war

Jonathan Hinds 6 jaren geleden
bovenliggende
commit
ec82737a85

+ 0
- 16
src/main/java/io/zipcoder/casino/CardGame.java Bestand weergeven

@@ -32,14 +32,6 @@ public abstract class CardGame {
32 32
 
33 33
     }
34 34
 
35
-    public void faceDown(Card card){
36
-        card.setVisibility(false);
37
-    }
38
-
39
-    public void faceUp(Card card){
40
-        card.setVisibility(true);
41
-    }
42
-
43 35
     public Deck getDeck() {
44 36
         return deck;
45 37
     }
@@ -55,10 +47,6 @@ public abstract class CardGame {
55 47
         }
56 48
     }
57 49
 
58
-    public void setDeck(Deck deck) {
59
-        this.deck = deck;
60
-    }
61
-
62 50
     public int getAnte(){
63 51
         return ante;
64 52
     }
@@ -124,10 +112,6 @@ public abstract class CardGame {
124 112
         }
125 113
     }
126 114
 
127
-    public void printTurn(){
128
-        System.out.println("it is now " + playersTurn.getPlayer().getName() + "'s turn");
129
-    }
130
-
131 115
     public Player getLoser() {
132 116
         return loser;
133 117
     }

+ 21
- 5
src/main/java/io/zipcoder/casino/Printer.java Bestand weergeven

@@ -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 = "";
@@ -44,10 +48,6 @@ public class Printer {
44 48
         printMessage("Thanks for your money chump!");
45 49
     }
46 50
 
47
-    public static void printMessage(String string) {
48
-        System.out.println(string);
49
-    }
50
-
51 51
     public static void pleaseEnterNum(){
52 52
         printMessage("Please enter a number");
53 53
     }
@@ -57,7 +57,23 @@ public class Printer {
57 57
     }
58 58
     
59 59
     public static void wrongCommand(){
60
-        printMessage("Sorry, there is no game with that name.")
60
+        printMessage("Sorry, there is no game with that name.");
61
+    }
62
+
63
+    public static void printWarTurnResult(String name, String cardName, int handSize){
64
+        printMessage(name + " has played " + cardName + " and has " + handSize + " cards left.");
65
+    }
66
+
67
+    public static void playedFaceDown(String name){
68
+        printMessage(name + " has played a card face down.");
69
+    }
70
+
71
+    public static void startSlots(){
72
+        printMessage("You are all set to play a new slot game..zrrr..! \n");
73
+    }
74
+
75
+    public static void payOut(int payout){
76
+        printMessage("Your payout amount for slot machine is: $" + payout + "\n");
61 77
     }
62 78
 
63 79
 

+ 5
- 29
src/main/java/io/zipcoder/casino/SlotMachine.java Bestand weergeven

@@ -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,13 +21,9 @@ 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.payOut(payout);
27 25
     }
28 26
 
29
-
30
-    // implementd from game
31
-
32 27
     @Override
33 28
     public void quit() {
34 29
 
@@ -36,13 +31,13 @@ public class SlotMachine implements Game, Gamble {
36 31
 
37 32
     @Override
38 33
     public void startGame() {
39
-        System.out.println("You are all set to play a new slot game..zrrr..!"+"\n");
34
+        Printer.startSlots();
40 35
         try {
41 36
             Thread.sleep(3000);
42 37
         } catch (InterruptedException e) {
43 38
             e.printStackTrace();
44 39
         }
45
-        System.out.println("Your slot is in progress"+"\n");
40
+        Printer.printMessage("Your slot is in progress"+"\n");
46 41
 
47 42
         try {
48 43
             Thread.sleep(3000);
@@ -111,8 +106,6 @@ public class SlotMachine implements Game, Gamble {
111 106
                 }
112 107
             }
113 108
             outputword= "[ " + word1+ " ]" + "   " + "[ " + word2 + " ]" + "   "+ "[ " + word3 + " ]" + "\n" ;
114
-            //System.out.println(outputword);
115
-
116 109
 
117 110
             if(((!word1.equals(word2)) )&& ((!word1.equals(word3))) && ((!word2.equals(word3)))){
118 111
 
@@ -133,35 +126,18 @@ public class SlotMachine implements Game, Gamble {
133 126
                 payout=betAmount*3;
134 127
             }
135 128
 
136
-            System.out.println(( outputword + "\n" ));
129
+            Printer.printMessage(( outputword + "\n" ));
137 130
 
138 131
         try {
139 132
             Thread.sleep(2000);
140 133
         } catch (InterruptedException e) {
141 134
             e.printStackTrace();
142 135
         }
143
-
144
-
145
-
146
-        }
147
-
148
-
149
-
150
-
151
-
136
+    }
152 137
 
153 138
     @Override
154 139
     public void startRound() {
155 140
 
156 141
     }
157
-
158
-   /* public  void main(String[] args) {
159
-
160
-
161
-        //System.out.println("Enter the amount of money you wan to enter into the slot machine");
162
-        //Scanner scanner = new Scanner(System.in);
163
-
164
-
165
-    }*/
166 142
 }
167 143
 

+ 48
- 87
src/main/java/io/zipcoder/casino/War.java Bestand weergeven

@@ -12,54 +12,49 @@ public class War extends CardGame implements Gamble, Game {
12 12
     private ArrayList<Card> tableCards = new ArrayList<Card>();
13 13
     private ArrayList<CardPlayer> warMembers = new ArrayList<CardPlayer>();
14 14
     private Console console = new Console();
15
-    private boolean war = false;
16
-
17
-    War(int ante) {
18
-        super(ante);
19
-    }
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
-    public void quit() {
141
-
142
-    }
117
+    public void quit() { }
143 118
 
144 119
     public void startGame() {
145 120
         Printer.welcomeTo("War");
@@ -151,51 +126,37 @@ public class War extends CardGame implements Gamble, Game {
151 126
 
152 127
     public void startRound() {
153 128
         while(super.getLoser() == null) {
154
-
155 129
             String input = console.getCMDFromUser("Type 'FLIP' to play the card at the top of your pile");
156 130
             if (input.equals("flip")) {
157
-                //each player
158
-                for (CardPlayer player : super.getPlayers()) {
159
-                    //plays a card, then
160
-                    playCard(true);
161
-                    //the turn updates to be the next players.
162
-                    super.chooseNextTurn();
163
-                }
164
-                //determine the winner once all players play a card
131
+                eachPlayerPlayCard();
165 132
                 CardPlayer winner = determineWinner(super.getPlayers());
166
-                System.out.println(winner.getPlayer().getName() + " has been rewarded " + tableCards.size() + " cards.");
167
-                //add all the table cards to the players discard
133
+                Printer.printMessage(winner.getPlayer().getName() + " has been rewarded " + tableCards.size() + " cards.");
168 134
                 winner.addDiscard(tableCards);
169
-                //clear the table cards pile
170 135
                 tableCards = new ArrayList<Card>();
171
-                //if the user does not type play
172 136
             } else {
173
-                //display a message
174
-                System.out.println("Sorry, I don't understand that command.");
137
+                Printer.printMessage("Sorry, I don't understand that command.");
175 138
             }
176 139
         }
140
+    }
177 141
 
142
+    public void eachPlayerPlayCard(){
143
+        for (CardPlayer player : super.getPlayers()) {
144
+            playCard(true);
145
+            super.chooseNextTurn();
146
+        }
178 147
     }
179 148
 
180 149
     public void deal() {
181
-        //while there are cards in the deck
182 150
         while(super.getDeck().size() != 0){
183
-            //for each player playing the game
184 151
             for(int i = 0; i < super.getPlayers().size(); i ++)
185 152
             {
186
-                //grab the card from the top (last added) to the deck
187 153
                 Card card = super.getDeck().get(super.getDeck().size() - 1);
188
-                //get the player whos hand we are adding the card to
189 154
                 CardPlayer player = super.getPlayers().get(i);
190
-                //add the card to their hand
191 155
                 player.getHand().add(card);
192
-                //remove the card from the deck
193 156
                 super.getDeck().remove(card);
194 157
             }
195 158
         }
196
-
197
-        System.out.println(super.getPlayersTurn().getPlayer().getName() +
198
-                "has: " + super.getPlayersTurn().getHand().size() + " cards.");
159
+        Printer.printMessage(super.getPlayersTurn().getPlayer().getName() + "has: " + super.getPlayersTurn().getHand().size() + " cards.");
199 160
 
200 161
     }
201 162
 }

+ 0
- 6
src/test/java/io/zipcoder/casino/DiceTest.java Bestand weergeven

@@ -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
 }