-
Notifications
You must be signed in to change notification settings - Fork 0
/
BlackJack.java
executable file
·85 lines (66 loc) · 2 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
/** Jacob Swanson
* 5/13/12
* BlackJack program
*/
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
public class BlackJack extends JPanel {
private static final long serialVersionUID = 1;
Game game = new Game();
Deck deck = new Deck();
Card card;
private JButton hit, stay, newGame;
public BlackJack() {
deck.build();
game.newGame(deck);
ButtonListener listener = new ButtonListener();
hit = new JButton("Hit");
hit.setActionCommand("hit");
hit.addActionListener(listener);
add(hit);
stay = new JButton("Stay");
stay.setActionCommand("stay");
stay.addActionListener(listener);
add(stay);
newGame = new JButton("New Game");
newGame.setActionCommand("newGame");
newGame.addActionListener(listener);
add(newGame);
}
public void paintComponent(Graphics pen) {
super.paintComponents(pen);
setBackground(Color.decode("#277714"));
pen.setFont(new Font("Arial", Font.BOLD, 20));
game.dealer.hand.drawHand(game.dealer.type, pen);
game.player.hand.drawHand(game.player.type, pen);
String message = game.checkScores(pen);
if (message != null) {
game.player.setAllowHit(false);
game.player.setAllowStay(false);
pen.setColor(Color.black);
pen.setFont(new Font("Arial", Font.BOLD, 40));
pen.drawString(message, 300, 215);
}
hit.setVisible(game.player.allowHit());
stay.setVisible(game.player.allowStay());
}
class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent buttonClick) {
String Click = buttonClick.getActionCommand();
if (Click.equals("hit")) {
game.player.hand.addCard(deck, true);
} else if (Click.equals("stay")) {
game.dealer.showHand(deck);
game.isOver = true;
} else if (Click.equals("newGame")) {
game.newGame(deck);
}
repaint();
}
}
}