Jennifer Chao 6 years ago
parent
commit
9d70595eb3

+ 133
- 43
src/main/java/io/zipcoder/casino/CardGame/BlackJack/BlackJack.java View File

3
 import io.zipcoder.casino.CardGame.Card;
3
 import io.zipcoder.casino.CardGame.Card;
4
 import io.zipcoder.casino.CardGame.CardGame;
4
 import io.zipcoder.casino.CardGame.CardGame;
5
 import io.zipcoder.casino.CardGame.Deck;
5
 import io.zipcoder.casino.CardGame.Deck;
6
+import io.zipcoder.casino.CardGame.Face;
6
 import io.zipcoder.casino.Interfaces.Gamble;
7
 import io.zipcoder.casino.Interfaces.Gamble;
7
 import io.zipcoder.casino.Player;
8
 import io.zipcoder.casino.Player;
8
 
9
 
11
 
12
 
12
 public class BlackJack extends CardGame implements Gamble {
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
     public BlackJack(Player player) {
24
     public BlackJack(Player player) {
23
         BlackJackPlayer blackJackPlayer = new BlackJackPlayer(player);
25
         BlackJackPlayer blackJackPlayer = new BlackJackPlayer(player);
24
-        this.blackJackPlayers = new ArrayList<BlackJackPlayer>();
26
+        blackJackPlayers.add(dealer);
25
         this.blackJackPlayers.add(blackJackPlayer);
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
         setJustDealt(false);
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
         setJustDealt(false);
60
         setJustDealt(false);
61
+
49
         // must be the same card value
62
         // must be the same card value
50
     }
63
     }
51
 
64
 
63
         // (end turn?) what does "take a turn" actually initiate
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
         return handSum;
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
         deck.shuffle();
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
         setJustDealt(true);
147
         setJustDealt(true);
95
     }
148
     }
98
         return blackJackPlayers.get(index);
151
         return blackJackPlayers.get(index);
99
     }
152
     }
100
 
153
 
154
+    public BlackJackPlayer getDealer() {
155
+        return dealer;
156
+    }
157
+
101
     public void start() {
158
     public void start() {
102
         Scanner reader = new Scanner(System.in);
159
         Scanner reader = new Scanner(System.in);
103
         System.out.println("Enter an initial bet: ");
160
         System.out.println("Enter an initial bet: ");
162
     public void setJustDealt(boolean justDealt) {
219
     public void setJustDealt(boolean justDealt) {
163
         this.justDealt = justDealt;
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 View File

1
 package io.zipcoder.casino.CardGame.BlackJack;
1
 package io.zipcoder.casino.CardGame.BlackJack;
2
 
2
 
3
 import io.zipcoder.casino.CardGame.Card;
3
 import io.zipcoder.casino.CardGame.Card;
4
+import io.zipcoder.casino.CardGame.Face;
4
 import io.zipcoder.casino.Player;
5
 import io.zipcoder.casino.Player;
5
 
6
 
6
 import java.util.ArrayList;
7
 import java.util.ArrayList;
10
     private Player player;
11
     private Player player;
11
 
12
 
12
     private int initialBet;
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
     public BlackJackPlayer(Player player) {
19
     public BlackJackPlayer(Player player) {
18
         this.player = player;
20
         this.player = player;
19
-        this.handValue = 0;
20
-        this.playerHand = new ArrayList<Card>();
21
-        this.betPot = 0;
22
     }
21
     }
23
 
22
 
24
     public void addToHand(Card card){
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
     public ArrayList<Card> getPlayerHand() {
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
     public Player getPlayer() {
58
     public Player getPlayer() {
33
-        return player;
59
+        return this.player;
34
     }
60
     }
35
 
61
 
36
     public int getInitialBet() {
62
     public int getInitialBet() {
37
-        return initialBet;
63
+        return this.initialBet;
38
     }
64
     }
39
 
65
 
40
     public void addToBetPot(int amount) {
66
     public void addToBetPot(int amount) {
42
     }
68
     }
43
 
69
 
44
     public int getBetPot() {
70
     public int getBetPot() {
45
-        return betPot;
71
+        return this.betPot;
46
     }
72
     }
47
 
73
 
48
     public void setInitialBet(int amount){
74
     public void setInitialBet(int amount){
49
         this.initialBet = amount;
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 View File

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 View File

26
     public void setName(String name) {
26
     public void setName(String name) {
27
         this.name = name;
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 View File

4
 import io.zipcoder.casino.CardGame.BlackJack.BlackJackPlayer;
4
 import io.zipcoder.casino.CardGame.BlackJack.BlackJackPlayer;
5
 import io.zipcoder.casino.CardGame.Card;
5
 import io.zipcoder.casino.CardGame.Card;
6
 import io.zipcoder.casino.CardGame.Deck;
6
 import io.zipcoder.casino.CardGame.Deck;
7
+import io.zipcoder.casino.CardGame.Face;
8
+import io.zipcoder.casino.CardGame.Suit;
7
 import org.junit.Assert;
9
 import org.junit.Assert;
8
 import org.junit.Test;
10
 import org.junit.Test;
9
 
11
 
12
+import java.lang.reflect.Array;
10
 import java.util.ArrayList;
13
 import java.util.ArrayList;
11
 
14
 
12
 public class BlackJackTest {
15
 public class BlackJackTest {
14
     Deck deck = new Deck();
17
     Deck deck = new Deck();
15
     Player player = new Player("Jack Black");
18
     Player player = new Player("Jack Black");
16
     BlackJack blackJack = new BlackJack(player);
19
     BlackJack blackJack = new BlackJack(player);
17
-    BlackJackPlayer testPlayer = blackJack.getPlayer(0);
20
+    BlackJackPlayer testPlayer = blackJack.getPlayer(1);
18
     ArrayList<Card> testHand = testPlayer.getPlayerHand();
21
     ArrayList<Card> testHand = testPlayer.getPlayerHand();
19
 
22
 
20
     @Test
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
         int expected = 2;
55
         int expected = 2;
25
         int actual = testHand.size();
56
         int actual = testHand.size();
28
     }
59
     }
29
 
60
 
30
     @Test
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
         Assert.assertEquals(expected, actual);
127
         Assert.assertEquals(expected, actual);
41
     }
128
     }
43
     @Test
130
     @Test
44
     public void testJustDealt() {
131
     public void testJustDealt() {
45
         blackJack.setJustDealt(false);
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
     @Test
140
     @Test
50
-    public void testDoubleDown(){
141
+    public void testDoubleDown() {
51
         blackJack.setJustDealt(true);
142
         blackJack.setJustDealt(true);
52
         testPlayer.setInitialBet(blackJack.betAmount(50, testPlayer));
143
         testPlayer.setInitialBet(blackJack.betAmount(50, testPlayer));
53
         blackJack.doubleDown(testPlayer);
144
         blackJack.doubleDown(testPlayer);
58
         Assert.assertEquals(expected, actual);
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
 }