Selaa lähdekoodia

blackjack works???

NedRedmond 6 vuotta sitten
vanhempi
commit
2480184b35

+ 0
- 1
src/main/java/io/zipcoder/casino/Casino.java Näytä tiedosto

82
     }
82
     }
83
 
83
 
84
     public void printBalance() {
84
     public void printBalance() {
85
-      console.println("FYI guys:\n");
86
       for (Player player : players.getPlayers()){
85
       for (Player player : players.getPlayers()){
87
         console.println(String.format(player.getName() + " has " + player.getChipBalance() + " chips.\n"));
86
         console.println(String.format(player.getName() + " has " + player.getChipBalance() + " chips.\n"));
88
       }
87
       }

+ 28
- 15
src/main/java/io/zipcoder/casino/cardgames/BlackJack.java Näytä tiedosto

38
         }
38
         }
39
     }
39
     }
40
 
40
 
41
+    boolean continueGame = true;
41
     public void play(BlackJackPlayer currentPlayer, long bet) {
42
     public void play(BlackJackPlayer currentPlayer, long bet) {
42
-        boolean continueGame = true;
43
         boolean dealerWins = false;
43
         boolean dealerWins = false;
44
 
44
 
45
         placePlayerBets();
45
         placePlayerBets();
55
 
55
 
56
             playersTurn();
56
             playersTurn();
57
 
57
 
58
+            if (blackJackPlayers.size() < 1) continueGame = false;
59
+
58
             continueGame = dealersTurn(); // IMPLEMENT: if dealer loses, all players should receive payout
60
             continueGame = dealersTurn(); // IMPLEMENT: if dealer loses, all players should receive payout
59
 
61
 
60
             evalWinner();
62
             evalWinner();
61
 
63
 
64
+            continueGame = false;
62
         }
65
         }
63
 
66
 
64
         if (dealerWins == true) {
67
         if (dealerWins == true) {
108
     public boolean evalWinner() {
111
     public boolean evalWinner() {
109
         for (BlackJackPlayer currentPlayer : blackJackPlayers) {
112
         for (BlackJackPlayer currentPlayer : blackJackPlayers) {
110
             if (getSum(dealerHand) == getSum(currentPlayer.getHand())) {
113
             if (getSum(dealerHand) == getSum(currentPlayer.getHand())) {
111
-                Console.println("Dealer wins on a tie.");
114
+                Console.println(String.format("Against %s's hand, dealer wins on a tie.", currentPlayer.getP().getName()));
112
                 evaluateBet(currentPlayer, -currentPlayer.getBet());
115
                 evaluateBet(currentPlayer, -currentPlayer.getBet());
113
                 return false;
116
                 return false;
114
             } else if (getSum(dealerHand) <= 21 && getSum(dealerHand) > getSum(currentPlayer.getHand())) {
117
             } else if (getSum(dealerHand) <= 21 && getSum(dealerHand) > getSum(currentPlayer.getHand())) {
115
-                Console.println("Dealer wins.");
118
+                Console.println(String.format("Against %s's hand, dealer wins."), currentPlayer.getP().getName());
116
                 evaluateBet(currentPlayer, -currentPlayer.getBet());
119
                 evaluateBet(currentPlayer, -currentPlayer.getBet());
117
                 return false;
120
                 return false;
118
             } else {
121
             } else {
119
-                Console.println("You win!");
122
+                Console.println(String.format("%s, you win!", currentPlayer.getP().getName()));
120
                 evaluateBet(currentPlayer, currentPlayer.getBet());
123
                 evaluateBet(currentPlayer, currentPlayer.getBet());
121
                 return false;
124
                 return false;
122
             }
125
             }
136
         if (getSum(dealerHand) > 21) {
139
         if (getSum(dealerHand) > 21) {
137
             Console.println("Dealer busted. You win.");
140
             Console.println("Dealer busted. You win.");
138
             payoutAll();
141
             payoutAll();
139
-            return true;
142
+            return false;
140
         }
143
         }
141
 
144
 
142
-        return false;
145
+        return true;
143
     }
146
     }
144
 
147
 
145
     public void playersTurn() {
148
     public void playersTurn() {
149
             currentPlayer = blackJackPlayers.get(i);
152
             currentPlayer = blackJackPlayers.get(i);
150
 
153
 
151
             Console.println(String.format("%s, you're up!", currentPlayer.getP().getName()));
154
             Console.println(String.format("%s, you're up!", currentPlayer.getP().getName()));
152
-            Console.println("You have: " + currentPlayer.getHand().display() + "\nYour sum is " + getSum(currentPlayer.getHand()));
155
+            displaySumPlayerHand(currentPlayer);
153
 
156
 
154
             hitOrStand(currentPlayer);
157
             hitOrStand(currentPlayer);
158
+        }
159
+    }
155
 
160
 
156
-            if (getSum(currentPlayer.getHand()) > 21){
157
-
158
-                Console.println("You busted. House wins.");
159
-                dealerCollect(currentPlayer);
160
-                blackJackPlayers.remove(currentPlayer);
161
-
162
-            }
161
+    private boolean playerBust(BlackJackPlayer currentPlayer) {
162
+        if (getSum(currentPlayer.getHand()) > 21){
163
+            Console.println(currentPlayer.getP().getName() + " busted. House wins.");
164
+            dealerCollect(currentPlayer);
165
+            blackJackPlayers.remove(currentPlayer);
166
+            return false;
163
         }
167
         }
168
+        return true;
169
+    }
170
+
171
+    private void displaySumPlayerHand(BlackJackPlayer currentPlayer) {
172
+        Console.println(currentPlayer.getP().getName() + ", you have: " + currentPlayer.getHand().display() + "\nThis hand is worth " + getSum(currentPlayer.getHand()));
164
     }
173
     }
165
 
174
 
166
     public void hitOrStand(BlackJackPlayer currentPlayer) {
175
     public void hitOrStand(BlackJackPlayer currentPlayer) {
167
-        String hitOrStand = Console.getStringInput("Do you want to Hit or Stand? \n Enter H for Hit or S for Stand");
168
 
176
 
169
         while (getSum(currentPlayer.getHand()) < 21) {
177
         while (getSum(currentPlayer.getHand()) < 21) {
178
+            String hitOrStand = Console.getStringInput(currentPlayer.getP().getName() + ", do you want to Hit or Stand?\nEnter H for Hit or S for Stand");
179
+
170
             if (hitOrStand.equalsIgnoreCase("H")) {
180
             if (hitOrStand.equalsIgnoreCase("H")) {
171
                 dealCards(currentPlayer, 1);
181
                 dealCards(currentPlayer, 1);
182
+                Console.println(currentPlayer.getP().getName() + ", you have been dealt %s.", currentPlayer.getHand().get(currentPlayer.getHand().size()-1).getCard());
183
+                displaySumPlayerHand(currentPlayer);
184
+                continueGame = playerBust(currentPlayer);
172
             } else if (hitOrStand.equalsIgnoreCase("S")) {
185
             } else if (hitOrStand.equalsIgnoreCase("S")) {
173
                 break;
186
                 break;
174
             } else {
187
             } else {