-
Notifications
You must be signed in to change notification settings - Fork 0
/
Blackjack.java
312 lines (280 loc) · 10.5 KB
/
Blackjack.java
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
import java.util.Scanner;
import java.util.InputMismatchException;
import java.util.Random;
import java.util.ArrayList;
/**
* Blackjack.java: A simple blackjack game.
* <p>
* <b>Discription</b>:<br /b> BlackJack is a card game (52 cards) where
* the player tries to beat the dealer. Players place a bet and are
* given a card face up. The dealer recives a card face up and then the
* player is given thier second card face up. The dealer than recives their
* second card face down. The player than has the option to stay or hit. Staying
* ends the players turn. Hitting gives the player another card. The player is
* trying to get a total card value closer to 21 than the dealer. If the player or
* dealer has a hand value of over 21 they bust and lose the game. If the get 21
* aka blackjack they instantly win the game. If they have the same hand value
* the game is a tie and no one wins or loses thier bet. If the player wins thier bet
* is multipled by 2x, 2.5x if they hit blackjack. If the dealer wins the player loses
* thier bet and it goes to the dealer (house).
* </p>
*
* <p>
* <b>Problem Statement</b>:<br /b> Create a text based blackajck game in the console.
* </p>
*
* <b>Algorithm</b>:
* <ol>
* <li> Create a Card Class containing all 52 cards. All cards have a value and suit. </li>
* <li> Create a Player Class. All players have a Name and Initial Money amount. </li>
* <li> Create a Dealer Class. This class will have dealer specific methods such as a Shuffle Deck Method. </li>
* <li> The Program will have a Main game loop and sub loops. The Main game loop will have the overall game
* while the sub loops will be specific to the player and dealer. </li>
* <li> The Main game loop will break when the player enters No/N when asked if they wish to keep playing or when
* the player runs out of money. </li>
* <li> The Player sub loop will end when the player busts, stays, or hits blackjack. </li>
* <li> The dealer sub loop will start if the player stays or hits blackjack. It will end when the dealer hits 17 or over. </li>
* <li> During the sub loops is when a winner or tie will be found. Depending on the result the Players money amount will be updated. </li>
* <li> The Player will then be asked if they wish to either cash out (end program) or keep playing (loop back). </li>
* </ol>
*
* @author Adolfo Sanpedro Gante
* @version 1.0
*/
public class Blackjack {
//Constants
public static final int MIN_BET = 5;
public static final int MAX_BET = 200;
//Instance variables
public int betAmount = 0;
public static int cardNum = 0;
public static Player player = new Player();
public static Random rand = new Random();
public static ArrayList <Card> deck = new ArrayList <Card>(52);
public static ArrayList <Integer> shuffle = new ArrayList <Integer>();
public static ArrayList <Card> playerHand = new ArrayList <Card>(11);
public static ArrayList <Card> dealerHand = new ArrayList <Card>(11);
public static void main(String[] args) {
//Creates a 52 card deck
createDeck();
//shuffles the deck
shuffleDeck();
//Loops untill user enters a proper name
while (true) {
try {
//Asks for players Name
askPlayerName();
//Ends Loop
break;
} catch (CharacterLimitException e) {
System.out.println(e.getMessage());
}
}
//Loops untill user enter a proper money amount
while (true) {
//Trys to get proper input from user
try {
//Asks for players money amount
askPlayerMoney();
//Ends Loop
break;
}
catch (InputMismatchException e) {
System.out.println("The money amount you entered must be a whole integer number. Try again.");
}
catch (MoneyInputException e) {
System.out.println(e.getMessage());
}
}
//Loops untill user enter a proper bet amount
while (true) {
//Trys to get proper input from user
try {
//Asks for players bet amount
askBetAmount();
//Ends Loop
break;
}
catch (InputMismatchException e) {
System.out.println("The money amount you entered must be a whole integer number. Try again.");
}
catch (MoneyInputException e) {
System.out.println(e.getMessage());
}
}
dealCards();
printTable();
//Loops untill player turn ends
while (true) {
try {
//Asks player for either hit or bust
if (playerMove()) {
break;
}
printTable();
} catch (IllegalMoveException e) {
System.out.println(e.getMessage());
}
}
}
//Helper methods
public static void askPlayerName() throws CharacterLimitException{
//Creates a scanner to get user input
Scanner userInput = new Scanner(System.in);
//Asks player for thier name.
System.out.print("Enter your name: ");
String name = userInput.next();
//Checks name length
if (name.length() > 15) {
throw new CharacterLimitException();
}
else {
//Sets players name.
player.setName(name);
}
}
public static void askPlayerMoney() throws MoneyInputException {
//Creates a scanner to get user input
Scanner userInput2 = new Scanner(System.in);
//Asks user for thier inital amount of money
System.out.print("Enter your amount of money: ");
int money = userInput2.nextInt();
//Checks for valid user input
if (money <= 0) {
throw new MoneyInputException();
}
//Sets players money amount
player.setMoney(money);
}
public static void askBetAmount() throws MoneyInputException {
//Creates a scanner to get user input
Scanner userInput3 = new Scanner(System.in);
//Asks player for thier name.
System.out.println("Min: $" + MIN_BET + ", Max: $" + MAX_BET);
System.out.print("Enter your bet amount in increments of $5: ");
int betAmount = userInput3.nextInt();
//Checks for valid user input
if ((betAmount % 5) != 0) {
throw new MoneyInputException(betAmount);
}
if (betAmount < MIN_BET || betAmount > MAX_BET) {
throw new MoneyInputException(betAmount);
}
}
public static void createDeck() {
String suit = "";
for (int i = 0; i < 4; i++) {
switch (i){
case 0: suit = "H";
deck.add (new Card(1, suit, "One"));
deck.add (new Card(2, suit, "Two"));
deck.add (new Card(3, suit, "Three"));
deck.add (new Card(4, suit, "Four"));
deck.add (new Card(5, suit, "Five"));
deck.add (new Card(6, suit, "Six"));
deck.add (new Card(7, suit, "Seven"));
deck.add (new Card(8, suit, "Eight"));
deck.add (new Card(9, suit, "Nine"));
deck.add (new Card(10, suit, "Ten"));
deck.add (new Card(10, suit, "Jack"));
deck.add (new Card(10, suit, "King"));
deck.add (new Card(10, suit, "Queen"));
break;
case 1: suit = "D";
deck.add (new Card(1, suit, "One"));
deck.add (new Card(2, suit, "Two"));
deck.add (new Card(3, suit, "Three"));
deck.add (new Card(4, suit, "Four"));
deck.add (new Card(5, suit, "Five"));
deck.add (new Card(6, suit, "Six"));
deck.add (new Card(7, suit, "Seven"));
deck.add (new Card(8, suit, "Eight"));
deck.add (new Card(9, suit, "Nine"));
deck.add (new Card(10, suit, "Ten"));
deck.add (new Card(10, suit, "Jack"));
deck.add (new Card(10, suit, "King"));
deck.add (new Card(10, suit, "Queen"));
break;
case 2: suit = "C";
deck.add (new Card(1, suit, "One"));
deck.add (new Card(2, suit, "Two"));
deck.add (new Card(3, suit, "Three"));
deck.add (new Card(4, suit, "Four"));
deck.add (new Card(5, suit, "Five"));
deck.add (new Card(6, suit, "Six"));
deck.add (new Card(7, suit, "Seven"));
deck.add (new Card(8, suit, "Eight"));
deck.add (new Card(9, suit, "Nine"));
deck.add (new Card(10, suit, "Ten"));
deck.add (new Card(10, suit, "Jack"));
deck.add (new Card(10, suit, "King"));
deck.add (new Card(10, suit, "Queen"));
break;
case 3: suit = "S";
deck.add (new Card(1, suit, "One"));
deck.add (new Card(2, suit, "Two"));
deck.add (new Card(3, suit, "Three"));
deck.add (new Card(4, suit, "Four"));
deck.add (new Card(5, suit, "Five"));
deck.add (new Card(6, suit, "Six"));
deck.add (new Card(7, suit, "Seven"));
deck.add (new Card(8, suit, "Eight"));
deck.add (new Card(9, suit, "Nine"));
deck.add (new Card(10, suit, "Ten"));
deck.add (new Card(10, suit, "Jack"));
deck.add (new Card(10, suit, "King"));
deck.add (new Card(10, suit, "Queen"));
break;
default:
break;
}
}
}
public static void shuffleDeck() {
while(shuffle.size() < 51) {
int deckSpot = rand.nextInt(52);
if (!shuffle.contains(deckSpot)) {
shuffle.add(deckSpot);
deck.add(deck.get(deckSpot));
deck.remove(deckSpot);
}
}
}
public static boolean playerMove() throws IllegalMoveException {
//Creates a scanner to get user input
Scanner userInput5 = new Scanner(System.in);
//Asks player for thier name.
System.out.print("Enter Hit or Stay: ");
String move = userInput5.next();
move = move.toUpperCase();
if (move.equals("STAY")) {
return (true);
}
else if (move.equals("HIT")) {
playerHand.add(deck.get(0));
deck.remove(0);
return (false);
}
else {
throw new IllegalMoveException(move);
}
}
public static void printTable() {
System.out.println();
System.out.println("Dealer's Hand ------ Player's Hand");
System.out.println("| " + dealerHand.get(cardNum).getName() + ", " + dealerHand.get(cardNum).getSuit() +
" | | " + playerHand.get(cardNum).getName() + ", " + playerHand.get(cardNum).getSuit() + " |");
System.out.println("----------------------------------");
cardNum++;
}
public static void dealCards() {
playerHand.add(deck.get(0));
deck.remove(0);
dealerHand.add(deck.get(0));
deck.remove(0);
playerHand.add(deck.get(0));
deck.remove(0);
dealerHand.add(deck.get(0));
deck.remove(0);
}
}