jpsp91 6 лет назад
Родитель
Сommit
a82c4665b7

+ 257
- 123
src/main/java/io/zipcoder/casino/Leviathan/Games/BlackJack.java Просмотреть файл

@@ -1,7 +1,11 @@
1 1
 package io.zipcoder.casino.Leviathan.Games;
2
+
2 3
 import io.zipcoder.casino.Leviathan.Interfaces.*;
3 4
 import io.zipcoder.casino.Leviathan.*;
4
-import  io.zipcoder.casino.Leviathan.Games.GameUtilities.*;
5
+import io.zipcoder.casino.Leviathan.Games.GameUtilities.*;
6
+
7
+import java.util.ArrayList;
8
+import java.util.List;
5 9
 
6 10
 public class BlackJack extends CardGame implements Gambling {
7 11
 
@@ -9,200 +13,330 @@ public class BlackJack extends CardGame implements Gambling {
9 13
     int wageAmount;
10 14
     int totalChips;
11 15
     Player aPlayer;
16
+    boolean playAgain = true;
12 17
 
13
-    public BlackJack(Player aPlayer){
18
+    public BlackJack(Player aPlayer) {
14 19
         this.aPlayer = aPlayer;
15 20
     }
16 21
 
22
+
17 23
     int aPlayerScore = Game.playerScore;
18 24
     int aHouseScore = Game.houseScore;
19 25
 
20 26
     //assuming this to be a random card rank in int
21
-    //Card aCard = new Card(Rank.ACE,Suit.CLUBS);
22
-    //Rank aGetRank = aCard.getRank();
23
-
27
+//    Card aCard = new Card(Rank.ACE,Suit.CLUBS);
28
+//    Rank aGetRank = aCard.getRank();
29
+//
24 30
     Deck deck = new Deck();
25 31
 
26
-
32
+//    Card acard = deck.draw();
33
+//    Rank rank = acard.getRank();
34
+//    int player = rank.getValue();
35
+//    String playervalueString = rank.toString();
36
+//    Suit suit = acard.getSuit();
37
+//    String suitString = suit.toString();
27 38
 
28 39
     String playerDecision;
29 40
 
30 41
 
31
-    public void playGame() {
32
-        
33
-    }
42
+////////////////////////////////////////////////////////////////////////////////////
34 43
 
35
-    public int drawASingleCard(int cardRank){
36 44
 
37
-        Card card = deck.draw();
45
+    //draws the initial two cards
46
+    public List<Card> drawNewHand() {
47
+        List<Card> hand = new ArrayList<>();
38 48
 
39
-        Rank rank = card.getRank();
49
+        hand.add(deck.draw());
50
+        hand.add(deck.draw());
40 51
 
41
-        int cardInt =rank.getValue();
42
-
43
-        return cardInt;
52
+        return hand;
44 53
     }
45 54
 
46
-    //Player making the starting bet
47
-    public int wageMoney() {
55
+    //get the sum int value of the drawn cards
56
+    public int getHandTotal(List<Card> hand) {
57
+        int sum = 0;
58
+        int countAces = 0;
59
+
60
+        for (Card c : hand) {
61
+            Rank rank = c.getRank();
62
+            int value = rank.getValue();
63
+
64
+            if (value <= 10) {
65
+                sum += value;
66
+            }
67
+            if (rank.equals(Rank.JACK) || rank.equals(Rank.QUEEN) || rank.equals(Rank.KING)) {
68
+                sum += 10;
69
+            }
70
+            if (rank.equals(Rank.ACE)) {
71
+                sum += 0;
72
+            }
73
+
74
+            if (rank.equals(Rank.ACE)) {
75
+                countAces += 1;
76
+            }
48 77
 
49
-        totalChips=aPlayer.getTotalChips();
78
+        }
50 79
 
51
-        wageAmount = aConsole.getIntInput("Please make your starting bet:");
52
-        if (wageAmount <= totalChips) {
53
-            aConsole.println("Your current bet amount is: " + wageAmount);
54
-            return wageAmount;
80
+        if (countAces == 0) {
81
+            return sum;
82
+        }
83
+
84
+        if ((sum + 11 + (countAces - 1)) <= 21) {
85
+            return sum + 11 + (countAces - 1);
55 86
         } else {
56
-            aConsole.println("Insufficient Chips!");
57
-            //Recursion - this runs the method again and asks user to make the starting bet again
58
-            return wageMoney();
87
+            return sum + countAces;
59 88
         }
89
+
60 90
     }
61 91
 
62
-    //initial player card value
63
-    private int startPlayerHand() {
92
+    //lets the player know of the starting hand
93
+    public String handToString(List<Card> hand) {
94
+        StringBuilder builder = new StringBuilder();
95
+        for (Card c : hand) {
96
+            String cardString = (c.getRank() + " of " + c.getSuit());
97
+            builder.append(cardString).append("\n");
98
+        }
99
+        return builder.toString();
100
+    }
64 101
 
65
-//        Card card = deck.draw();
66
-//
67
-//        Rank rank = card.getRank();
68
-//
69
-//        int cardInt =rank.getRank();
102
+    //*** Look this over and figure it out ***
103
+    //lets the player know one of house's starting hand
104
+    public String houseHandToString(List<Card> hand, int cardsToShow) {
105
+        StringBuilder builder = new StringBuilder();
106
+        for (int i = 0; i < cardsToShow; i++) {
107
+            builder.append(hand.get(i).getRank() + " of " + hand.get(i).getSuit() + "\n");
108
+        }
70 109
 
71
-        //aPlayerScore = drawASingleCard();
110
+        return builder.toString();
111
+    }
72 112
 
73 113
 
114
+    //draws a singe card
115
+    public Card drawSingleCard() {
74 116
 
75
-        return aPlayerScore; //= aGetRank + aGetRank;
117
+        return deck.draw();
76 118
     }
77 119
 
78
-    //initial house card value
79
-    public int startHouseHand() {
80
-        return aHouseScore; //= aGetRank + aGetRank;
81
-    }
82 120
 
121
+    //Gets the int value of a single drawn card
122
+    //Figure out how to apply this for just one card instead of going through the list
123
+    public int getSingleCardValue(List<Card> hand) {
124
+        int cardValue = 0;
83 125
 
84
-    //ask for player's next move
85
-    public String blackJack(String playerDecision) {
86
-        if(startPlayerHand() < 21){
87
-            playerDecision = aConsole.getStringInput("Please make a call:\nHit\nStand\nRaise Bet");
126
+        for (Card c : hand) {
127
+            Rank rank = c.getRank();
128
+            int value = rank.getValue();
129
+            cardValue += value;
88 130
         }
89
-        return playerDecision;
90
-    }
91 131
 
132
+        return cardValue;
133
+
134
+    }
92 135
 
93 136
 
94
-    //Hit
95
-    public int hit(int playerHand) {
137
+    public String drawnCardToString(Card card) {
96 138
 
139
+        return card.getRank() + " of " + card.getSuit();
97 140
 
98
-        return 0;
99 141
     }
100 142
 
101 143
 
102
-    //Stand
103
-    public int stand(int playerHand) {
144
+////////////////////////////////////////////////////////////////////////////////////
145
+
146
+    public void playGame() {
147
+
148
+        Boolean result;
104 149
 
105 150
 
106
-        return 0;
151
+        while (playAgain == true) {
152
+
153
+            aConsole.println("*** Welcome " + this.aPlayer.getName() +
154
+                    ", let’s play BlackJack! ***\n");
155
+
156
+            int wageAmount = wageMoney();
157
+
158
+            //Deck deck = new Deck();
159
+
160
+
161
+            //draw a fresh hand
162
+            List<Card> playerHand = drawNewHand();
163
+
164
+            //get the player's hand total
165
+            int handTotal = getHandTotal(playerHand);
166
+
167
+            //update player score with hand total
168
+            aPlayerScore = handTotal;
169
+
170
+            //show the player their hand
171
+            String handString = handToString(playerHand);
172
+            aConsole.println("\n You have drawn: \n" + handString);
173
+
174
+            //display hand total
175
+            aConsole.println("Your current hand value: " + aPlayerScore + "\n");
176
+
177
+
178
+            //draw a fresh hand for house
179
+            List<Card> houseHand = drawNewHand();
180
+
181
+            //get the house hand total
182
+            int houseHandTotal = getHandTotal(houseHand);
183
+
184
+            //update house score with hand total
185
+            aHouseScore = houseHandTotal;
186
+
187
+            //show the player one of house's card
188
+            //I need to figure out how I could only show one card
189
+            String houseHandString = houseHandToString(houseHand, 1);
190
+            aConsole.println("\n The house has drawn: \n" + houseHandString +
191
+                    "And has one card faced down");
192
+
193
+            //Decision
194
+            while (aPlayerScore < 21) {
195
+                //playerDecision = aConsole.getStringInput("Please make a call:\nHit\nStand\nRaise Bet");
196
+                playerDecision = aConsole.getStringInput("Please make a call:\nHit\nStand");
197
+
198
+                //Hit
199
+                if (playerDecision.equalsIgnoreCase("Hit")) {
200
+                    playerHit(playerHand);
201
+                }
202
+
203
+                if (playerDecision.equalsIgnoreCase("Stand")) {
204
+                    //House start drawing cards
205
+                    houseDraws(houseHand);
206
+                    break;
207
+                }
208
+            }
209
+            //House winning situation
210
+            if (aPlayerScore > 21) {
211
+                aConsole.println("Bust!");
212
+            }else if ((aHouseScore >= aPlayerScore && aHouseScore < 21) || aHouseScore == 21) {
213
+                aConsole.println("You lose\n" + "Your score: " + aPlayerScore +
214
+                        "\nHouse score: " + aHouseScore);
215
+                aConsole.println("\n Dealer's hand: \n" + handToString(houseHand));
216
+            }
217
+
218
+            //Player winning situation
219
+            else if (aHouseScore < 21) {
220
+                aConsole.println("You win!");
221
+
222
+            } else {
223
+                //aConsole.println("You win!");
224
+                aConsole.println("You win!\n" + "Your score: " + aPlayerScore +
225
+                        "\nHouse score: " + aHouseScore);
226
+            }
227
+
228
+            if ((aPlayer.getTotalChips() == 0) || aConsole.getStringInput("Would you like to play again?").equalsIgnoreCase("no")) {
229
+                playAgain = false;
230
+
231
+            }
232
+
233
+        }
107 234
     }
108 235
 
109
-    //Raise bet
110
-    public int raiseBet(int wageAmount) {
236
+    private void playerHit(List<Card> playerHand) {
237
+        //draw a new card
238
+        Card drawSingleCard = drawSingleCard();
239
+        playerHand.add(drawSingleCard);
240
+
241
+        //update player score with card value
242
+        aPlayerScore = getHandTotal(playerHand);
243
+
111 244
 
245
+        //show the newly drawn card
246
+        String newCardString = drawnCardToString(drawSingleCard);
247
+        aConsole.println("\n You have drawn: \n" + newCardString);
112 248
 
113
-        return 0;
249
+        //display updated hand total
250
+        aConsole.println("Your current hand value: " + aPlayerScore + "\n");
114 251
     }
115 252
 
253
+    private void houseDraws(List<Card> houseHand) {
254
+        while (aPlayerScore > aHouseScore && aHouseScore < 21) {
255
+            //draw a new card
256
+            Card drawSingleCard = drawSingleCard();
116 257
 
258
+            houseHand.add(drawSingleCard);
117 259
 
260
+            //get the card value
261
+            //int cardValue = getSingleCardValue(drawSingleCard);
118 262
 
263
+            //update house score with card value
264
+            aHouseScore = getHandTotal(houseHand);
119 265
 
120 266
 
267
+            //show the newly drawn card
268
+            String newCardString = drawnCardToString(drawSingleCard);
269
+            aConsole.println("\n House have drawn: \n" + newCardString);
270
+        }
271
+    }
121 272
 
122
-}
123 273
 
274
+    public int wageMoney() {
275
+        int bet;
124 276
 
125
-//    public boolean playGame(Player aPlayer) {
126
-//        return false;
127
-//    }
128
-//
129
-//
277
+        totalChips = aPlayer.getTotalChips();
278
+
279
+        bet = aConsole.getIntInput("Please make your starting bet:\n" +
280
+                "You currently have: " + aPlayer.getTotalChips() + " chips.");
281
+
282
+        if (bet > 0 && bet <= totalChips) {
283
+            aConsole.println("Your current bet amount is: " + bet);
284
+            return bet;
285
+        } else if (bet == 0 || bet < 0) {
286
+            aConsole.println("You need to bet at least 1 chip");
287
+            return wageMoney();
288
+        } if(bet > totalChips) {
289
+            aConsole.println("Insufficient Chips!");
290
+            //return wageMoney();
291
+            return wageMoney();
292
+
293
+        }
294
+        //return wageMoney();
295
+        //return betMoney();
296
+        return bet;
297
+    }
298
+
299
+
300
+//    //Player making the starting bet
130 301
 //    public int wageMoney() {
131
-//        return 0;
132
-//    }
133
-//
134
-//
135
-//
136
-//    private static final String HOLD_CONSTANT = "hold";
137
-//
138
-//
139
-//    public void blackJack (String[] args){
140
-//
141
-//        //user input for Int
142
-//        int betAmount = 0;
143
-//
144
-//        //user input for String
145
-//        String decision;
146
-//
147
-//
148
-//
149
-//
150
-//
151
-//
152
-//        int aPlayerScore = Game.playerScore;
153 302
 //
154
-//        int aHouseScore = Game.houseScore;
155
-//
156
-//
157
-//
158
-//
159
-//        //How do I make this work??
160
-//        //Card aCard = new Card();
161
-//        //or
162
-//        //int aCard = Card.getRank;
163
-//
164
-//
165
-//        ////////public int startTheGame(int playerScore, int houseScore, int getRank)
166
-//
167
-//        //playerScore = Card.getRank() + Card.getRank();
168
-//
169
-//        //houseScore = Card.getRank() + Card.getRank();
170
-//
171
-//
172
-//
173
-//
174
-//
175
-//        while(!HOLD_CONSTANT.equals(decision) || playerScore < 21) {
176
-//
177
-//            //IF statement that asks for userInput after each loop(?)
178
-//            //add sum values of two cards to playerScore hand
179
-//            //add sum values of two cards to houseScore hand
180
-//
181
-//           decision = aConsole.getStringInput("Please make a call:\nHit\nStand");
182
-//
183
-//
184
-//            If(decision.equals("Hit")) {
185
-//
186
-//                If(decision.equals("Ace") && playerScore > 10) {
187
-//                    playerScore + 1;
188
-//                }
189
-//                else{
190
-//                    playerScore + 11;
191
-//                }
192
-//            }
193
-//            else if(aConsole.getStringInput().equals("Stand"))
303
+//        totalChips = aPlayer.getTotalChips();
194 304
 //
305
+//        wageAmount = aConsole.getIntInput("Please make your starting bet:");
195 306
 //
196 307
 //
308
+//        if (wageAmount > 0 && wageAmount <= totalChips) {
309
+//            aConsole.println("Your current bet amount is: " + wageAmount);
310
+//            return wageAmount;
311
+//        }
312
+//        if (wageAmount > totalChips) {
313
+//            aConsole.println("Insufficient Chips!!!!!!!!!!!!!!!!!!");
314
+//            //Recursion - this runs the method again and asks user to make the starting bet again
315
+//            //return wageMoney();
316
+//        }
317
+//        return wageMoney();
197 318
 //    }
319
+
320
+
321
+//    public int findWinner(int player,int house,int betAmount,int totalChips)
322
+//    {
198 323
 //
199
-//    If(playerScore > 21){
324
+//        if (player > house) {
200 325
 //
326
+//            totalChips = totalChips+ betAmount;
201 327
 //        }
328
+//        else {
202 329
 //
203
-//
330
+//            totalChips = totalChips-betAmount;
331
+//        }
332
+//        aPlayer.setTotalChips(totalChips);
333
+//        return totalChips;
204 334
 //    }
205 335
 
206 336
 
337
+}
338
+
339
+
340
+
207 341
 
208 342
 

+ 63
- 4
src/test/java/io/zipcoder/casino/BlackJackTest.java Просмотреть файл

@@ -1,18 +1,30 @@
1 1
 package io.zipcoder.casino;
2 2
 
3
+import io.zipcoder.casino.Leviathan.Games.BlackJack;
4
+import io.zipcoder.casino.Leviathan.Games.GameUtilities.Card;
5
+import io.zipcoder.casino.Leviathan.Games.GameUtilities.Rank;
6
+import io.zipcoder.casino.Leviathan.Games.GameUtilities.Suit;
7
+import io.zipcoder.casino.Leviathan.Player;
3 8
 import org.junit.Before;
4 9
 import org.junit.Test;
10
+
11
+import java.util.ArrayList;
12
+import java.util.List;
13
+
5 14
 import static org.junit.Assert.assertEquals;
6 15
 
7 16
 public class BlackJackTest {
8 17
 
18
+    private BlackJack testBlackJack;
19
+
9 20
     @Before
10
-    public void setUp()
11
-    {
21
+    public void setUp() {
22
+        Player p = new Player("name", 25, 25);
23
+        this.testBlackJack = new BlackJack(p);
12 24
     }
13 25
 
14 26
     @Test
15
-    public void testStartPlayerScore(){
27
+    public void testStartPlayerScore() {
16 28
         //Given
17 29
 
18 30
         //When
@@ -23,7 +35,7 @@ public class BlackJackTest {
23 35
     }
24 36
 
25 37
     @Test
26
-    public void testStartHouseScore(){
38
+    public void testStartHouseScore() {
27 39
         //Given
28 40
 
29 41
         //When
@@ -33,6 +45,53 @@ public class BlackJackTest {
33 45
         assertEquals(actual, expected);
34 46
     }
35 47
 
48
+    @Test
49
+    public void testDrawNewHandHasTwoCards() {
50
+        int expected = 2;
51
+        int actual = testBlackJack.drawNewHand().size();
52
+
53
+        assertEquals(expected, actual);
54
+    }
55
+
56
+
57
+
58
+
59
+    @Test
60
+    public void testGetHandTotal() {
61
+        Card c1 = new Card(Rank.FOUR, Suit.CLUBS);
62
+        Card c2 = new Card(Rank.EIGHT, Suit.CLUBS);
63
+        Card c3 = new Card(Rank.TWO, Suit.CLUBS);
36 64
 
65
+        List<Card> hand = new ArrayList<>();
66
+        hand.add(c1);
67
+        hand.add(c2);
68
+        hand.add(c3);
37 69
 
70
+        int expected = 14;
71
+        int actual = testBlackJack.getHandTotal(hand);
72
+
73
+        assertEquals(expected, actual);
74
+    }
75
+
76
+
77
+    @Test
78
+    public void testHandToString() {
79
+        Card c1 = new Card(Rank.FOUR, Suit.CLUBS);
80
+        Card c2 = new Card(Rank.EIGHT, Suit.CLUBS);
81
+        Card c3 = new Card(Rank.TWO, Suit.CLUBS);
82
+
83
+        List<Card> hand = new ArrayList<>();
84
+        hand.add(c1);
85
+        hand.add(c2);
86
+        hand.add(c3);
87
+
88
+        String expected = "FOUR of CLUBS\n" +
89
+                "EIGHT of CLUBS\n" +
90
+                "TWO of CLUBS\n";
91
+        String actual = testBlackJack.handToString(hand);
92
+
93
+        System.out.println(actual);
94
+
95
+        assertEquals(expected, actual);
96
+    }
38 97
 }