-
Notifications
You must be signed in to change notification settings - Fork 0
/
BlackjackGUI.java
321 lines (252 loc) · 12.3 KB
/
BlackjackGUI.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
313
314
315
316
317
318
319
320
321
//layout for Blackjack game
//last modified: 06/14
//layout
import javax.swing.*;
import java.awt.*;
import java.util.LinkedList;
public class BlackjackGUI extends JPanel {
// variable declaration
private BlackjackGame game;
private JButton hit = new JButton("Hit");
private JButton stand = new JButton("Stand");
private JButton newGame = new JButton("New Game");
private JLabel score = new JLabel("Score:");
private JLabel compScore = new JLabel("Computer - " + BlackjackGame.getCompScore());
private JLabel playerScore = new JLabel("Player - " + BlackjackGame.getPlayerScore());
private JLabel computer = new JLabel("Computer");
private JLabel player = new JLabel("Player");
private JLabel compSuit = new JLabel();
private JLabel compNum = new JLabel();
private JLabel compSuit2 = new JLabel();
private JLabel compNum2 = new JLabel();
private JLabel playerSuit = new JLabel();
private JLabel playerNum = new JLabel();
private JLabel playerSuit2 = new JLabel();
private JLabel playerNum2 = new JLabel();
private JLabel deck = new JLabel();
private JLabel playerSum = new JLabel("Sum:");
private JLabel compSum = new JLabel("Sum:");
private JLabel display = new JLabel();
private JLabel white = new JLabel();
private JLabel white2 = new JLabel();
private JLabel white3 = new JLabel();
private JLabel white4 = new JLabel();
private LayoutFrame layoutFrame;
private Card[] dealt;
// private LinkedList<Card> playerQueue;
// private LinkedList<Card> compQueue;
public BlackjackGUI() {
super();
this.setUp();
this.layoutFrame = new LayoutFrame("Blackjack", this);
// this.update();
}
// main testing method
public static void main(String[] args) throws InterruptedException {
new BlackjackGUI();
// BlackjackGame game = new BlackjackGame();
}
public void startNew() {
this.layoutFrame.dispose();
new BlackjackGUI();
}
public void layoutView() {
// set up buttons and labels
SpringLayout layout = new SpringLayout();
this.setLayout(layout);
this.add(this.hit);
this.add(this.stand);
this.add(this.newGame);
this.add(this.score);
this.add(this.compScore);
this.add(this.playerScore);
//
this.add(this.playerNum2);
this.add(this.playerSuit2);
this.add(this.white4);
this.add(this.compNum);
this.add(this.compSuit);
this.add(this.white3);
this.add(this.compSuit2);
this.add(this.compNum2);
this.add(this.playerNum);
this.add(this.playerSuit);
this.add(this.computer);
this.add(this.player);
this.add(this.deck);
this.add(this.playerSum);
this.add(this.compSum);
this.add(this.display);
this.add(this.white);
this.add(this.white2);
// add constraints
hit.setPreferredSize(new Dimension(100, 60));
hit.setFont(new Font("Arial", Font.BOLD, 22));
layout.putConstraint(SpringLayout.NORTH, hit, 470, SpringLayout.NORTH, this);
layout.putConstraint(SpringLayout.WEST, hit, 680, SpringLayout.WEST, this);
stand.setPreferredSize(new Dimension(100, 60));
stand.setFont(new Font("Arial", Font.BOLD, 22));
layout.putConstraint(SpringLayout.NORTH, stand, 380, SpringLayout.NORTH, this);
layout.putConstraint(SpringLayout.WEST, stand, 680, SpringLayout.WEST, this);
newGame.setPreferredSize(new Dimension(200, 60));
newGame.setFont(new Font("Arial", Font.BOLD, 22));
layout.putConstraint(SpringLayout.NORTH, newGame, 550, SpringLayout.NORTH, this);
layout.putConstraint(SpringLayout.WEST, newGame, 640, SpringLayout.WEST, this);
computer.setFont(new Font("Arial", Font.BOLD, 22));
layout.putConstraint(SpringLayout.WEST, computer, 75, SpringLayout.WEST, this);
layout.putConstraint(SpringLayout.NORTH, computer, 10, SpringLayout.NORTH, this);
player.setFont(new Font("Arial", Font.BOLD, 22));
layout.putConstraint(SpringLayout.WEST, player, 375, SpringLayout.WEST, this);
layout.putConstraint(SpringLayout.NORTH, player, 10, SpringLayout.NORTH, this);
layout.putConstraint(SpringLayout.WEST, deck, 600, SpringLayout.WEST, this);
layout.putConstraint(SpringLayout.NORTH, deck, 40, SpringLayout.NORTH, this);
layout.putConstraint(SpringLayout.WEST, playerSuit, 320, SpringLayout.WEST, this);
layout.putConstraint(SpringLayout.NORTH, playerSuit, 80, SpringLayout.NORTH, this);
layout.putConstraint(SpringLayout.WEST, playerNum, 320, SpringLayout.WEST, this);
layout.putConstraint(SpringLayout.NORTH, playerNum, 40, SpringLayout.NORTH, this);
layout.putConstraint(SpringLayout.WEST, playerSuit2, 320, SpringLayout.WEST, this);
layout.putConstraint(SpringLayout.NORTH, playerSuit2, 180, SpringLayout.NORTH, this);
layout.putConstraint(SpringLayout.WEST, playerNum2, 320, SpringLayout.WEST, this);
layout.putConstraint(SpringLayout.NORTH, playerNum2, 140, SpringLayout.NORTH, this);
layout.putConstraint(SpringLayout.WEST, white, 320, SpringLayout.WEST, this);
layout.putConstraint(SpringLayout.NORTH, white, 40, SpringLayout.NORTH, this);
layout.putConstraint(SpringLayout.WEST, white2, 40, SpringLayout.WEST, this);
layout.putConstraint(SpringLayout.NORTH, white2, 40, SpringLayout.NORTH, this);
layout.putConstraint(SpringLayout.WEST, white3, 40, SpringLayout.WEST, this);
layout.putConstraint(SpringLayout.NORTH, white3, 150, SpringLayout.NORTH, this);
layout.putConstraint(SpringLayout.WEST, white4, 320, SpringLayout.WEST, this);
layout.putConstraint(SpringLayout.NORTH, white4, 150, SpringLayout.NORTH, this);
layout.putConstraint(SpringLayout.WEST, compSuit2, 40, SpringLayout.WEST, this);
layout.putConstraint(SpringLayout.NORTH, compSuit2, 80, SpringLayout.NORTH, this);
layout.putConstraint(SpringLayout.WEST, compSuit, 40, SpringLayout.WEST, this);
layout.putConstraint(SpringLayout.NORTH, compSuit, 180, SpringLayout.NORTH, this);
layout.putConstraint(SpringLayout.WEST, compNum2, 40, SpringLayout.WEST, this);
layout.putConstraint(SpringLayout.NORTH, compNum2, 40, SpringLayout.NORTH, this);
layout.putConstraint(SpringLayout.WEST, compNum, 40, SpringLayout.WEST, this);
layout.putConstraint(SpringLayout.NORTH, compNum, 140, SpringLayout.NORTH, this);
playerSum.setFont(new Font("Arial", Font.BOLD, 22));
layout.putConstraint(SpringLayout.WEST, playerSum, 330, SpringLayout.WEST, this);
layout.putConstraint(SpringLayout.NORTH, playerSum, 470, SpringLayout.NORTH, this);
compSum.setFont(new Font("Arial", Font.BOLD, 22));
layout.putConstraint(SpringLayout.WEST, compSum, 50, SpringLayout.WEST, this);
layout.putConstraint(SpringLayout.NORTH, compSum, 470, SpringLayout.NORTH, this);
display.setFont(new Font("Arial", Font.BOLD, 30));
layout.putConstraint(SpringLayout.WEST, display, 40, SpringLayout.WEST, this);
layout.putConstraint(SpringLayout.NORTH, display, 590, SpringLayout.NORTH, this);
score.setFont(new Font("Arial", Font.BOLD, 25));
layout.putConstraint(SpringLayout.WEST, score, 40, SpringLayout.WEST, this);
layout.putConstraint(SpringLayout.NORTH, score, 532, SpringLayout.NORTH, this);
compScore.setFont(new Font("Arial", Font.BOLD, 25));// hi there
layout.putConstraint(SpringLayout.WEST, compScore, 170, SpringLayout.WEST, this);
layout.putConstraint(SpringLayout.NORTH, compScore, 532, SpringLayout.NORTH, this);
playerScore.setFont(new Font("Arial", Font.BOLD, 25)); // hi there
layout.putConstraint(SpringLayout.WEST, playerScore, 380, SpringLayout.WEST, this);
layout.putConstraint(SpringLayout.NORTH, playerScore, 532, SpringLayout.NORTH, this);
deck.setIcon(new ImageIcon(new javax.swing.ImageIcon(getClass().getResource("back.jpg"))
.getImage().getScaledInstance(250, 330, Image.SCALE_SMOOTH)));
white.setIcon(new ImageIcon(new javax.swing.ImageIcon(getClass().getResource("white.png"))
.getImage().getScaledInstance(250, 42, Image.SCALE_SMOOTH)));
white2.setIcon(new ImageIcon(new javax.swing.ImageIcon(getClass().getResource("white.png"))
.getImage().getScaledInstance(250, 42, Image.SCALE_SMOOTH)));
white3.setIcon(new ImageIcon(new javax.swing.ImageIcon(getClass().getResource("white.png"))
.getImage().getScaledInstance(250, 42, Image.SCALE_SMOOTH)));
white4.setIcon(new ImageIcon(new javax.swing.ImageIcon(getClass().getResource("white.png"))
.getImage().getScaledInstance(250, 42, Image.SCALE_SMOOTH)));
}
public void displayCardComp(Card card1, Card card2) {
int num1 = card1.getNum();
String suit1 = card1.getSuit();
int num2 = card2.getNum();
String suit2 = card2.getSuit();
compSuit.setIcon(new ImageIcon(new javax.swing.ImageIcon(getClass().getResource(suit1 + ".png"))
.getImage().getScaledInstance(250, 280, Image.SCALE_SMOOTH)));
compNum.setIcon(new ImageIcon(new javax.swing.ImageIcon(getClass().getResource(num1 + ".png"))
.getImage().getScaledInstance(50, 50, Image.SCALE_SMOOTH)));
compSuit2.setIcon(new ImageIcon(new javax.swing.ImageIcon(getClass().getResource(suit2 + ".png"))
.getImage().getScaledInstance(250, 280, Image.SCALE_SMOOTH)));
compNum2.setIcon(new ImageIcon(new javax.swing.ImageIcon(getClass().getResource(num2 + ".png"))
.getImage().getScaledInstance(50, 50, Image.SCALE_SMOOTH)));
}
public void displayCardPlayer(Card card1, Card card2) {
int num1 = card1.getNum();
String suit1 = card1.getSuit();
int num2 = card2.getNum();
String suit2 = card2.getSuit();
playerSuit.setIcon(new ImageIcon(new javax.swing.ImageIcon(getClass().getResource(suit1 + ".png"))
.getImage().getScaledInstance(250, 280, Image.SCALE_SMOOTH)));
playerNum.setIcon(new ImageIcon(new javax.swing.ImageIcon(getClass().getResource(num1 + ".png"))
.getImage().getScaledInstance(47, 47, Image.SCALE_SMOOTH)));
playerSuit2.setIcon(new ImageIcon(new javax.swing.ImageIcon(getClass().getResource(suit2 + ".png"))
.getImage().getScaledInstance(250, 280, Image.SCALE_SMOOTH)));
playerNum2.setIcon(new ImageIcon(new javax.swing.ImageIcon(getClass().getResource(num2 + ".png"))
.getImage().getScaledInstance(47, 47, Image.SCALE_SMOOTH)));
}
public void updateText(int num) {
switch (num) {
/*
* case 1:
* display.setText("It is now the player's turn");
* break;
*/
case 2:
display.setText("It is now the computer's turn");
break;
case 3:
display.setText("Choose to hit or stand");
break;
case 4:
display.setText("You won!");
break;
case 5:
display.setText("You lost...");
break;
/*
* case 6:
* display.setText("Play again?");
* break;
*/
}
}
private void registerControllers() {
HitController hitController = new HitController(this.game, this.hit);
this.hit.addActionListener(hitController);
StandController standController = new StandController(this.game, this.stand);
this.stand.addActionListener(standController);
NewGameController newGameController = new NewGameController(this, this.newGame);
this.newGame.addActionListener(newGameController);
}
public void updateScore() {
playerScore.setText("Player - " + game.getPlayerScore());
compScore.setText("Computer - " + game.getCompScore());
}
public void update() {
playerSum.setText("Sum:" + String.valueOf((game.getPlayerSum())));
compSum.setText("Sum:" + String.valueOf((game.getCompSum())));
if (game.getTurn() == "player") {
displayCardPlayer(game.getPlayerQueue().poll(), game.getPlayerQueue().peek());
game.endTurn();
} else if (game.getTurn() == "comp") {
this.updateText(2);
game.compTurn();
// System.out.println(game.getTurn());
this.update();
} else if (game.getTurn() == "end") {
game.endTurn();
}
}
public void setUp() {
game = new BlackjackGame();
game.setGUI(this);
this.layoutView();
this.registerControllers();
this.updateText(3);
dealt = game.getDealt();
// playerQueue = game.getPlayerQueue();
// compQueue = game.getCompQueue();
playerSum.setText("Sum:" + String.valueOf((game.getPlayerSum())));
compSum.setText("Sum:" + String.valueOf((game.getCompSum())));
displayCardPlayer(game.getPlayerQueue().poll(), game.getPlayerQueue().peek());
displayCardComp(game.getCompQueue().poll(), game.getCompQueue().peek());
game.endTurn();
}
}