Преглед изворни кода

New working stud with betting removed, working test methods

Nick Satinover пре 6 година
родитељ
комит
4059d819d3

+ 1
- 1
src/main/java/io/zipcoder/casino/Printer.java Прегледај датотеку

@@ -57,7 +57,7 @@ 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 61
     }
62 62
 
63 63
 

+ 114
- 150
src/main/java/io/zipcoder/casino/Stud.java Прегледај датотеку

@@ -1,45 +1,39 @@
1
-
2 1
 package io.zipcoder.casino;
3
-import java.util.Scanner;
4 2
 
5
-public class Stud extends CardGame implements Gamble, Game {
6
-    Scanner scanner = new Scanner(System.in);
7
-    Console console;
8
-    // private int roundCount = 0;
3
+import java.util.ArrayList;
4
+
5
+public class Stud extends CardGame implements Game {
6
+    Console console = new Console();
9 7
 
10 8
     public Stud(int ante) {
11 9
         super(ante);
12 10
     }
13
-    
11
+
14 12
     public void playCard(Player player, Card card) {
15 13
         card.setVisibility(true);               //CARD isVISIBLE
16 14
         Printer.showCard(player, card);         //PRINT card name to CONSOLE
17 15
     }
18 16
 
19
-
20
-    public void fold(){
21
-
22
-    }
23
-
24 17
     /**
25 18
      * Determine what player wins by looping through player array and then
26 19
      * passing each hand to the 'handValue' method
27 20
      */
28 21
     public CardPlayer determineWinner(){
29
-    int max = 0;
30
-    CardPlayer winner = null;
31
-
32
-    for(int i = 0; i < getPlayers().size(); i ++){
33
-        CardPlayer player = getPlayers().get(i);
34
-        int playerHandValue = handValue(player); // 'handValue' method sets 'max' value of this hand
35
-        if(playerHandValue > max){
36
-            max = playerHandValue;
37
-            winner = player;
22
+        int max = 0;
23
+        CardPlayer winner = null;
24
+
25
+        for(int i = 0; i < getPlayers().size(); i ++){
26
+            CardPlayer player = getPlayers().get(i);
27
+            int playerHandValue = handValue(player); // 'handValue' method sets 'max' value of this hand
28
+            if(playerHandValue > max){
29
+                max = playerHandValue;
30
+                winner = player;
31
+            }
38 32
         }
39
-    }
40
-    System.out.println("The winner is " + winner.getPlayer().getName());
41
-    System.out.println(winner.getPlayer().getName() + "\'s hand was: " + winner.getHand().get(0).getName() + " - " + winner.getHand().get(1).getName() + " - " + winner.getHand().get(2).getName() );
42
-    return winner;
33
+        System.out.println("The winner is " + winner.getPlayer().getName());
34
+        System.out.println(winner.getPlayer().getName() + "\'s hand was: " + winner.getHand().get(0).getName() +
35
+                " - " + winner.getHand().get(1).getName() + " - " + winner.getHand().get(2).getName() + "\n\n" );
36
+        return winner;
43 37
     }
44 38
 
45 39
     /**
@@ -53,89 +47,89 @@ public class Stud extends CardGame implements Gamble, Game {
53 47
         int card2 = player.getHand().get(1).getCardValue();
54 48
         int card3 = player.getHand().get(2).getCardValue();
55 49
 
56
-        //Three of a Kind
57
-        if (card1 == card2 && card1 == card3){
58
-            handValue = card1 * 1000000;
59
-        //Two pair
50
+        if (card1 == card2 && card1 == card3) {                         //Three of a Kind
51
+            handValue = threeOfAKindHandValue(card1);
60 52
         }
61
-        else if (card1 == card2){
53
+        else if (card1 == card2 || card1 == card3 || card2 == card3){
54
+            handValue = onePairHandValue(card1, card2, card3);
55
+        }
56
+        else {
57
+            handValue = highCardHandValue(card1, card2, card3);
58
+        }
59
+        return handValue;
60
+    }
61
+
62
+    /**
63
+     * Helper method for handValue() if had is three-of-a-kind
64
+     * @param card1
65
+     * @return
66
+     */
67
+    private int threeOfAKindHandValue(int card1){
68
+        int handValue;
69
+        handValue = card1 * 1000000;
70
+        return handValue;
71
+    }
72
+
73
+    /**
74
+     * Helper method for handValue() if had is one-pair
75
+     * @param card1
76
+     * @param card2
77
+     * @param card3
78
+     * @return
79
+     */
80
+    private int onePairHandValue(int card1, int card2, int card3){
81
+        int handValue = 0;
82
+
83
+        if (card1 == card2){
62 84
             handValue = (card1 * 10000) + card3;
63 85
         }
64 86
         else if (card1 == card3){
65 87
             handValue = (card1 * 10000) + card2;
66 88
         }
67
-        else if (card2 == card3){
89
+        else if (card2 == card3) {
68 90
             handValue = (card2 * 10000) + card1;
69
-        //High Card
70
-        } else {
71
-            // Card1 is Highest
72
-            if (card1 > card2 && card1 > card3 && card2 > card3) {
73
-                handValue = (card1 * 100) + (card2 * 10) + card3;
74
-            }
75
-            else if (card1 > card2 && card1 > card3 && card3 > card2) {
76
-                handValue = (card1 * 100) + (card3 * 10) + card2;
77
-            }
78
-            // Card2 is Highest
79
-            else if (card2 > card1 && card2 > card3 && card1 > card3) {
80
-                handValue = (card2 * 100) + (card1 * 10) + card3;
81
-            }
82
-            else if (card2 > card1 && card2 > card3 && card3 > card1) {
83
-                handValue = (card2 * 100) + (card3 * 10) + card1;
84
-            }
85
-            // Card3 is Highest
86
-            else if (card3 > card1 && card3 > card2 && card1 > card3) {
87
-                handValue = (card3 * 100) + (card1 * 10) + card3;
88
-            }
89
-            else if (card3 > card1 && card3 > card2 && card3 > card1) {
90
-                handValue = (card3 * 100) + (card3 * 10) + card1;
91
-            }
92
-            else {
93
-                handValue = 0;
94
-            }
91
+            return handValue;
95 92
         }
96 93
         return handValue;
97 94
     }
98 95
 
99
-
100
-
101
-
102
-
103
-
104
-
105
-    /*
106
-
107
-        create a bet method which asks the first player how much to bet
108
-        loops through other plays to see how they would like to react to this.
109
-
110
-        fold, call, raise, check
111
-
96
+    /**
97
+     * Helper method for handValue() if had is high-card
98
+     * @param card1
99
+     * @param card2
100
+     * @param card3
101
+     * @return
112 102
      */
113
-
114
-
115
-
116
-
117
-
118
-
119
-    public void bet(int betAmount) {
120
-        super.changeTablePot(betAmount);
121
-        //player.changeBalance(betAmount * -1);
103
+    private int highCardHandValue(int card1, int card2, int card3){
104
+        int handValue;
105
+        // Card1 is Highest
106
+        if (card1 > card2 && card1 > card3 && card2 > card3) {
107
+            handValue = (card1 * 100) + (card2 * 10) + card3;
108
+        }
109
+        else if (card1 > card2 && card1 > card3 && card3 > card2) {
110
+            handValue = (card1 * 100) + (card3 * 10) + card2;
111
+        }
112
+        // Card2 is Highest
113
+        else if (card2 > card1 && card2 > card3 && card1 > card3) {
114
+            handValue = (card2 * 100) + (card1 * 10) + card3;
115
+        }
116
+        else if (card2 > card1 && card2 > card3 && card3 > card1) {
117
+            handValue = (card2 * 100) + (card3 * 10) + card1;
118
+        }
119
+        // Card3 is Highest
120
+        else if (card3 > card1 && card3 > card2 && card1 > card3) {
121
+            handValue = (card3 * 100) + (card1 * 10) + card3;
122
+        }
123
+        else if (card3 > card1 && card3 > card2 && card3 > card1) {
124
+            handValue = (card3 * 100) + (card3 * 10) + card1;
125
+        }
126
+        else {
127
+            handValue = 0;
128
+        }
129
+        return handValue;
122 130
     }
123 131
 
124 132
 
125
-
126
-
127
-
128
-
129
-
130
-
131
-
132
-
133
-
134
-
135
-
136
-
137
-
138
-
139 133
     public void payout(){
140 134
         if(super.getWinner() != null){
141 135
             super.getWinner().changeBalance(super.getTablePot());
@@ -151,19 +145,7 @@ public class Stud extends CardGame implements Gamble, Game {
151 145
         }
152 146
     }
153 147
 
154
-    public void Quit() {
155
-        System.out.println("Play again? Y : or any key to quit.");
156
-        String answer = scanner.next();
157
-        if (answer.equals("Y")){
158
-            startGame();
159
-        } else {
160
-            console = new Console();
161
-        }
162
-    }
163
-
164
-
165 148
     public void startGame() {
166
-        // Deck deck = new Deck();     //CREATE deck for game
167 149
         setHandSize(3);             //SET Hand Size for game(3)
168 150
         payAnte();                  //PAY ante (all players)
169 151
         deal();                     //DEALS cards/ hands to each player
@@ -175,14 +157,14 @@ public class Stud extends CardGame implements Gamble, Game {
175 157
      * Game played in this method by calling the 'gameRound' methods
176 158
      */
177 159
     public void startRound() {
178
-        System.out.println("Welcome to Three Card Stud!");
179
-        //for (int i = 0; i < getHandSize() - 1; i++){    //Each player turns a card in hand to face up
160
+        System.out.println("Welcome to Three Card Stud! Cards are dealt!");
161
+        flipCard();
180 162
         gameRound1();
163
+        flipCard();
181 164
         gameRound2();
182
-        //}
165
+        flipCard();
183 166
         lastGameRound();
184 167
         determineWinner();
185
-        // Payout();
186 168
     }
187 169
 
188 170
 
@@ -190,61 +172,27 @@ public class Stud extends CardGame implements Gamble, Game {
190 172
      * Plays through rounds that includes flipping cards face up and then betting or folding
191 173
      */
192 174
     public void gameRound1(){
193
-
194
-        playersPlayCard();
195
-
196
-        for (int x = 0; x < getPlayers().size(); x++) {                          //Betting round or fold
197
-            CardPlayer player = super.getPlayers().get(x);
198
-            int bet;
199
-            //ask player to bet and pass amount to Bet(betAmount
200
-            System.out.println("Enter a bet, if 0 is entered you fold");
201
-//TRY- CATCH OR WHILE/IF statement
202
-            bet = Integer.parseInt(scanner.next());
203
-            if (bet == 0){
204
-                System.out.println(player.getPlayer().getName() + " folds.");
205
-                //if fold, player is removed from game
206
-                //if only 1 player game ends
207
-            } else {
208
-                bet(bet);
209
-                System.out.println(player.getPlayer().getName() + " bets: " + bet);
210
-            }
211
-        }
175
+        playersPlayCard1();
212 176
     }
213 177
 
214
-    public void playersPlayCard(){
178
+    private void playersPlayCard1(){
215 179
         for (int j = 0; j < getPlayers().size(); j++) {
216 180
             CardPlayer player = super.getPlayers().get(j);                       //GET a player
217 181
             playCard(player.getPlayer(), player.getHand().get(0));      //SHOW-PRINT players first CARD
218
-            //roundCount++;
219 182
         }
220 183
     }
221 184
 
222
-    public void quit() {}
223
-
224 185
     /**
225 186
      * Plays through rounds that includes flipping cards face up and then betting or folding
226 187
      */
227 188
     public void gameRound2(){
189
+        playersPlayCard2();
190
+    }
191
+
192
+    private void playersPlayCard2(){
228 193
         for (int j = 0; j < getPlayers().size(); j++) {
229 194
             CardPlayer player = super.getPlayers().get(j);                       //GET a player
230 195
             playCard(player.getPlayer(), player.getHand().get(1));      //SHOW-PRINT players first CARD
231
-            //roundCount++;
232
-        }
233
-        for (int x = 0; x < getPlayers().size(); x++) {                          //Betting round or fold
234
-            CardPlayer player = super.getPlayers().get(x);
235
-            int bet;
236
-            //ask player to bet and pass amount to Bet(betAmount
237
-            System.out.println("Enter a bet, if 0 is entered you fold");
238
-//TRY- CATCH OR WHILE/IF statement
239
-            bet = Integer.parseInt(scanner.next());
240
-            if (bet == 0){
241
-                System.out.println(player.getPlayer().getName() + " folds.");
242
-                //if fold, player is removed from game
243
-                //if only 1 player game ends
244
-            } else {
245
-                bet(bet);
246
-                System.out.println(player.getPlayer().getName() + " bets: " + bet);
247
-            }
248 196
         }
249 197
     }
250 198
 
@@ -261,7 +209,6 @@ public class Stud extends CardGame implements Gamble, Game {
261 209
         }
262 210
     }
263 211
 
264
-
265 212
     /**
266 213
      * Deal each player(and dealer) 3 face down cards in turn
267 214
      */
@@ -274,4 +221,21 @@ public class Stud extends CardGame implements Gamble, Game {
274 221
             }
275 222
         }
276 223
     }
224
+
225
+    public void flipCard()
226
+    {
227
+        boolean flipnextCard = false;
228
+        while(!flipnextCard) {
229
+            String input = "";
230
+            input = console.getCMDFromUser("Type 'FLIP' to play the cards at the top of your pile");
231
+            if (input.equals("flip")) {
232
+                break;
233
+            } else {
234
+                System.out.println("Sorry, I don't understand that command.");
235
+            }
236
+        }
237
+    }
238
+
239
+    public void quit() {}
277 240
 }
241
+

+ 0
- 1
src/main/java/io/zipcoder/casino/test.java Прегледај датотеку

@@ -1,5 +1,4 @@
1 1
 package io.zipcoder.casino;
2 2
 
3 3
 public class test {
4
-
5 4
 }

+ 18
- 0
src/test/java/io/zipcoder/casino/StudTest.java Прегледај датотеку

@@ -0,0 +1,18 @@
1
+package io.zipcoder.casino;
2
+
3
+import org.junit.Test;
4
+
5
+public class StudTest {
6
+    public static void main(String[] args){
7
+        Casino casino = new Casino();
8
+        //get a player
9
+        casino.setPlayer(casino.getConsole().createAccount());
10
+        //player picks game
11
+        casino.chooseGame();
12
+    }
13
+
14
+    @Test
15
+    public void thismethod(){
16
+
17
+    }
18
+}