-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBlackjackGUI.java
More file actions
340 lines (292 loc) · 10.9 KB
/
Copy pathBlackjackGUI.java
File metadata and controls
340 lines (292 loc) · 10.9 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
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class BlackjackGUI extends JApplet {
public void init() {
setContentPane( new BlackjackGUI() );
}
// sets up the button and the panel and the color
public BlackjackGUI() {
setBackground( new Color(130,50,40) );
setLayout( new BorderLayout(3,3) );
CardPanel board = new CardPanel();
add(board, BorderLayout.CENTER);
JPanel buttonPanel = new JPanel();
buttonPanel.setBackground( new Color(220,200,180) );
add(buttonPanel, BorderLayout.SOUTH);
JButton newGame = new JButton( "New Game" );
newGame.addActionListener(board);
buttonPanel.add(newGame);
JButton newBet = new JButton( "Bet $10" );
newBet.addActionListener(board);
buttonPanel.add(newBet);
JButton hitButton = new JButton( "Hit" );
hitButton.addActionListener(board);
buttonPanel.add(hitButton);
JButton standButton = new JButton( "Stand" );
standButton.addActionListener(board);
buttonPanel.add(standButton);
JButton doubleButton = new JButton("Double Down");
doubleButton.addActionListener(board);
buttonPanel.add(doubleButton);
}
// nested class for j panel
private class CardPanel extends JPanel implements ActionListener {
Deck deck;
blackJackHand dealerHand;
blackJackHand playerHand;
String message; // string to write messages
boolean gameInProgress; //true=started, false=new game
Font bigFont;
Font smallFont;
Card dFirst; // used to print back of the card for dealer
int noWinsPlayer = 0;
int noWinsDealer = 0;
int playerAmt = 100; // players pockets
int playerBet = 0;
boolean betYet = false; // If bet true else false
CardPanel() {
setPreferredSize( new Dimension(460,310) );
setBackground( new Color(0,120,0) );
smallFont = new Font("SansSerif", Font.PLAIN, 12);
bigFont = new Font("Serif", Font.BOLD, 16);
doNewGame();
}
// takes input from button pressed and runs the method
public void actionPerformed(ActionEvent evt) {
String command = evt.getActionCommand();
if (command.equals("Hit"))
doHit();
else if (command.equals("Stand"))
doStand();
else if(command.equals("New Game")){
resetBet();
repaint();
doNewGame();
}
else if (command.equals("Bet $10"))
doBet();
else if (command.equals("Double Down"))
doDoubleDown();
}
// button hit checks for bet and game in progress
void doHit() {
if (betYet == false) {
message = "Click\"Bet $10\" to begin play. ";
repaint();
return;
}
if (gameInProgress == false) {
message = "Click \"New Game\" to start a new game.";
repaint();
return;
}
playerHand.addCard( deck.dealCard() );
if ( playerHand.BlackjackValue() > 21 ) {
message = "You've busted! Sorry, you lose.";
gameInProgress = false;
noWinsDealer ++;
}
else if (playerHand.BlackjackValue() == 21) {
message = "You have " + playerHand.BlackjackValue();
repaint();
doStand();
return;
}
else {
message = "You have " + playerHand.BlackjackValue() + ". Hit or Stand?";
}
repaint();
}
// button bet checks for bet
void doBet(){
if (betYet == true) {
message= "You have already placed a bet. ";
repaint();
}
if (betYet == false) {
playerBet = 10;
if (playerBet > playerAmt){
message = "SORRY! You CANNOT double down because you don't have enough money.";
repaint();
return;
}
betYet = true;
playerAmt -= playerBet;
doNewGame();
}
}
// button Double down checks for bet and game in progress
void doDoubleDown(){
if (betYet == false) {
message = "You must place a bet first. Click\"Bet $10\" to begin play.";
repaint();
return;
}
if (gameInProgress == false) {
message = "Click\"Deal\"to start a new game.";
repaint();
return;
}
//if bet is > than Amt - no bet
if (playerBet > playerAmt){
message = "SORRY! You CANNOT double down because you don't have enough money.";
repaint();
return;
}
playerAmt = playerAmt - playerBet;
playerBet = playerBet *2;
playerHand.addCard(deck.dealCard());
if ( playerHand.BlackjackValue() > 21) {
message = "You've busted! Sorry, you lose";
gameInProgress = false;
noWinsDealer ++;
repaint();
return;
}
else if (playerHand.BlackjackValue() == 21) {
message = "You have " + playerHand.BlackjackValue();
repaint();
return;
}else{
message = "You have " + playerHand.BlackjackValue();
}
repaint();
doStand();
}
// button Stand checks for bet and game in progress
//plays the dealer hand. If hand <= 16, have to take hit
//if dealer hand greater than or = 17 have to stand.
void doStand() {
if (betYet==false) {
message = "Click\"Bet $10\" to begin play.";
repaint();
return;
}
if (gameInProgress == false) {
message = "Click \"New Game\" to start a new game.";
repaint();
return;
}
gameInProgress = false;
while (dealerHand.BlackjackValue() <= 16)
dealerHand.addCard( deck.dealCard() );
if (dealerHand.BlackjackValue() > 21){
message = "You win! Dealer has busted with " + dealerHand.BlackjackValue() + ".";
noWinsPlayer ++;
playerAmt += 2*playerBet;
}
else if (dealerHand.BlackjackValue() > playerHand.BlackjackValue()){
message = "Sorry, you lose, " + dealerHand.BlackjackValue()
+ " to " + playerHand.BlackjackValue() + ".";
noWinsDealer ++;
}
else if (dealerHand.BlackjackValue() == playerHand.BlackjackValue()){
message = "Sorry, you lose. Dealer wins on a tie.";
noWinsDealer ++;
}
else{
message = "You win, " + playerHand.BlackjackValue()
+ " to " + dealerHand.BlackjackValue() + "!";
noWinsPlayer ++;
playerAmt += 2*playerBet;
}
repaint();
}
// resets the playerBet and bet boolean chek
void resetBet(){
betYet = false;
playerBet = 0;
}
//
// Called when button "New Game" pressed and player has bet. If player hasn't bet,
// returns with message to place bet. Uses new deck and sets up new cards. Start a new game. Deal two cards
// Sets gameInProgress is set to true .
// begins.
//
void doNewGame() {
if (gameInProgress) {
message = "You still have to finish this game!";
repaint();
return;
}
if (betYet==false) {
message = "Click\"Bet $10\" to begin play.";
repaint();
return;
}
deck = new Deck(); // Create the deck and hands to use for this game.
dealerHand = new blackJackHand();
playerHand = new blackJackHand();
dealerHand.handClear();
playerHand.handClear();
deck.shuffle();
dealerHand.addCard( deck.dealCard() ); // Deal two cards to each player.
dealerHand.addCard( deck.dealCard() );
playerHand.addCard( deck.dealCard() );
playerHand.addCard( deck.dealCard() );
if (dealerHand.BlackjackValue() == 21) {
message = "Sorry, you lose. Dealer has Blackjack.";
gameInProgress = false;
noWinsDealer ++;
}
else if (playerHand.BlackjackValue() == 21) {
message = "You win! You have Blackjack.";
gameInProgress = false;
noWinsPlayer ++;
playerAmt += 2*playerBet;
}
else {
message = "You have " + playerHand.BlackjackValue() + ". Hit or stand?";
gameInProgress = true;
}
repaint();
}
// The paint method shows the message at the bottom of the
// canvas, and it draws all of the dealt cards spread out
// across the canvas.
public void paintComponent(Graphics g) {
super.paintComponent(g); // fill with background color.
g.setFont(bigFont);
g.setColor(Color.GREEN);
g.drawString(message, 10, getHeight() - 10);
g.drawString(("Player amount : $" + playerAmt), 10, getSize().height-90);
g.drawString(("Player Score: " + noWinsPlayer + " Dealer Score: " + noWinsDealer), 10, getSize().height-50);
// Title for dealer and player hands.
g.drawString("Dealer's Cards:", 10, 23);
g.drawString("Your Cards:", 10, 153);
// Draw cards, if betYet is true. If false, don't draw.
// Dealer first card is down
g.setFont(smallFont);
if(betYet){
g.drawString(("You've bet " + playerBet),10, getSize().height-70);
if (gameInProgress)
drawCard(g, null, 10, 30);
else
drawCard(g, dealerHand.getCard(0), 10, 30);
for (int i = 1; i < dealerHand.getCardCount(); i++)
drawCard(g, dealerHand.getCard(i), 10 + i * 90, 30);
// Draw the user's cards.
for (int i = 0; i < playerHand.getCardCount(); i++)
drawCard(g, playerHand.getCard(i), 10 + i * 90, 160);
} else if (betYet == false) g.drawString((""),10, getSize().height-70);
} // end paint();
// drawsCard image
void drawCard(Graphics g, Card card, int x, int y) {
// Draws a card about 70 by 90...
// first it shows back for dealer hand
//else shows card for dealer and player
String temp;
if (card == null) {
dFirst=dealerHand.getCard(0);
dFirst.cardImage("");
dFirst.drawblackJackCard(g, new Rectangle(x+4,y+4,71,91));
}
else {
temp=card.getCardImageName();
card.cardImage(temp);
card.drawblackJackCard(g, new Rectangle(x+1, y+1, 77, 97));
}
} // end drawCard()
}
}