|
@@ -1,19 +1,33 @@
|
1
|
1
|
package io.zipcoder.casino.cardgames;
|
2
|
2
|
|
|
3
|
+import io.zipcoder.casino.cardgames.cards.Card;
|
|
4
|
+import io.zipcoder.casino.cardgames.cards.CardHand;
|
|
5
|
+import io.zipcoder.casino.cardgames.cards.CardValue;
|
3
|
6
|
import io.zipcoder.casino.cardgames.cards.Deck;
|
|
7
|
+import io.zipcoder.casino.utilities.Console;
|
4
|
8
|
import io.zipcoder.casino.utilities.Gamble;
|
5
|
9
|
import io.zipcoder.casino.player.BlackJackPlayer;
|
6
|
10
|
import io.zipcoder.casino.player.Player;
|
7
|
11
|
import io.zipcoder.casino.player.Players;
|
8
|
12
|
|
|
13
|
+import java.sql.SQLOutput;
|
9
|
14
|
import java.util.ArrayList;
|
|
15
|
+import java.util.Vector;
|
10
|
16
|
|
11
|
17
|
public class BlackJack extends CardGame implements Gamble {
|
|
18
|
+
|
12
|
19
|
private Deck deck = new Deck();
|
13
|
20
|
private ArrayList<BlackJackPlayer> blackJackPlayers = new ArrayList<>();
|
|
21
|
+ CardHand dealerHand = new CardHand();
|
|
22
|
+ long bet;
|
|
23
|
+
|
14
|
24
|
|
15
|
25
|
public BlackJack(){
|
16
|
26
|
readyPlayers();
|
|
27
|
+ dealCards(2);
|
|
28
|
+ for (BlackJackPlayer p : blackJackPlayers) {
|
|
29
|
+ run();
|
|
30
|
+ }
|
17
|
31
|
}
|
18
|
32
|
|
19
|
33
|
public void readyPlayers() {
|
|
@@ -21,19 +35,151 @@ public class BlackJack extends CardGame implements Gamble {
|
21
|
35
|
blackJackPlayers.add(new BlackJackPlayer(player));
|
22
|
36
|
}
|
23
|
37
|
}
|
24
|
|
- public void dealCards(Player player, int numberOfCards) {
|
25
|
38
|
|
|
39
|
+ public void run(){
|
|
40
|
+ for(int i = 0; i < blackJackPlayers.size(); i++){
|
|
41
|
+ play(blackJackPlayers.get(i),blackJackPlayers.get(i).getBet());
|
|
42
|
+ }
|
26
|
43
|
}
|
27
|
44
|
|
28
|
|
- public void revealCard() {
|
|
45
|
+ public boolean play(BlackJackPlayer currentPlayer, long bet) {
|
|
46
|
+ placeBet();
|
|
47
|
+ // this.bet = bet;
|
|
48
|
+
|
|
49
|
+ Console.println("The dealer is dealing cards to the players.");
|
|
50
|
+
|
|
51
|
+ dealerHand.add(deck.removeFirst());
|
|
52
|
+ dealerHand.add(deck.removeFirst());
|
|
53
|
+
|
|
54
|
+ Console.println(String.format("%s, you're up!", currentPlayer.getP().getName()));
|
|
55
|
+
|
|
56
|
+ if (getSum(dealerHand) == 21){
|
|
57
|
+ Console.println("Dealer has +," + dealerHand.display());
|
|
58
|
+ Console.println("You have " + currentPlayer.getHand().display());
|
|
59
|
+ Console.println("Dealer has blackjack. Dealer wins.");
|
|
60
|
+ }
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+ Console.println(String.format("%s, you're up!", currentPlayer.getP().getName()));
|
|
64
|
+
|
|
65
|
+ if (getSum(currentPlayer.getHand()) == 21){
|
|
66
|
+ Console.println("Dealer has " + dealerHand.display());
|
|
67
|
+ Console.println("You have " + currentPlayer.getHand().display());
|
|
68
|
+ Console.println("You have blackjack. You win.");
|
|
69
|
+ }
|
|
70
|
+
|
|
71
|
+ while(true) {
|
|
72
|
+
|
|
73
|
+ Console.println("You have: " + currentPlayer.getHand().display() + "\nYour sum is " + getSum(currentPlayer.getHand()));
|
|
74
|
+
|
|
75
|
+ String hitOrStand = Console.getStringInput("Do you want to Hit or Stand? \n Enter H for Hit or S for Stand");
|
|
76
|
+
|
|
77
|
+ while (getSum(currentPlayer.getHand()) < 21) {
|
|
78
|
+ if (hitOrStand.equalsIgnoreCase("H")) {
|
|
79
|
+ dealCard(currentPlayer,1);
|
|
80
|
+ } else if (hitOrStand.equalsIgnoreCase("S")) {
|
|
81
|
+ break;
|
|
82
|
+ } else {
|
|
83
|
+ hitOrStand = Console.getStringInput("Invalid input. Please enter H for Hit or S for Stand");
|
|
84
|
+ }
|
|
85
|
+ }
|
|
86
|
+
|
|
87
|
+ Console.println("You have: " + currentPlayer.getHand().display());
|
|
88
|
+ Console.println("The sum of your cards is " + getSum(currentPlayer.getHand()));
|
|
89
|
+
|
|
90
|
+ if (getSum(currentPlayer.getHand()) > 21){
|
|
91
|
+ Console.println("You busted. House wins.");
|
|
92
|
+ return false;
|
|
93
|
+ }
|
|
94
|
+
|
|
95
|
+ Console.println("Dealer's card are: " + dealerHand.display());
|
|
96
|
+ Console.println("The sum of dealer's cards is " + getSum(dealerHand));
|
|
97
|
+
|
|
98
|
+ while (getSum(dealerHand) < 17) {
|
|
99
|
+ revealCard();
|
|
100
|
+ Console.println("Dealer's sum is " + getSum(dealerHand));
|
|
101
|
+ }
|
|
102
|
+
|
|
103
|
+ if (getSum(dealerHand) > 21) {
|
|
104
|
+ Console.println("Dealer busted. You win.");
|
|
105
|
+ return false;
|
|
106
|
+ }
|
|
107
|
+
|
|
108
|
+ if (getSum(dealerHand) == getSum(currentPlayer.getHand())) {
|
|
109
|
+ Console.println("It's a tie. You lose.");
|
|
110
|
+ return false;
|
|
111
|
+ } else if (getSum(dealerHand) <=21 && getSum(dealerHand) > getSum(currentPlayer.getHand())) {
|
|
112
|
+ Console.println("Dealer wins.");
|
|
113
|
+ return false;
|
|
114
|
+ } else {
|
|
115
|
+ Console.println("You win!");
|
|
116
|
+ return false;
|
|
117
|
+ }
|
|
118
|
+
|
|
119
|
+ }
|
|
120
|
+
|
|
121
|
+}
|
|
122
|
+
|
|
123
|
+ public void dealCards(int numberOfCards) {
|
|
124
|
+
|
|
125
|
+ for(BlackJackPlayer p: blackJackPlayers){ //deal 2 cards to each player
|
|
126
|
+ for (int i = 0; i < numberOfCards; i++) {
|
|
127
|
+ Card card = deck.removeFirst();
|
|
128
|
+ p.getHand().add(card);
|
|
129
|
+ }
|
|
130
|
+ }
|
|
131
|
+ }
|
29
|
132
|
|
|
133
|
+ public void dealCard(BlackJackPlayer p, int numberOfCards) {
|
|
134
|
+ for (int i = 0; i < numberOfCards; i++) {
|
|
135
|
+ Card card = deck.removeFirst();
|
|
136
|
+ p.getHand().add(card);
|
|
137
|
+ }
|
30
|
138
|
}
|
31
|
139
|
|
32
|
140
|
public void placeBet() {
|
|
141
|
+ this.bet = Console.getLongInput("Please enter your bet.");
|
|
142
|
+ }
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+ public void evaluateBet(Player player, long payout) {
|
33
|
146
|
|
34
|
147
|
}
|
35
|
148
|
|
36
|
|
- public void evaluateBet(Player player, long payout){
|
37
|
|
-
|
|
149
|
+
|
|
150
|
+ public Card drawCard() {
|
|
151
|
+ // draws card from deck and adds it to "upcard" total
|
|
152
|
+ Card card = deck.removeFirst();
|
|
153
|
+ return card;
|
|
154
|
+ }
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+ public int getSum(CardHand cardHand) {
|
|
158
|
+ int cardSum = 0;
|
|
159
|
+ boolean ace = false;
|
|
160
|
+
|
|
161
|
+ for (int i = 0; i < cardHand.size(); i++) {
|
|
162
|
+ int cardValue = cardHand.get(i).getCardValue().getCardValue();
|
|
163
|
+
|
|
164
|
+ if (cardValue > 10) {
|
|
165
|
+ cardValue = 10; // For a Jack, Queen, or King.
|
|
166
|
+ }
|
|
167
|
+ if (cardValue == 1) {
|
|
168
|
+ ace = true;
|
|
169
|
+ }
|
|
170
|
+ cardSum = cardSum + cardValue;
|
|
171
|
+ }
|
|
172
|
+ if (cardHand.size() == 2 && ace == true && cardSum + 10 == 21) {
|
|
173
|
+ cardSum = cardSum + 10;
|
|
174
|
+ }
|
|
175
|
+ return cardSum;
|
|
176
|
+ }
|
|
177
|
+
|
|
178
|
+ public void revealCard(){
|
|
179
|
+ Card newCard = drawCard();
|
|
180
|
+ Console.println("Dealer drew a " + newCard.getCardValue() + " of " + newCard.getSuit());
|
|
181
|
+ dealerHand.add(newCard);
|
38
|
182
|
}
|
|
183
|
+
|
|
184
|
+
|
39
|
185
|
}
|