Browse Source

Merge branch 'Working' of tkhong102/ZCW-OOP-Casino into Working

nedredmond 6 years ago
parent
commit
fb7f2181a7

+ 92
- 49
src/main/java/io/zipcoder/casino/cardgames/BlackJack.java View File

17
     private Deck deck = new Deck();
17
     private Deck deck = new Deck();
18
     private ArrayList<BlackJackPlayer> blackJackPlayers = new ArrayList<>();
18
     private ArrayList<BlackJackPlayer> blackJackPlayers = new ArrayList<>();
19
     CardHand dealerHand = new CardHand();
19
     CardHand dealerHand = new CardHand();
20
-    long bet;
21
-
22
 
20
 
23
     public BlackJack(){
21
     public BlackJack(){
24
         readyPlayers();
22
         readyPlayers();
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();
52
+
53
+            Console.println("Dealer is showing a " + dealerHand.get(0).getCard()); // dealer reveals upcard
54
+
55
+            playersTurn();
56
+
57
+            continueGame = dealersTurn(); // IMPLEMENT: if dealer loses, all players should receive payout
58
+
59
+            evalWinner();
53
 
60
 
54
-        Console.println("Dealer is showing a " + dealerHand.get(0).getCard()); // dealer reveals upcard
61
+        }
62
+
63
+        if (dealerWins == true) {
64
+            dealerCollectAll();
65
+        }
55
 
66
 
67
+    }
56
 
68
 
69
+    public boolean dealerCheck() {
57
         if (getSum(dealerHand) == 21){
70
         if (getSum(dealerHand) == 21){
58
-            Console.println("Dealer has +," +  dealerHand.display());
59
-            Console.println("You have " + currentPlayer.getHand().display());
71
+            Console.println("Dealer has " +  dealerHand.display());
60
             Console.println("Dealer has blackjack. Dealer wins.");
72
             Console.println("Dealer has blackjack. Dealer wins.");
61
-            for (BlackJackPlayer p: this.blackJackPlayers){ // collects bet from all players
62
-                evaluateBet(p.getP(), bet);
63
-            }
73
+            return true;
64
         }
74
         }
65
-            evalBlackjack();
75
+        return false;
76
+    }
66
 
77
 
67
-            playersTurn();
78
+    public void dealerCollectAll() {
79
+        for (BlackJackPlayer p: this.blackJackPlayers){ // collects bet from all players
80
+            evaluateBet(p, -p.getBet());
81
+        }
82
+    }
68
 
83
 
84
+    public void payoutAll() {
85
+        for (BlackJackPlayer p: this.blackJackPlayers){ // collects bet from all players
86
+            long winnings = p.getBet()*2;
87
+            evaluateBet(p, winnings);
88
+        }
89
+    }
69
 
90
 
70
-        if (dealersTurn()) return false;
91
+    public void dealerCollect(BlackJackPlayer p) {
92
+        evaluateBet(p, -p.getBet());
93
+    }
71
 
94
 
72
-        return evalWinner(currentPlayer, bet);
95
+    public void placePlayerBets() {
96
+        for (BlackJackPlayer p: this.blackJackPlayers){ //gets bet from each player
97
+            Console.println(p.getName());
98
+            placeBet(p);
99
+        }
100
+    }
73
 
101
 
102
+    public void dealerDraw() {
103
+        dealerHand.add(deck.removeFirst());
104
+        dealerHand.add(deck.removeFirst());
74
     }
105
     }
75
 
106
 
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;
107
+    public boolean evalWinner() {
108
+        for (BlackJackPlayer currentPlayer : blackJackPlayers) {
109
+            if (getSum(dealerHand) == getSum(currentPlayer.getHand())) {
110
+                Console.println("Dealer wins on a tie.");
111
+                evaluateBet(currentPlayer, -currentPlayer.getBet());
112
+                return false;
113
+            } else if (getSum(dealerHand) <= 21 && getSum(dealerHand) > getSum(currentPlayer.getHand())) {
114
+                Console.println("Dealer wins.");
115
+                evaluateBet(currentPlayer, -currentPlayer.getBet());
116
+                return false;
117
+            } else {
118
+                Console.println("You win!");
119
+                evaluateBet(currentPlayer, currentPlayer.getBet());
120
+                return false;
121
+            }
89
         }
122
         }
123
+        return true;
90
     }
124
     }
91
 
125
 
92
     public boolean dealersTurn() {
126
     public boolean dealersTurn() {
100
 
134
 
101
         if (getSum(dealerHand) > 21) {
135
         if (getSum(dealerHand) > 21) {
102
             Console.println("Dealer busted. You win.");
136
             Console.println("Dealer busted. You win.");
137
+            payoutAll();
103
             return true;
138
             return true;
104
         }
139
         }
140
+
105
         return false;
141
         return false;
106
     }
142
     }
107
 
143
 
108
     public void playersTurn() {
144
     public void playersTurn() {
145
+        BlackJackPlayer currentPlayer;
109
 
146
 
110
-        for (BlackJackPlayer currentPlayer : this.blackJackPlayers) {
147
+        for (int i = 0; i < blackJackPlayers.size(); i++) {
148
+            currentPlayer = blackJackPlayers.get(i);
111
 
149
 
112
             Console.println(String.format("%s, you're up!", currentPlayer.getP().getName()));
150
             Console.println(String.format("%s, you're up!", currentPlayer.getP().getName()));
113
-
114
             Console.println("You have: " + currentPlayer.getHand().display() + "\nYour sum is " + getSum(currentPlayer.getHand()));
151
             Console.println("You have: " + currentPlayer.getHand().display() + "\nYour sum is " + getSum(currentPlayer.getHand()));
115
 
152
 
116
             hitOrStand(currentPlayer);
153
             hitOrStand(currentPlayer);
117
 
154
 
118
-            Console.println("You have: " + currentPlayer.getHand().display());
119
-            Console.println("The sum of your cards is " + getSum(currentPlayer.getHand()));
120
-
121
             if (getSum(currentPlayer.getHand()) > 21){
155
             if (getSum(currentPlayer.getHand()) > 21){
156
+
122
                 Console.println("You busted. House wins.");
157
                 Console.println("You busted. House wins.");
158
+                dealerCollect(currentPlayer);
159
+                blackJackPlayers.remove(currentPlayer);
160
+
123
             }
161
             }
124
         }
162
         }
125
     }
163
     }
139
     }
177
     }
140
 
178
 
141
     public void evalBlackjack() {
179
     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.");
180
+        BlackJackPlayer p;
181
+        for (int i = 0; i < blackJackPlayers.size(); i++) {
182
+            p = blackJackPlayers.get(i);
183
+            if (getSum(p.getHand()) == 21) {
184
+                Console.println(p.getName() + ", you have " + p.getHand().display());
185
+                Console.println(p.getName() + ", you have blackjack. You win!");
186
+                evaluateBet(p, p.getBet());
187
+                blackJackPlayers.remove(i);
188
+            }
147
         }
189
         }
148
     }
190
     }
149
 
191
 
150
     @Override
192
     @Override
151
     public void dealCards(int numberOfCards) {
193
     public void dealCards(int numberOfCards) {
152
-
194
+        Console.println("The dealer is dealing cards to the players.");
153
         for(BlackJackPlayer p: blackJackPlayers){ //deal 2 cards to each player
195
         for(BlackJackPlayer p: blackJackPlayers){ //deal 2 cards to each player
154
             for (int i = 0; i < numberOfCards; i++) {
196
             for (int i = 0; i < numberOfCards; i++) {
155
                 Card card = deck.removeFirst();
197
                 Card card = deck.removeFirst();
167
         }
209
         }
168
     }
210
     }
169
 
211
 
170
-    public void placeBet() {
171
-        this.bet = Console.getLongInput("Please enter your bet.");
212
+    @Override
213
+    public void placeBet(Player player) {
214
+        BlackJackPlayer blackJackPlayer = (BlackJackPlayer) player;
215
+        long bet = Console.getLongInput("Please enter your bet.");
216
+        blackJackPlayer.setBet(bet);
172
     }
217
     }
173
 
218
 
174
-
175
     public void evaluateBet(Player player, long payout) {
219
     public void evaluateBet(Player player, long payout) {
176
         player.setChipBalance(player.getChipBalance()+payout);
220
         player.setChipBalance(player.getChipBalance()+payout);
177
     }
221
     }
183
         return card;
227
         return card;
184
     }
228
     }
185
 
229
 
186
-
187
     public int  getSum(CardHand cardHand) {
230
     public int  getSum(CardHand cardHand) {
188
         int cardSum = 0;
231
         int cardSum = 0;
189
         boolean ace = false;
232
         boolean ace = false;
199
             }
242
             }
200
             cardSum = cardSum + cardValue;
243
             cardSum = cardSum + cardValue;
201
         }
244
         }
202
-        if (cardHand.size() == 2 && ace == true && cardSum + 10 == 21) {
245
+        if (cardHand.size() == 2 && ace && cardSum + 10 == 21) {
203
             cardSum = cardSum + 10;
246
             cardSum = cardSum + 10;
204
         }
247
         }
205
         return cardSum;
248
         return cardSum;

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

1
 package io.zipcoder.casino.dicegames;
1
 package io.zipcoder.casino.dicegames;
2
 
2
 
3
+import io.zipcoder.casino.player.BlackJackPlayer;
3
 import io.zipcoder.casino.utilities.Console;
4
 import io.zipcoder.casino.utilities.Console;
4
 import io.zipcoder.casino.utilities.interfaces.Gamble;
5
 import io.zipcoder.casino.utilities.interfaces.Gamble;
5
 import io.zipcoder.casino.player.CrapsPlayer;
6
 import io.zipcoder.casino.player.CrapsPlayer;
42
         this.bet = console.integerInputSameLine("Enter your bet: ");
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
     public void evaluateBet(Player player, long payout) {
53
     public void evaluateBet(Player player, long payout) {
46
         player.setChipBalance(player.getChipBalance()+payout);
54
         player.setChipBalance(player.getChipBalance()+payout);
47
     }
55
     }

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

26
     public void hit(Player player) {
26
     public void hit(Player player) {
27
     }
27
     }
28
 
28
 
29
-    public void stay(Player player) {
30
-    }
31
-
32
     public Player getP() {
29
     public Player getP() {
33
         return blackJackPlayer;
30
         return blackJackPlayer;
34
     }
31
     }
37
         return hand;
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
-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
 import io.zipcoder.casino.player.Player;
3
 import io.zipcoder.casino.player.Player;
4
 
4
 
5
 public interface Gamble {
5
 public interface Gamble {
6
-    public void placeBet();
6
+    void placeBet(Player player);
7
 
7
 
8
     void evaluateBet(Player player, long payout);
8
     void evaluateBet(Player player, long payout);
9
 }
9
 }