Browse Source

refactored BlackJack

ThuyKhong 6 years ago
parent
commit
6e29a9b22f

+ 84
- 50
src/main/java/io/zipcoder/casino/cardgames/BlackJack.java View File

@@ -17,8 +17,6 @@ public class BlackJack extends CardGame implements Gamble {
17 17
     private Deck deck = new Deck();
18 18
     private ArrayList<BlackJackPlayer> blackJackPlayers = new ArrayList<>();
19 19
     CardHand dealerHand = new CardHand();
20
-    long bet;
21
-
22 20
 
23 21
     public BlackJack(){
24 22
         readyPlayers();
@@ -39,54 +37,82 @@ public class BlackJack extends CardGame implements Gamble {
39 37
         }
40 38
     }
41 39
 
42
-    public boolean play(BlackJackPlayer currentPlayer, long bet) {
43
-        // this.bet = bet;
40
+    public void play(BlackJackPlayer currentPlayer, long bet) {
41
+        boolean continueGame = true;
42
+        boolean dealerWins = false;
44 43
 
45
-        Console.println("The dealer is dealing cards to the players.");
44
+        placePlayerBets();
46 45
 
47
-        for (BlackJackPlayer p: this.blackJackPlayers){ //gets bet from each player
48
-            placeBet();
49
-        }
46
+        evalBlackjack(); // all players evaluate hand for blackjack. Winners are paid out and removed.
47
+        dealerWins = dealerCheck(); //TO ADD: if both player and dealer have blackjack, player keeps bet
48
+        if (dealerWins == true) continueGame = false; // all players lose
50 49
 
51
-        dealerHand.add(deck.removeFirst());
52
-        dealerHand.add(deck.removeFirst());
50
+        while (continueGame == true) {
51
+            dealerDraw();
53 52
 
54
-        Console.println("Dealer is showing a " + dealerHand.get(0).getCard()); // dealer reveals upcard
53
+            Console.println("Dealer is showing a " + dealerHand.get(0).getCard()); // dealer reveals upcard
55 54
 
55
+            playersTurn();
56
+
57
+            if (dealersTurn()) continueGame = false;
56 58
 
59
+            evalWinner();
60
+        }
61
+
62
+        if (dealerWins == true) {
63
+            dealerCollectAll();
64
+        }
65
+
66
+    }
67
+
68
+    public boolean dealerCheck() {
57 69
         if (getSum(dealerHand) == 21){
58
-            Console.println("Dealer has +," +  dealerHand.display());
59
-            Console.println("You have " + currentPlayer.getHand().display());
70
+            Console.println("Dealer has " +  dealerHand.display());
60 71
             Console.println("Dealer has blackjack. Dealer wins.");
61
-            for (BlackJackPlayer p: this.blackJackPlayers){ // collects bet from all players
62
-                evaluateBet(p.getP(), bet);
63
-            }
72
+            return true;
64 73
         }
65
-            evalBlackjack();
66
-
67
-            playersTurn();
74
+        return false;
75
+    }
68 76
 
77
+    public void dealerCollectAll() {
78
+        for (BlackJackPlayer p: this.blackJackPlayers){ // collects bet from all players
79
+            evaluateBet(p, -p.getBet());
80
+        }
81
+    }
69 82
 
70
-        if (dealersTurn()) return false;
83
+    public void dealerCollect(BlackJackPlayer p) {
84
+        evaluateBet(p, -p.getBet());
85
+    }
71 86
 
72
-        return evalWinner(currentPlayer, bet);
87
+    public void placePlayerBets() {
88
+        for (BlackJackPlayer p: this.blackJackPlayers){ //gets bet from each player
89
+            Console.println(p.getName());
90
+            placeBet(p);
91
+        }
92
+    }
73 93
 
94
+    public void dealerDraw() {
95
+        dealerHand.add(deck.removeFirst());
96
+        dealerHand.add(deck.removeFirst());
74 97
     }
75 98
 
76
-    public boolean evalWinner(BlackJackPlayer currentPlayer, long bet) {
77
-        if (getSum(dealerHand) == getSum(currentPlayer.getHand())) {
78
-            Console.println("Dealer wins on a tie.");
79
-            evaluateBet(currentPlayer.getP(), -bet);
80
-            return false;
81
-        } else if (getSum(dealerHand) <=21 && getSum(dealerHand) > getSum(currentPlayer.getHand())) {
82
-            Console.println("Dealer wins.");
83
-            evaluateBet(currentPlayer.getP(), -bet);
84
-            return false;
85
-        } else {
86
-            Console.println("You win!");
87
-            evaluateBet(currentPlayer.getP(), bet);
88
-            return false;
99
+    public boolean evalWinner() {
100
+        for (BlackJackPlayer currentPlayer : blackJackPlayers) {
101
+            if (getSum(dealerHand) == getSum(currentPlayer.getHand())) {
102
+                Console.println("Dealer wins on a tie.");
103
+                evaluateBet(currentPlayer, -currentPlayer.getBet());
104
+                return false;
105
+            } else if (getSum(dealerHand) <= 21 && getSum(dealerHand) > getSum(currentPlayer.getHand())) {
106
+                Console.println("Dealer wins.");
107
+                evaluateBet(currentPlayer, -currentPlayer.getBet());
108
+                return false;
109
+            } else {
110
+                Console.println("You win!");
111
+                evaluateBet(currentPlayer, currentPlayer.getBet());
112
+                return false;
113
+            }
89 114
         }
115
+        return true;
90 116
     }
91 117
 
92 118
     public boolean dealersTurn() {
@@ -102,24 +128,27 @@ public class BlackJack extends CardGame implements Gamble {
102 128
             Console.println("Dealer busted. You win.");
103 129
             return true;
104 130
         }
131
+
105 132
         return false;
106 133
     }
107 134
 
108 135
     public void playersTurn() {
136
+        BlackJackPlayer currentPlayer;
109 137
 
110
-        for (BlackJackPlayer currentPlayer : this.blackJackPlayers) {
138
+        for (int i = 0; i < blackJackPlayers.size(); i++) {
139
+            currentPlayer = blackJackPlayers.get(i);
111 140
 
112 141
             Console.println(String.format("%s, you're up!", currentPlayer.getP().getName()));
113
-
114 142
             Console.println("You have: " + currentPlayer.getHand().display() + "\nYour sum is " + getSum(currentPlayer.getHand()));
115 143
 
116 144
             hitOrStand(currentPlayer);
117 145
 
118
-            Console.println("You have: " + currentPlayer.getHand().display());
119
-            Console.println("The sum of your cards is " + getSum(currentPlayer.getHand()));
120
-
121 146
             if (getSum(currentPlayer.getHand()) > 21){
147
+
122 148
                 Console.println("You busted. House wins.");
149
+                dealerCollect(currentPlayer);
150
+                blackJackPlayers.remove(currentPlayer);
151
+
123 152
             }
124 153
         }
125 154
     }
@@ -139,17 +168,21 @@ public class BlackJack extends CardGame implements Gamble {
139 168
     }
140 169
 
141 170
     public void evalBlackjack() {
142
-        for (BlackJackPlayer p: this.blackJackPlayers)
143
-        if (getSum(p.getHand()) == 21){
144
-            Console.println("Dealer has " + dealerHand.display());
145
-            Console.println("You have " + p.getHand().display());
146
-            Console.println("You have blackjack. You win.");
171
+        BlackJackPlayer p;
172
+        for (int i = 0; i < blackJackPlayers.size(); i++) {
173
+            p = blackJackPlayers.get(i);
174
+            if (getSum(p.getHand()) == 21) {
175
+                Console.println(p.getName() + ", you have " + p.getHand().display());
176
+                Console.println(p.getName() + ", you have blackjack. You win!");
177
+                evaluateBet(p, p.getBet());
178
+                blackJackPlayers.remove(i);
179
+            }
147 180
         }
148 181
     }
149 182
 
150 183
     @Override
151 184
     public void dealCards(int numberOfCards) {
152
-
185
+        Console.println("The dealer is dealing cards to the players.");
153 186
         for(BlackJackPlayer p: blackJackPlayers){ //deal 2 cards to each player
154 187
             for (int i = 0; i < numberOfCards; i++) {
155 188
                 Card card = deck.removeFirst();
@@ -167,11 +200,13 @@ public class BlackJack extends CardGame implements Gamble {
167 200
         }
168 201
     }
169 202
 
170
-    public void placeBet() {
171
-        this.bet = Console.getLongInput("Please enter your bet.");
203
+    @Override
204
+    public void placeBet(Player player) {
205
+        BlackJackPlayer blackJackPlayer = (BlackJackPlayer) player;
206
+        long bet = Console.getLongInput("Please enter your bet.");
207
+        blackJackPlayer.setBet(bet);
172 208
     }
173 209
 
174
-
175 210
     public void evaluateBet(Player player, long payout) {
176 211
         player.setChipBalance(player.getChipBalance()+payout);
177 212
     }
@@ -183,7 +218,6 @@ public class BlackJack extends CardGame implements Gamble {
183 218
         return card;
184 219
     }
185 220
 
186
-
187 221
     public int  getSum(CardHand cardHand) {
188 222
         int cardSum = 0;
189 223
         boolean ace = false;
@@ -199,7 +233,7 @@ public class BlackJack extends CardGame implements Gamble {
199 233
             }
200 234
             cardSum = cardSum + cardValue;
201 235
         }
202
-        if (cardHand.size() == 2 && ace == true && cardSum + 10 == 21) {
236
+        if (cardHand.size() == 2 && ace && cardSum + 10 == 21) {
203 237
             cardSum = cardSum + 10;
204 238
         }
205 239
         return cardSum;

+ 8
- 0
src/main/java/io/zipcoder/casino/dicegames/Craps.java View File

@@ -1,5 +1,6 @@
1 1
 package io.zipcoder.casino.dicegames;
2 2
 
3
+import io.zipcoder.casino.player.BlackJackPlayer;
3 4
 import io.zipcoder.casino.utilities.Console;
4 5
 import io.zipcoder.casino.utilities.interfaces.Gamble;
5 6
 import io.zipcoder.casino.player.CrapsPlayer;
@@ -42,6 +43,13 @@ public class Craps extends DiceGame implements Gamble {
42 43
         this.bet = console.integerInputSameLine("Enter your bet: ");
43 44
     }
44 45
 
46
+    @Override
47
+    public void placeBet(Player player) {
48
+        BlackJackPlayer blackJackPlayer = (BlackJackPlayer) player;
49
+        long bet = Console.getLongInput("Please enter your bet.");
50
+        blackJackPlayer.setBet(bet);
51
+    }
52
+
45 53
     public void evaluateBet(Player player, long payout) {
46 54
         player.setChipBalance(player.getChipBalance()+payout);
47 55
     }

+ 0
- 9
src/main/java/io/zipcoder/casino/player/BlackJackPlayer.java View File

@@ -26,9 +26,6 @@ public class BlackJackPlayer extends Player{
26 26
     public void hit(Player player) {
27 27
     }
28 28
 
29
-    public void stay(Player player) {
30
-    }
31
-
32 29
     public Player getP() {
33 30
         return blackJackPlayer;
34 31
     }
@@ -37,10 +34,4 @@ public class BlackJackPlayer extends Player{
37 34
         return hand;
38 35
     }
39 36
 
40
-    public void setHand(CardHand hand) {
41
-        this.hand = hand;
42
-    }
43
-
44
-
45
-
46 37
 }

+ 0
- 44
src/main/java/io/zipcoder/casino/utilities/Pot.java View File

@@ -1,44 +0,0 @@
1
-package io.zipcoder.casino.utilities;
2
-
3
-public class Pot {
4
-
5
-    private static final Pot INSTANCE = new Pot();
6
-
7
-    private long bettingPool;
8
-    private long currentBet;
9
-    private long currentAmount;
10
-
11
-    private Pot(){}
12
-
13
-    public static Pot getInstance(){
14
-        return INSTANCE;
15
-    }
16
-
17
-    public static Pot getINSTANCE() {
18
-        return INSTANCE;
19
-    }
20
-
21
-    public long getBettingPool() {
22
-        return bettingPool;
23
-    }
24
-
25
-    public void setBettingPool(long bettingPool) {
26
-        this.bettingPool = bettingPool;
27
-    }
28
-
29
-    public long getCurrentBet() {
30
-        return currentBet;
31
-    }
32
-
33
-    public void setCurrentBet(long currentBet) {
34
-        this.currentBet = currentBet;
35
-    }
36
-
37
-    public long getCurrentAmount() {
38
-        return currentAmount;
39
-    }
40
-
41
-    public void setCurrentAmount(long currentAmount) {
42
-        this.currentAmount = currentAmount;
43
-    }
44
-}

+ 1
- 1
src/main/java/io/zipcoder/casino/utilities/interfaces/Gamble.java View File

@@ -3,7 +3,7 @@ package io.zipcoder.casino.utilities.interfaces;
3 3
 import io.zipcoder.casino.player.Player;
4 4
 
5 5
 public interface Gamble {
6
-    public void placeBet();
6
+    void placeBet(Player player);
7 7
 
8 8
     void evaluateBet(Player player, long payout);
9 9
 }