Quellcode durchsuchen

blackjack works???

NedRedmond vor 6 Jahren
Ursprung
Commit
2480184b35

+ 0
- 1
src/main/java/io/zipcoder/casino/Casino.java Datei anzeigen

@@ -82,7 +82,6 @@ public class Casino {
82 82
     }
83 83
 
84 84
     public void printBalance() {
85
-      console.println("FYI guys:\n");
86 85
       for (Player player : players.getPlayers()){
87 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 Datei anzeigen

@@ -38,8 +38,8 @@ public class BlackJack extends CardGame implements Gamble {
38 38
         }
39 39
     }
40 40
 
41
+    boolean continueGame = true;
41 42
     public void play(BlackJackPlayer currentPlayer, long bet) {
42
-        boolean continueGame = true;
43 43
         boolean dealerWins = false;
44 44
 
45 45
         placePlayerBets();
@@ -55,10 +55,13 @@ public class BlackJack extends CardGame implements Gamble {
55 55
 
56 56
             playersTurn();
57 57
 
58
+            if (blackJackPlayers.size() < 1) continueGame = false;
59
+
58 60
             continueGame = dealersTurn(); // IMPLEMENT: if dealer loses, all players should receive payout
59 61
 
60 62
             evalWinner();
61 63
 
64
+            continueGame = false;
62 65
         }
63 66
 
64 67
         if (dealerWins == true) {
@@ -108,15 +111,15 @@ public class BlackJack extends CardGame implements Gamble {
108 111
     public boolean evalWinner() {
109 112
         for (BlackJackPlayer currentPlayer : blackJackPlayers) {
110 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 115
                 evaluateBet(currentPlayer, -currentPlayer.getBet());
113 116
                 return false;
114 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 119
                 evaluateBet(currentPlayer, -currentPlayer.getBet());
117 120
                 return false;
118 121
             } else {
119
-                Console.println("You win!");
122
+                Console.println(String.format("%s, you win!", currentPlayer.getP().getName()));
120 123
                 evaluateBet(currentPlayer, currentPlayer.getBet());
121 124
                 return false;
122 125
             }
@@ -136,10 +139,10 @@ public class BlackJack extends CardGame implements Gamble {
136 139
         if (getSum(dealerHand) > 21) {
137 140
             Console.println("Dealer busted. You win.");
138 141
             payoutAll();
139
-            return true;
142
+            return false;
140 143
         }
141 144
 
142
-        return false;
145
+        return true;
143 146
     }
144 147
 
145 148
     public void playersTurn() {
@@ -149,26 +152,36 @@ public class BlackJack extends CardGame implements Gamble {
149 152
             currentPlayer = blackJackPlayers.get(i);
150 153
 
151 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 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 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 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 180
             if (hitOrStand.equalsIgnoreCase("H")) {
171 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 185
             } else if (hitOrStand.equalsIgnoreCase("S")) {
173 186
                 break;
174 187
             } else {