Jennifer Chao 6 anos atrás
pai
commit
9d70595eb3

+ 133
- 43
src/main/java/io/zipcoder/casino/CardGame/BlackJack/BlackJack.java Ver arquivo

@@ -3,6 +3,7 @@ package io.zipcoder.casino.CardGame.BlackJack;
3 3
 import io.zipcoder.casino.CardGame.Card;
4 4
 import io.zipcoder.casino.CardGame.CardGame;
5 5
 import io.zipcoder.casino.CardGame.Deck;
6
+import io.zipcoder.casino.CardGame.Face;
6 7
 import io.zipcoder.casino.Interfaces.Gamble;
7 8
 import io.zipcoder.casino.Player;
8 9
 
@@ -11,41 +12,53 @@ import java.util.Scanner;
11 12
 
12 13
 public class BlackJack extends CardGame implements Gamble {
13 14
 
14
-    private ArrayList<BlackJackPlayer> blackJackPlayers;
15
-    private int minBet;
16
-    private ArrayList<Card> wastepile;
17
-    private ArrayList<Card> dealerHand;
18
-    private Deck deck;
19
-    private boolean justDealt;
20
-    private int numOfTurns;
15
+    private ArrayList<BlackJackPlayer> blackJackPlayers = new ArrayList<>();
16
+    private final int minBet = 50;
17
+    private ArrayList<Card> wastepile = new ArrayList<>();
18
+    private Deck deck = new Deck();
19
+    private boolean justDealt = false;
20
+    private int numOfTurns = 0;
21
+    private BlackJackPlayer dealer = new BlackJackPlayer(new Dealer());
22
+
21 23
 
22 24
     public BlackJack(Player player) {
23 25
         BlackJackPlayer blackJackPlayer = new BlackJackPlayer(player);
24
-        this.blackJackPlayers = new ArrayList<BlackJackPlayer>();
26
+        blackJackPlayers.add(dealer);
25 27
         this.blackJackPlayers.add(blackJackPlayer);
26
-        this.minBet = 100;
27
-        this.wastepile = new ArrayList<Card>();
28
-        this.dealerHand = new ArrayList<Card>();
29
-        this.deck = new Deck();
30
-        this.justDealt = false;
31
-        this.numOfTurns = 0;
32 28
     }
33 29
 
34
-    // basically draw testestestetstsetsetstest
35
-    public void hit(BlackJackPlayer blackJackPlayer) {
30
+    public void hit(BlackJackPlayer player) {
36 31
         setJustDealt(false);
37
-        Card playerCard1 = deck.draw();
38
-        blackJackPlayer.addToHand(playerCard1);
39
-        System.out.println(blackJackPlayer.getPlayerHand().toString());
40
-    }
32
+        Card card = deck.draw();
33
+        player.addToHand(card);
41 34
 
42
-    public void flip() {
43
-        // shoulder technically be card.setCovered(false); or something like that
44
-        boolean covered = false;
35
+        if (countPlayerHand(player).get(0) > 21 || (countPlayerHand(player).size() == 2 && countPlayerHand(player).get(1) > 21)) {
36
+            System.out.println("Your Current Hand: \n" + formatHand(player.getPlayerHand()) + "\nHand Value: " + formatHandValue(countPlayerHand(player)) + "\nToo high, you lose!");
37
+        } else {
38
+            System.out.println("Your Current Hand: \n" + formatHand(player.getPlayerHand()) + "\nHand Value: " + formatHandValue(countPlayerHand(player)));
39
+        }
45 40
     }
46 41
 
47
-    public void split() {
42
+//    public void flip() {
43
+//        // shoulder technically be card.setCovered(false); or something like that
44
+//        boolean covered = false;
45
+//    }
46
+
47
+    public void split(BlackJackPlayer player) {
48
+
49
+        System.out.println(player.getPlayerHand() + "\n");
50
+
51
+        Card movingCard = player.getPlayerHand().get(1);
52
+
53
+        if (justDealt && player.getPlayerHand().get(0).getFace() == player.getPlayerHand().get(1).getFace()) {
54
+            ArrayList<Card> newHand = player.createNewHand();
55
+            newHand.add(movingCard);
56
+            player.getPlayerHand().remove(movingCard);
57
+        }
58
+
59
+        System.out.println(player.getPlayerHand() + " " + player.getSecondHand());
48 60
         setJustDealt(false);
61
+
49 62
         // must be the same card value
50 63
     }
51 64
 
@@ -63,33 +76,73 @@ public class BlackJack extends CardGame implements Gamble {
63 76
         // (end turn?) what does "take a turn" actually initiate
64 77
     }
65 78
 
66
-    public int countHand(BlackJackPlayer blackJackPlayer) {
67
-        int handSum = 0;
79
+    public int calculate_AceIsOne(BlackJackPlayer player) {
80
+        ArrayList<Card> playerHand = player.getPlayerHand();
81
+        int aceIsOne = 0;
82
+
83
+        for (int i = 0; i < player.getPlayerHand().size(); i++) {
84
+            if (playerHand.get(i).getFace() == Face.ACE) {
85
+                aceIsOne += playerHand.get(i).getFace().getPrimaryValue();
86
+            } else {
87
+                aceIsOne += playerHand.get(i).getFace().getSecondaryValue();
88
+            }
89
+        }
90
+
91
+        return aceIsOne;
92
+    }
93
+
94
+    public int calculate_Standard(BlackJackPlayer player) {
95
+        ArrayList<Card> playerHand = player.getPlayerHand();
96
+        int aceIsEleven = 0;
68 97
 
69
-        for (int i = 0; i < blackJackPlayer.getPlayerHand().size(); i++) {
70
-            handSum += (blackJackPlayer.getPlayerHand().get(i).getFace().getSecondaryValue());
98
+        for (int i = 0; i < player.getPlayerHand().size(); i++) {
99
+            aceIsEleven += playerHand.get(i).getFace().getSecondaryValue();
71 100
         }
101
+
102
+        return aceIsEleven;
103
+    }
104
+
105
+    public ArrayList<Integer> countPlayerHand(BlackJackPlayer player) {
106
+        ArrayList<Integer> handSum = new ArrayList<>();
107
+        Integer aceIsOne;
108
+        Integer aceIsEleven;
109
+        Integer noAce;
110
+
111
+        if (player.hasAce() && calculate_Standard(player) > 21) {
112
+            player.setHandValue(calculate_AceIsOne(player));
113
+            aceIsOne = player.getHandValue();
114
+            handSum.add(aceIsOne);
115
+
116
+        } else if (player.hasAce() && calculate_Standard(player) < 21) {
117
+            player.setHandValue(calculate_AceIsOne(player));
118
+            aceIsOne = player.getHandValue();
119
+            handSum.add(aceIsOne);
120
+
121
+            player.setHandValue(calculate_Standard(player));
122
+            aceIsEleven = player.getHandValue();
123
+            handSum.add(aceIsEleven);
124
+
125
+        } else {
126
+            player.setHandValue(calculate_Standard(player));
127
+            noAce = player.getHandValue();
128
+            handSum.add(noAce);
129
+        }
130
+
72 131
         return handSum;
73 132
     }
74 133
 
75
-    public void deal(BlackJackPlayer blackJackPlayer) {
76
-        // BlackJackPlayer player1 = blackJackPlayers.get(playerIndex);
77
-        // blackJackPlayers.add(blackJackPlayer);
78
-        // should we move this to start instead? we would also need to deal for each player if multiple
134
+    public void deal() {
79 135
         deck.shuffle();
80
-        Card playerCard1 = deck.draw();
81
-        blackJackPlayer.addToHand(playerCard1);
82 136
 
83
-        Card dealerCard1 = deck.draw();
84
-        dealerHand.add(dealerCard1);
85
-        System.out.println("Mystery Card");
86
-
87
-        Card playerCard2 = deck.draw();
88
-        blackJackPlayer.addToHand(playerCard2);
137
+        for (int i = 0; i < 2; i++) {
138
+            for (BlackJackPlayer player : this.blackJackPlayers) {
139
+                Card card = deck.draw();
140
+                player.addToHand(card);
141
+            }
142
+        }
89 143
 
90
-        Card dealerCard2 = deck.draw();
91
-        dealerHand.add(dealerCard2);
92
-        System.out.println(dealerCard2);
144
+        System.out.println("Your Current Hand: \n" + formatHand(this.blackJackPlayers.get(1).getPlayerHand()) + "\nHand Value: " + formatHandValue(countPlayerHand(this.blackJackPlayers.get(1))) + "\n");
145
+        System.out.println("Dealer's Current Hand: \nMYSTERY-CARD || " + formatHand(dealer.getDealerHand()) + "\nHand Value: ??" + "\n");
93 146
 
94 147
         setJustDealt(true);
95 148
     }
@@ -98,6 +151,10 @@ public class BlackJack extends CardGame implements Gamble {
98 151
         return blackJackPlayers.get(index);
99 152
     }
100 153
 
154
+    public BlackJackPlayer getDealer() {
155
+        return dealer;
156
+    }
157
+
101 158
     public void start() {
102 159
         Scanner reader = new Scanner(System.in);
103 160
         System.out.println("Enter an initial bet: ");
@@ -162,4 +219,37 @@ public class BlackJack extends CardGame implements Gamble {
162 219
     public void setJustDealt(boolean justDealt) {
163 220
         this.justDealt = justDealt;
164 221
     }
222
+
223
+    public String formatHand(ArrayList<Card> array) {
224
+        String stringHand = "";
225
+
226
+        String uglyArray = array.toString();
227
+
228
+        for (int i = 0; i < uglyArray.length(); i++) {
229
+            if (uglyArray.charAt(i) != ' ' && uglyArray.charAt(i) != '[' && uglyArray.charAt(i) != ']' && uglyArray.charAt(i) != ',') {
230
+                stringHand += uglyArray.charAt(i);
231
+            } else if (uglyArray.charAt(i) == ' ') {
232
+                stringHand += " || ";
233
+            }
234
+        }
235
+
236
+        return stringHand;
237
+    }
238
+
239
+    public String formatHandValue(ArrayList<Integer> array) {
240
+        String stringHandValue = "";
241
+
242
+        String uglyArray = array.toString();
243
+
244
+        for (int i = 0; i < uglyArray.length(); i++) {
245
+            if (uglyArray.charAt(i) != ' ' && uglyArray.charAt(i) != '[' && uglyArray.charAt(i) != ']' && uglyArray.charAt(i) != ',') {
246
+                stringHandValue += uglyArray.charAt(i);
247
+            } else if (uglyArray.charAt(i) == ' ') {
248
+                stringHandValue += " or ";
249
+            }
250
+        }
251
+
252
+        return stringHandValue;
253
+    }
254
+
165 255
 }

+ 45
- 11
src/main/java/io/zipcoder/casino/CardGame/BlackJack/BlackJackPlayer.java Ver arquivo

@@ -1,6 +1,7 @@
1 1
 package io.zipcoder.casino.CardGame.BlackJack;
2 2
 
3 3
 import io.zipcoder.casino.CardGame.Card;
4
+import io.zipcoder.casino.CardGame.Face;
4 5
 import io.zipcoder.casino.Player;
5 6
 
6 7
 import java.util.ArrayList;
@@ -10,31 +11,56 @@ public class BlackJackPlayer {
10 11
     private Player player;
11 12
 
12 13
     private int initialBet;
13
-    private int handValue;
14
-    private ArrayList<Card> playerHand;
15
-    private int betPot;
14
+    private int handValue = 0;
15
+    private ArrayList<Card> playerHand = new ArrayList<>();
16
+    private ArrayList<Card> newHand;
17
+    private int betPot = 0;
16 18
 
17 19
     public BlackJackPlayer(Player player) {
18 20
         this.player = player;
19
-        this.handValue = 0;
20
-        this.playerHand = new ArrayList<Card>();
21
-        this.betPot = 0;
22 21
     }
23 22
 
24 23
     public void addToHand(Card card){
25
-        playerHand.add(card);
24
+        this.playerHand.add(card);
25
+    }
26
+
27
+    public boolean hasAce() {
28
+        ArrayList<Face> onlyFaces = new ArrayList<>();
29
+        for (Card card : playerHand) {
30
+            onlyFaces.add(card.getFace());
31
+        }
32
+        return onlyFaces.contains(Face.ACE);
26 33
     }
27 34
 
28 35
     public ArrayList<Card> getPlayerHand() {
29
-        return playerHand;
36
+        return this.playerHand;
37
+    }
38
+
39
+    public ArrayList<Card> createNewHand() {
40
+        newHand = new ArrayList<>();
41
+        return this.newHand;
42
+    }
43
+
44
+    public ArrayList<Card> getSecondHand() {
45
+        return this.newHand;
46
+    }
47
+
48
+    public ArrayList<Card> getDealerHand() {
49
+        ArrayList<Card> dealerHandRemoveMystery = new ArrayList<>();
50
+
51
+        for (int i = 1; i < this.playerHand.size(); i++) {
52
+           dealerHandRemoveMystery.add(this.playerHand.get(i));
53
+        }
54
+
55
+        return dealerHandRemoveMystery;
30 56
     }
31 57
 
32 58
     public Player getPlayer() {
33
-        return player;
59
+        return this.player;
34 60
     }
35 61
 
36 62
     public int getInitialBet() {
37
-        return initialBet;
63
+        return this.initialBet;
38 64
     }
39 65
 
40 66
     public void addToBetPot(int amount) {
@@ -42,11 +68,19 @@ public class BlackJackPlayer {
42 68
     }
43 69
 
44 70
     public int getBetPot() {
45
-        return betPot;
71
+        return this.betPot;
46 72
     }
47 73
 
48 74
     public void setInitialBet(int amount){
49 75
         this.initialBet = amount;
50 76
     }
51 77
 
78
+    public int getHandValue() {
79
+        return this.handValue;
80
+    }
81
+
82
+    public void setHandValue(int value) {
83
+        this.handValue = value;
84
+    }
85
+
52 86
 }

+ 9
- 0
src/main/java/io/zipcoder/casino/CardGame/BlackJack/Dealer.java Ver arquivo

@@ -0,0 +1,9 @@
1
+package io.zipcoder.casino.CardGame.BlackJack;
2
+
3
+import io.zipcoder.casino.Player;
4
+
5
+public class Dealer extends Player {
6
+
7
+    public Dealer() { }
8
+
9
+}

+ 8
- 0
src/main/java/io/zipcoder/casino/Player.java Ver arquivo

@@ -26,4 +26,12 @@ public class Player {
26 26
     public void setName(String name) {
27 27
         this.name = name;
28 28
     }
29
+
30
+
31
+
32
+
33
+    public Player() { }
34
+
35
+
36
+
29 37
 }

+ 130
- 11
src/test/java/io/zipcoder/casino/BlackJackTest.java Ver arquivo

@@ -4,9 +4,12 @@ import io.zipcoder.casino.CardGame.BlackJack.BlackJack;
4 4
 import io.zipcoder.casino.CardGame.BlackJack.BlackJackPlayer;
5 5
 import io.zipcoder.casino.CardGame.Card;
6 6
 import io.zipcoder.casino.CardGame.Deck;
7
+import io.zipcoder.casino.CardGame.Face;
8
+import io.zipcoder.casino.CardGame.Suit;
7 9
 import org.junit.Assert;
8 10
 import org.junit.Test;
9 11
 
12
+import java.lang.reflect.Array;
10 13
 import java.util.ArrayList;
11 14
 
12 15
 public class BlackJackTest {
@@ -14,12 +17,40 @@ public class BlackJackTest {
14 17
     Deck deck = new Deck();
15 18
     Player player = new Player("Jack Black");
16 19
     BlackJack blackJack = new BlackJack(player);
17
-    BlackJackPlayer testPlayer = blackJack.getPlayer(0);
20
+    BlackJackPlayer testPlayer = blackJack.getPlayer(1);
18 21
     ArrayList<Card> testHand = testPlayer.getPlayerHand();
19 22
 
20 23
     @Test
21
-    public void testDeal() {
22
-        blackJack.deal(testPlayer);
24
+    public void testHit() {
25
+        blackJack.deal();
26
+        blackJack.hit(testPlayer);
27
+
28
+        int expected = 3;
29
+        int actual = testHand.size();
30
+
31
+        Assert.assertEquals(expected, actual);
32
+    }
33
+
34
+    @Test
35
+    public void testSplit() {
36
+        blackJack.setJustDealt(true);
37
+        Card seven1 = new Card(Suit.HEARTS, Face.SEVEN);
38
+        Card seven2 = new Card(Suit.SPADES, Face.SEVEN);
39
+
40
+        testHand.add(seven1);
41
+        testHand.add(seven2);
42
+
43
+        blackJack.split(testPlayer);
44
+
45
+        Face expected = testPlayer.getPlayerHand().get(0).getFace();
46
+        Face actual = testPlayer.getSecondHand().get(0).getFace();
47
+
48
+        Assert.assertEquals(expected, actual);
49
+    }
50
+
51
+    @Test
52
+    public void testDeal_player() {
53
+        blackJack.deal();
23 54
 
24 55
         int expected = 2;
25 56
         int actual = testHand.size();
@@ -28,14 +59,70 @@ public class BlackJackTest {
28 59
     }
29 60
 
30 61
     @Test
31
-    public void testCountHand() {
32
-        testPlayer.addToHand(deck.draw());
33
-        testPlayer.addToHand(deck.draw());
62
+    public void testDeal_dealer() {
63
+        blackJack.deal();
64
+
65
+        int expected = 2;
66
+        int actual = blackJack.getDealer().getPlayerHand().size();
67
+
68
+        Assert.assertEquals(expected, actual);
69
+    }
70
+
71
+    @Test
72
+    public void testCountPlayerHand_HasAce_OneOrEleven() {
73
+        Card ace = new Card(Suit.HEARTS, Face.ACE);
74
+        Card two = new Card(Suit.HEARTS, Face.TWO);
75
+        Card six = new Card(Suit.HEARTS, Face.SIX);
76
+
77
+        testHand.add(ace);
78
+        testHand.add(two);
79
+        testHand.add(six);
80
+
81
+        int total_aceEqualsOne = 9;
82
+        int total_aceEqualsEleven = 19;
83
+
84
+        ArrayList<Integer> expected = new ArrayList<>();
85
+        expected.add(total_aceEqualsOne);
86
+        expected.add(total_aceEqualsEleven);
87
+        ArrayList<Integer>  actual = blackJack.countPlayerHand(testPlayer);
88
+
89
+        Assert.assertEquals(expected, actual);
90
+    }
91
+
92
+    @Test
93
+    public void testCountPlayerHand_HasAce_CantBeEleven() {
94
+        Card ace = new Card(Suit.HEARTS, Face.ACE);
95
+        Card king = deck.draw();
96
+        Card queen = deck.draw();
34 97
 
35
-        System.out.println(testHand);
98
+        testHand.add(ace);
99
+        testHand.add(king);
100
+        testHand.add(queen);
36 101
 
37
-        int expected = 25;
38
-        int actual = blackJack.countHand(testPlayer);
102
+        int total_aceEqualsOne = 21;      // int total_aceEqualsEleven = 31;
103
+
104
+        ArrayList<Integer> expected = new ArrayList<>();
105
+        expected.add(total_aceEqualsOne);
106
+        ArrayList<Integer>  actual = blackJack.countPlayerHand(testPlayer);
107
+
108
+        Assert.assertEquals(expected, actual);
109
+    }
110
+
111
+    @Test
112
+    public void testCountPlayerHand_NoAce() {
113
+        Card king = deck.draw();
114
+        Card two = new Card(Suit.HEARTS, Face.TWO);
115
+        Card six = new Card(Suit.HEARTS, Face.SIX);
116
+
117
+        testHand.add(king);
118
+        testHand.add(two);
119
+        testHand.add(six);
120
+
121
+        int total = 18;
122
+
123
+        ArrayList<Integer> expected = new ArrayList<>();
124
+        expected.add(total);
125
+        ArrayList<Integer>  actual = blackJack.countPlayerHand(testPlayer);
39 126
 
40 127
         Assert.assertEquals(expected, actual);
41 128
     }
@@ -43,11 +130,15 @@ public class BlackJackTest {
43 130
     @Test
44 131
     public void testJustDealt() {
45 132
         blackJack.setJustDealt(false);
46
-        System.out.println(blackJack.getJustDealt());
133
+
134
+        boolean expected = false;
135
+        boolean actual = blackJack.getJustDealt();
136
+
137
+        Assert.assertEquals(expected, actual);
47 138
     }
48 139
 
49 140
     @Test
50
-    public void testDoubleDown(){
141
+    public void testDoubleDown() {
51 142
         blackJack.setJustDealt(true);
52 143
         testPlayer.setInitialBet(blackJack.betAmount(50, testPlayer));
53 144
         blackJack.doubleDown(testPlayer);
@@ -58,4 +149,32 @@ public class BlackJackTest {
58 149
         Assert.assertEquals(expected, actual);
59 150
     }
60 151
 
152
+    @Test
153
+    public void testPlayerHasAce_True() {
154
+        Card ace = new Card(Suit.HEARTS, Face.ACE);
155
+
156
+        testHand.add(ace);
157
+        testHand.add(deck.draw());
158
+        testHand.add(deck.draw());
159
+
160
+        boolean expected = true;
161
+        boolean actual = testPlayer.hasAce();
162
+
163
+        Assert.assertEquals(expected, actual);
164
+    }
165
+
166
+    @Test
167
+    public void testPlayerHasAce_False() {
168
+        testHand.add(deck.draw());
169
+        testHand.add(deck.draw());
170
+        testHand.add(deck.draw());
171
+
172
+        boolean expected = false;
173
+        boolean actual = testPlayer.hasAce();
174
+
175
+        Assert.assertEquals(expected, actual);
176
+    }
177
+
178
+
179
+
61 180
 }