Quellcode durchsuchen

adding blackjack

Jennifer Chao vor 6 Jahren
Ursprung
Commit
b72ca74062

+ 151
- 48
src/main/java/io/zipcoder/casino/CardGame/BlackJack/BlackJack.java Datei anzeigen

@@ -3,53 +3,62 @@ 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
 
9 10
 import java.util.ArrayList;
11
+import java.util.Scanner;
10 12
 
11 13
 public class BlackJack extends CardGame implements Gamble {
12 14
 
13
-    private ArrayList<BlackJackPlayer> blackJackPlayers;
14
-    private int minBet;
15
-    private ArrayList<Card> wastepile;
16
-    private ArrayList<Card> dealerHand;
17
-    private Deck deck;
18
-    private boolean justDealt;
19
-    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
+
20 23
 
21 24
     public BlackJack(Player player) {
22 25
         BlackJackPlayer blackJackPlayer = new BlackJackPlayer(player);
23
-        this.blackJackPlayers = new ArrayList<BlackJackPlayer>();
26
+        blackJackPlayers.add(dealer);
24 27
         this.blackJackPlayers.add(blackJackPlayer);
25
-        this.minBet = 100;
26
-        this.wastepile = new ArrayList<Card>();
27
-        this.dealerHand = new ArrayList<Card>();
28
-        this.deck = new Deck();
29
-        this.justDealt = false;
30
-        this.numOfTurns = 0;
31 28
     }
32 29
 
33
-//    public BlackJack(){
34
-//        this.blackJackPlayers = new ArrayList<BlackJackPlayer>();
35
-//    }
30
+    public void hit(BlackJackPlayer player) {
31
+        setJustDealt(false);
32
+        Card card = deck.draw();
33
+        player.addToHand(card);
36 34
 
37
-//    public BlackJack(Player player) {
38
-//        BlackJackPlayer blackJackPlayer = new BlackJackPlayer(player);
39
-//        this.blackJackPlayers.add(blackJackPlayer);
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
+        }
40
+    }
41
+
42
+//    public void flip() {
43
+//        // shoulder technically be card.setCovered(false); or something like that
44
+//        boolean covered = false;
40 45
 //    }
41 46
 
42
-    // basically draw
43
-    public Card hit() {
44
-        return null;
45
-    }
47
+    public void split(BlackJackPlayer player) {
46 48
 
47
-    public void flip() {
48
-        // shoulder technically be card.setCovered(false); or something like that
49
-        boolean covered = false;
50
-    }
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());
60
+        setJustDealt(false);
51 61
 
52
-    public void split() {
53 62
         // must be the same card value
54 63
     }
55 64
 
@@ -57,6 +66,8 @@ public class BlackJack extends CardGame implements Gamble {
57 66
         if (getJustDealt() == true) {
58 67
             blackJackPlayer.addToBetPot(blackJackPlayer.getInitialBet());
59 68
         }
69
+
70
+        setJustDealt(false);
60 71
         // must be right after deal, and you can only get one more card
61 72
     }
62 73
 
@@ -65,30 +76,73 @@ public class BlackJack extends CardGame implements Gamble {
65 76
         // (end turn?) what does "take a turn" actually initiate
66 77
     }
67 78
 
68
-    public int countHand(BlackJackPlayer blackJackPlayer) {
69
-        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;
70 97
 
71
-        for (int i = 0; i < blackJackPlayer.getPlayerHand().size(); i++) {
72
-            handSum += (blackJackPlayer.getPlayerHand().get(i).getFace().getSecondaryValue());
98
+        for (int i = 0; i < player.getPlayerHand().size(); i++) {
99
+            aceIsEleven += playerHand.get(i).getFace().getSecondaryValue();
73 100
         }
74
-        return handSum;
101
+
102
+        return aceIsEleven;
75 103
     }
76 104
 
77
-    public void deal(BlackJackPlayer blackJackPlayer) {
78
-        // BlackJackPlayer player1 = blackJackPlayers.get(playerIndex);
79
-        blackJackPlayers.add(blackJackPlayer);
80
-        // should we move this to start instead? we would also need to deal for each player if multiple
81
-        Card playerCard1 = deck.draw();
82
-        blackJackPlayer.addToHand(playerCard1);
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
+
131
+        return handSum;
132
+    }
83 133
 
84
-        Card dealerCard1 = deck.draw();
85
-        dealerHand.add(dealerCard1);
134
+    public void deal() {
135
+        deck.shuffle();
86 136
 
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);
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");
92 146
 
93 147
         setJustDealt(true);
94 148
     }
@@ -97,9 +151,26 @@ public class BlackJack extends CardGame implements Gamble {
97 151
         return blackJackPlayers.get(index);
98 152
     }
99 153
 
154
+    public BlackJackPlayer getDealer() {
155
+        return dealer;
156
+    }
157
+
100 158
     public void start() {
159
+        Scanner reader = new Scanner(System.in);
160
+        System.out.println("Enter an initial bet: ");
161
+        int initialBet = reader.nextInt();
162
+
163
+        start(initialBet);
164
+    }
165
+
166
+    public void start(int initialBet) {
101 167
         // upon starting a new game, every player places a bet? make a loop???
102
-        betAmount(50, blackJackPlayers.get(0));
168
+        BlackJackPlayer blackJackPlayer = blackJackPlayers.get(0);
169
+        if (initialBet < minBet) {
170
+            System.out.println("TOo low");
171
+        } else {
172
+            blackJackPlayer.setInitialBet(betAmount(initialBet, blackJackPlayers.get(0)));
173
+        }
103 174
     }
104 175
 
105 176
     public void end() {
@@ -126,7 +197,6 @@ public class BlackJack extends CardGame implements Gamble {
126 197
                 break;
127 198
             }
128 199
         }
129
-
130 200
     }
131 201
 
132 202
     public int betAmount(int amount, BlackJackPlayer blackJackPlayer) {
@@ -149,4 +219,37 @@ public class BlackJack extends CardGame implements Gamble {
149 219
     public void setJustDealt(boolean justDealt) {
150 220
         this.justDealt = justDealt;
151 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
+
152 255
 }

+ 45
- 11
src/main/java/io/zipcoder/casino/CardGame/BlackJack/BlackJackPlayer.java Datei anzeigen

@@ -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 Datei anzeigen

@@ -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 Datei anzeigen

@@ -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 Datei anzeigen

@@ -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
 }