-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBlackjack.java
More file actions
189 lines (156 loc) · 6.18 KB
/
Blackjack.java
File metadata and controls
189 lines (156 loc) · 6.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
public class Blackjack extends Game {
//TODO: Don't reshuffle deck between each game. Need to keep track of how many cards we have left.
Scanner scan = new Scanner(System.in);
boolean gameOn = false;
boolean willHit = true;
boolean dealerHit = true;
boolean playerBust = false;
boolean dealerBust = false;
int playerValue = 0;
int dealerValue = 0;
Game game = new Game();
Map<Integer, Integer> cardValues = game.getCardValues();
public int getPlayerValue() {
return playerValue;
}
public int getDealerValue() {
return dealerValue;
}
public boolean playGame(boolean gameOn, Player player) {
if (player.getChipStack() == 0) {
gameOn = false;
} else {
String initialMessage = "Do you want to play Blackjack";
String playAgain = initialMessage + " again?";
if (gameOn) {
System.out.println(playAgain);
} else {
System.out.println(initialMessage + "?");
}
String result = scan.next();
if (result.toLowerCase().charAt(0) == 'y') {
gameOn = true;
}
}
return gameOn;
}
public int makeBet(Player player) {
System.out.println("You have " + player.getChipStack() + " chips in your stack.");
System.out.println("Place your bet!");
int result = scan.nextInt();
boolean madeBet = player.makeBet(result);
if (madeBet) {
System.out.println("You have bet " + result + " chips.");
}
return result;
}
public List<List<Card>> initialDeal(Deck deck) {
List<Card> playerCards = deck.dealCards(2);
List<Card> dealerCards = deck.dealCards(2);
List<List<Card>> initialList = new ArrayList<>();
initialList.add(playerCards);
initialList.add(dealerCards);
System.out.println("Dealer:");
Card visibleCard = dealerCards.get(0);
System.out.println(Game.getRank(visibleCard.getRank()) + " " + Game.getSuit(visibleCard.getSuit()));
System.out.println("##");
System.out.println("Player:");
for (Card card : playerCards) {
System.out.println(Game.getRank(card.getRank()) + " " + Game.getSuit(card.getSuit()));
}
return initialList;
}
public boolean playerLoop(List<List<Card>> initialList, Deck deck, boolean willHit, Player player) {
List<Card> playerCards = initialList.get(0);
boolean playerBust = false;
while (willHit) {
playerValue = 0;
int aceCounter = 0;
for (Card card : playerCards) {
playerValue += cardValues.get(card.getRank());
if (Game.getRank(card.getRank()).equals("Ace")) {
aceCounter++;
}
}
if (aceCounter > 0 && playerValue <= 11) {
playerValue += 10;
}
if (aceCounter > 0 && playerValue > 21) {
playerValue -= 10;
}
if (playerValue > 21) {
System.out.println(playerValue + ": you bust!");
dealerHit = false;
playerBust = true;
player.loseBet();
break;
}
if (playerValue == 21) {
System.out.println("21, that's Blackjack!");
break;
}
System.out.println("Player hand worth: " + playerValue);
System.out.println("Do you want to hit or stay?");
String result = scan.next();
if (result.toLowerCase().charAt(0) == 'h') {
List<Card> newCard = deck.dealCards(1);
Card hit = newCard.remove(0);
System.out.println(Game.getRank(hit.getRank()) + " " + Game.getSuit(hit.getSuit()));
playerCards.add(hit);
} else {
willHit = false;
}
}
return playerBust;
}
public boolean dealerLoop(List<List<Card>> initialList, Deck deck, boolean dealerHit, Player player, int bet) {
List<Card> dealerCards = initialList.get(1);
boolean dealerBust = false;
while (dealerHit) {
System.out.println("Dealer:");
for (Card card : dealerCards) {
System.out.println(Game.getRank(card.getRank()) + " " + Game.getSuit(card.getSuit()));
}
dealerValue = 0;
for (Card card : dealerCards) {
dealerValue += cardValues.get(card.getRank());
}
System.out.println("Dealer Value: " + dealerValue);
if (dealerValue < 17) {
List<Card> newCard = deck.dealCards(1);
Card hit = newCard.remove(0);
System.out.println(Game.getRank(hit.getRank()) + " " + Game.getSuit(hit.getSuit()));
dealerCards.add(hit);
} else if (dealerValue > 21) {
System.out.println(dealerValue + ": Dealer busts, you win!");
dealerBust = true;
player.winBet(bet);
break;
} else {
dealerHit = false;
}
}
return dealerBust;
}
public void winLossCheck(boolean playerBust, boolean dealerBust, int playerValue, int dealerValue, Player player, int bet) {
if (!playerBust && !dealerBust) {
if (dealerValue > playerValue) {
System.out.println(dealerValue + " is higher than " + playerValue + ". You lose!");
player.loseBet();
} else if (playerValue == 21 && playerValue > dealerValue) {
System.out.println("You win!");
player.blackjack(bet);
} else if (playerValue > dealerValue) {
System.out.println(playerValue + " is higher than " + dealerValue + ". You win!");
player.winBet(bet);
} else {
System.out.println(playerValue + " is the same as " + dealerValue + ". You push!");
player.pushBet(bet);
}
}
}
}