Bladeren bron

settled conflicts with stud

Jonathan Hinds 6 jaren geleden
bovenliggende
commit
a30590db31

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

@@ -81,6 +81,10 @@ public abstract class CardGame {
81 81
         return handSize;
82 82
     }
83 83
 
84
+    public void setHandSize(int handSize) {
85
+        this.handSize = handSize;
86
+    }
87
+
84 88
     public CardPlayer getPlayersTurn() {
85 89
         return playersTurn;
86 90
     }

+ 4
- 0
src/main/java/io/zipcoder/casino/CardPlayer.java Bestand weergeven

@@ -17,6 +17,10 @@ public class CardPlayer {
17 17
         return hand;
18 18
     }
19 19
 
20
+    public void setHand(ArrayList<Card> hand) {
21
+        this.hand = hand;
22
+    }
23
+
20 24
     public Player getPlayer() {
21 25
         return player;
22 26
     }

+ 39
- 7
src/main/java/io/zipcoder/casino/Console.java Bestand weergeven

@@ -7,14 +7,16 @@ public class Console {
7 7
     private Scanner scanner = new Scanner(System.in);
8 8
     private ArrayList<String> gameLib = new ArrayList<>();
9 9
     private Game game = null;
10
-    private Player player;
10
+    private ArrayList<Player> players = new ArrayList<>();
11 11
     private boolean running = true;
12 12
 
13
+
13 14
     Console(){
14 15
         gameLib.add("yahtzee");
15 16
         gameLib.add("war");
16 17
         gameLib.add("stud");
17 18
         gameLib.add("quit");
19
+
18 20
     }
19 21
 
20 22
     public void createAccount()
@@ -25,9 +27,14 @@ public class Console {
25 27
         System.out.println("How much money are you bringing to the table?");
26 28
         int balance = getIntFromUser();
27 29
 
28
-        player = new Player(name, balance);
30
+        Player player = new Player(name, balance);
31
+        players.add(player);
29 32
     }
30 33
 
34
+    /**
35
+     * Currently required to make Stud a 2 player game, could later refactor to declare 'player' in createAccount method and pass to PlayerArray
36
+     */
37
+
31 38
     public void chooseGame()
32 39
     {
33 40
         while(running) {
@@ -39,21 +46,21 @@ public class Console {
39 46
                 case "war":
40 47
                     int[] warMinMax = getMinMax();
41 48
                     Game war = new War(warMinMax[0], warMinMax[1], 10);
42
-                    ((War) war).addPlayers(player);
49
+                    ((War) war).addPlayers(players.get(0));
43 50
                     ((War) war).addNpc();
44 51
                     war.startGame();
45 52
                     break;
46 53
 
47
-                case "three card stud":
54
+                case "stud":
48 55
                     int[] studMinMax = getMinMax();
49 56
                     Game stud = new Stud(studMinMax[0], studMinMax[1], 10);
50
-                    ((Stud) stud).addPlayers(player);
51
-                    ((Stud) stud).addNpc();
57
+                    ((Stud) stud).addPlayers(players.get(0));
58
+                    addStudPlayers(stud);
52 59
                     stud.startGame();
53 60
                     break;
54 61
 
55 62
                 case "yahtzee":
56
-                    Game yahtzee = new Yahtzee(player);
63
+                    Game yahtzee = new Yahtzee(players.get(0));
57 64
                     yahtzee.startGame();
58 65
                     break;
59 66
 
@@ -134,4 +141,29 @@ public class Console {
134 141
         }
135 142
         return command;
136 143
     }
144
+
145
+    public int getStudPlayers() {
146
+        int numOfStudPlayers = 0;
147
+
148
+        while (numOfStudPlayers <= 1) {
149
+
150
+            System.out.println("How many players are there?");
151
+            try {
152
+                numOfStudPlayers = scanner.nextInt();
153
+            } catch (InputMismatchException e) {
154
+                System.out.println("Please enter a number");
155
+            }
156
+        }
157
+
158
+        return numOfStudPlayers;
159
+    }
160
+
161
+    public void addStudPlayers(Game game){
162
+        int numPlayers = getStudPlayers();
163
+
164
+        for(int i = 1; i < numPlayers; i ++) {
165
+            createAccount();
166
+            ((Stud) game).addPlayers(players.get(i));
167
+        }
168
+    }
137 169
 }

+ 1
- 0
src/main/java/io/zipcoder/casino/Gamble.java Bestand weergeven

@@ -1,6 +1,7 @@
1 1
 package io.zipcoder.casino;
2 2
 
3 3
 public interface Gamble {
4
+
4 5
      void bet(int betAmount);
5 6
      void payout();
6 7
 }

+ 8
- 0
src/main/java/io/zipcoder/casino/Printer.java Bestand weergeven

@@ -27,4 +27,12 @@ public class Printer {
27 27
     public static void unacceptableMinBet(){
28 28
         System.out.println("Your bet must be above $0");
29 29
     }
30
+
31
+    public static void studHandsDealt(){
32
+        System.out.println("Each player Dealt 3 cards");
33
+    }
34
+
35
+    public static void showCard(Player player, Card card){
36
+        System.out.println(player.getName() + " shows a " + card.getName());
37
+    }
30 38
 }

+ 230
- 13
src/main/java/io/zipcoder/casino/Stud.java Bestand weergeven

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

+ 4
- 2
src/main/java/io/zipcoder/casino/War.java Bestand weergeven

@@ -97,7 +97,6 @@ public class War extends CardGame implements Gamble, Game {
97 97
                 war = true;
98 98
             }
99 99
         }
100
-
101 100
         if(war)
102 101
         {
103 102
             warMembers.add(winner);
@@ -195,6 +194,9 @@ public class War extends CardGame implements Gamble, Game {
195 194
                 super.getDeck().remove(card);
196 195
             }
197 196
         }
198
-        System.out.println(super.getPlayersTurn().getPlayer().getName() + " has: " + super.getPlayersTurn().getHand().size() + " cards.");
197
+
198
+        System.out.println(super.getPlayersTurn().getPlayer().getName() +
199
+                "has: " + super.getPlayersTurn().getHand().size() + " cards.");
200
+
199 201
     }
200 202
 }