/
BlackJack.java
125 lines (122 loc) · 4.66 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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package blackjack;
/**
*
* @author ConnorReilly
*/
public class BlackJack {
private static final int BUFFER_SIZE = 1000;
private static final int BUST_LIMIT = Dealer.BUST_LIMIT;
private static final char PLAY = 'p';
private static final char QUIT = 'q';
private static final char HIT = '1';
private static final char STAND = '2';
private static final char NL = '\n';
private static final String DRAW = "It's a push!";
private static final String PLAYER_WIN = "Player wins!";
private static final String DEALER_WIN = "Dealer wins!";
private static final String BAD_INPUT = "Please enter a valid character.";
private static final String BUSTED = "Busted!";
private static final String DEALER_BUSTED = "Dealer busted!";
private static final String WELCOME = "Welcome to Connor's BlackJack game!";
private static final String BREAK = WELCOME.replaceAll(".", "-");
private static final String SCORE = "Score:";
private static final String PLAYER_HAND = "Player hand:";
private static final String DEALER_HAND = "Dealer hand:";
private static final String MAIN_MENU_TITLE = "Main Menu:";
private static final String MAIN_MENU_OPTIONS = "Enter " + PLAY + " to play"
+ NL + "Enter " + QUIT + " to quit";
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
byte[] b;
char c = ' ';
System.out.println(BREAK + NL + WELCOME + NL + BREAK + NL);
do {
b = new byte[BUFFER_SIZE];
System.out.println(MAIN_MENU_TITLE + NL + MAIN_MENU_OPTIONS);
try {
System.in.read(b, 0, 1);
c = (char) b[0];
System.out.println(System.in.available());
switch (c) {
case PLAY:
System.out.println(NL + BREAK + NL);
play();
break;
case QUIT:
break;
default:
System.out.println(BAD_INPUT);
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
} while (c != QUIT);
}
private static void play() {
Dealer dealer = new Dealer();
Hand hand = new Hand();
byte[] b;
dealer.shuffleDeck();
hand.add(dealer.hit());
hand.add(dealer.hit());
System.out.println(PLAYER_HAND + NL + hand.toString());
do {
System.out.println(SCORE + ' ' + hand.getScore() + NL);
System.out.println(HIT + ": hit" + NL + STAND + ": stand");
b = new byte[BUFFER_SIZE];
try {
System.in.read(b);
System.out.println(NL + BREAK);
switch ((char) b[0]) {
case HIT:
System.out.println();
hand.add(dealer.hit());
System.out.println(PLAYER_HAND + NL + hand.toString());
break;
case STAND:
break;
default:
System.out.println(BAD_INPUT);
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
} while (b[0] != (int) STAND && hand.getScore() <= BUST_LIMIT);
boolean playerBust = hand.getScore() > BUST_LIMIT;
if (playerBust) {
System.out.println(SCORE + ' ' + hand.getScore());
System.out.println(BUSTED + NL);
} else
System.out.println();
boolean dealerBust = dealer.play();
System.out.println(DEALER_HAND + NL + dealer.handToString());
System.out.println(SCORE + ' ' + dealer.scoreHand());
if (dealerBust) {
System.out.println(DEALER_BUSTED + NL);
if (playerBust)
System.out.println(DRAW);
else
System.out.println(PLAYER_WIN);
}
else if (playerBust) {
System.out.println(NL + DEALER_WIN);
}
else {
System.out.println();
if (hand.getScore() > dealer.scoreHand())
System.out.println(PLAYER_WIN);
else if (hand.getScore() < dealer.scoreHand())
System.out.println(DEALER_WIN);
else
System.out.println(DRAW);
}
System.out.println();
}
}