-
Notifications
You must be signed in to change notification settings - Fork 0
/
CardGameGUI.java
616 lines (518 loc) · 21.5 KB
/
CardGameGUI.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
/**
* Class for the Card Game Graphical User Interface
*
* @author Kuan Jia Hong
*
*/
public class CardGameGUI {
// Swing components
JFrame frame;
JLabel dealerCard1, dealerCard2, dealerCard3;
JLabel playerCard1, playerCard2, playerCard3;
JButton btnReplaceCard1, btnReplaceCard2, btnReplaceCard3;
JButton startButton, resultButton;
JLabel betLabel, infoLabel, moneyLabel;
JTextField inputBetText;
ImageIcon dealerCardImage1, dealerCardImage2, dealerCardImage3;
ImageIcon playerCardImage1, playerCardImage2, playerCardImage3;
JPanel MainPanel, DealerPanel, PlayerPanel;
JPanel ReplaceCardPanel, ButtonPanel, InfoPanel;
JMenuBar menuBar;
JMenu menu;
JMenuItem menuItem;
// variable to represent the game state
int budget = 100;
int betAmount = 0;
int replaceCount = 0;
ArrayList<String> playerCards = new ArrayList<>();
ArrayList<String> dealerCards = new ArrayList<>();
ArrayList<String> deckOfCards = new ArrayList<>();
public static void main(String[] args) {
CardGameGUI gui = new CardGameGUI();
gui.run();
}
/**
* Create all the components for the card game
*/
public void createComponent() {
dealerCard1 = new JLabel();
dealerCard2 = new JLabel();
dealerCard3 = new JLabel();
playerCard1 = new JLabel();
playerCard2 = new JLabel();
playerCard3 = new JLabel();
btnReplaceCard1 = new JButton("Replace Card 1");
btnReplaceCard2 = new JButton("Replace Card 2");
btnReplaceCard3 = new JButton("Replace Card 3");
startButton = new JButton("Start");
resultButton = new JButton("Result");
betLabel = new JLabel("Bet: $");
infoLabel = new JLabel();
moneyLabel = new JLabel();
inputBetText = new JTextField(10);
hideCard();
MainPanel = new JPanel();
DealerPanel = new JPanel();
PlayerPanel = new JPanel();
ReplaceCardPanel = new JPanel();
ButtonPanel = new JPanel();
InfoPanel = new JPanel();
JLabel dealerTitle = new JLabel("Dealer");
DealerPanel.add(dealerTitle);
DealerPanel.add(dealerCard1);
DealerPanel.add(dealerCard2);
DealerPanel.add(dealerCard3);
JLabel playerTitle = new JLabel("Player");
PlayerPanel.add(playerTitle);
PlayerPanel.add(playerCard1);
PlayerPanel.add(playerCard2);
PlayerPanel.add(playerCard3);
ReplaceCardPanel.add(btnReplaceCard1);
ReplaceCardPanel.add(btnReplaceCard2);
ReplaceCardPanel.add(btnReplaceCard3);
ButtonPanel.add(betLabel);
ButtonPanel.add(inputBetText);
ButtonPanel.add(startButton);
ButtonPanel.add(resultButton);
InfoPanel.add(infoLabel);
InfoPanel.add(moneyLabel);
MainPanel.setLayout(new GridLayout(5,1));
MainPanel.add(DealerPanel);
MainPanel.add(PlayerPanel);
MainPanel.add(ReplaceCardPanel);
MainPanel.add(ButtonPanel);
MainPanel.add(InfoPanel);
// Setting the background color
DealerPanel.setBackground(Color.green);
PlayerPanel.setBackground(Color.orange);
ReplaceCardPanel.setBackground(Color.cyan);
ButtonPanel.setBackground(Color.magenta);
InfoPanel.setBackground(Color.pink);
// Setting up the menu bar
menuBar = new JMenuBar();
menu = new JMenu("Control");
menuItem = new JMenuItem("Exit");
menu.add(menuItem);
menuBar.add(menu);
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setJMenuBar(menuBar);
frame.getContentPane().add(MainPanel);
frame.setTitle("A Simple Card Game");
frame.setSize(400,700);
frame.setVisible(true);
// result button is disabled in the beginning
resultButton.setEnabled(false);
btnReplaceCard1.setEnabled(false);
btnReplaceCard2.setEnabled(false);
btnReplaceCard3.setEnabled(false);
// Implement event listener
menuItem.addActionListener(new MenuItemListener());
startButton.addActionListener(new StartButtonListener());
resultButton.addActionListener(new ResultButtonListener());
btnReplaceCard1.addActionListener(new ButtonReplaceCard1());
btnReplaceCard2.addActionListener(new ButtonReplaceCard2());
btnReplaceCard3.addActionListener(new ButtonReplaceCard3());
}
/**
* Event handler for the menu item.
*/
class MenuItemListener implements ActionListener {
/**
* Exit the system user click on the menu item
* @param event mouseclick on menu item
*/
public void actionPerformed(ActionEvent event) {
System.exit(0);
}
}
/**
* Event handler for start button
*/
class StartButtonListener implements ActionListener{
/**
* Retrieve the bet amount from the text field and update
* the bet amount. The start button will be disabled
* if the bet amount is valid (i.e. more than zero
* and not exceeding player's budget)。The result button and all 3 replace buttons
* will be enabled after the start button is disabled.
* @param event mouseclick event
*/
public void actionPerformed(ActionEvent event) {
try {
betAmount = Integer.parseInt(inputBetText.getText());
if (betAmount > 0 && betAmount <= budget) {
System.out.println("Start Button disabled");
startButton.setEnabled(false);
btnReplaceCard1.setEnabled(true);
btnReplaceCard2.setEnabled(true);
btnReplaceCard3.setEnabled(true);
resultButton.setEnabled(true);
infoLabel.setText("Your current bet is: $" + betAmount);
moneyLabel.setText("Amount of money you have: $" + budget);
populateCards();
Collections.shuffle(deckOfCards);
playerCards = distributeCards();
dealerCards = distributeCards();
// Set dealer card with their corresponding image
dealerCardImage1 = new ImageIcon(getClass().getResource("images/" + dealerCards.get(0)));
dealerCardImage2 = new ImageIcon(getClass().getResource("images/" + dealerCards.get(1)));
dealerCardImage3 = new ImageIcon(getClass().getResource("images/" + dealerCards.get(2)));
// Set player card with their corresponding image
playerCardImage1 = new ImageIcon(getClass().getResource("images/" + playerCards.get(0)));
playerCardImage2 = new ImageIcon(getClass().getResource("images/" + playerCards.get(1)));
playerCardImage3 = new ImageIcon(getClass().getResource("images/" + playerCards.get(2)));
playerCard1.setIcon(playerCardImage1);
playerCard2.setIcon(playerCardImage2);
playerCard3.setIcon(playerCardImage3);
// Debug
System.out.print("Dealer: ");
for (String i: dealerCards) {
System.out.print(i);
System.out.print(" ");
}
System.out.println();
System.out.print("Player: ");
for (String i: playerCards) {
System.out.print(i);
System.out.print(" ");
}
System.out.println();
} else {
System.out.println("Bet amount is invalid");
JOptionPane.showMessageDialog(frame,"Your bet amount is invalid");
}
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(frame,"Please input a positive integer number.");
System.out.println("Invalid text detected");
} catch (Exception e) {
JOptionPane.showMessageDialog(frame,"An error occurred");
System.out.println("Error: " + e);
}
}
}
/**
* Event handler for result button
*/
class ResultButtonListener implements ActionListener {
/**
* Show the result of the game. The game will reset if player still has money.
* If player hos no money, the player will not be able to continue the game.
* @param event mouse click event
*/
public void actionPerformed(ActionEvent event) {
// Reveal dealer's cards
dealerCard1.setIcon(dealerCardImage1);
dealerCard2.setIcon(dealerCardImage2);
dealerCard3.setIcon(dealerCardImage3);
boolean win = checkWin();
if (win) {
budget += betAmount;
infoLabel.setText("You won $" + betAmount + "!");
moneyLabel.setText("Amount of money you have: $" + budget);
JOptionPane.showMessageDialog(frame, "Congratulations! You win this round!");
System.out.println("Budget is now $" + budget);
} else {
budget -= betAmount;
infoLabel.setText("You lose $" + betAmount + "!");
moneyLabel.setText("Amount of money you have: $" + budget);
System.out.println("Budget is now $" + budget);
JOptionPane.showMessageDialog(frame, "Sorry! The Dealer wins this round!");
}
if (budget > 0) {
System.out.println("Resetting...");
resultButton.setEnabled(false);
startButton.setEnabled(true);
disableAllReplaceButton();
hideCard();
betAmount = 0;
replaceCount = 0;
playerCards.clear();
dealerCards.clear();
deckOfCards.clear();
inputBetText.setText("");
infoLabel.setText("Please place your bet!");
moneyLabel.setText("Amount of money you have is $"+ budget);
System.out.println("Resetting finished.");
} else {
infoLabel.setText("You have no more money!");
moneyLabel.setText("Please start a new game!");
JOptionPane.showMessageDialog(frame, "Game over! You have no more money! " +
"Please start a new game");
resultButton.setEnabled(false);
disableAllReplaceButton();
}
}
}
/**
* Event handler for button replace card one
*/
class ButtonReplaceCard1 implements ActionListener{
/**
* Replace player's first card.
* @param event mouse click event
*/
public void actionPerformed(ActionEvent event) {
if (replaceCount < 2) {
btnReplaceCard1.setEnabled(false);
String newCardFileName = deckOfCards.get(0);
deckOfCards.remove(0);
System.out.println("Deck of card now: " + deckOfCards.size());
playerCards.set(0, newCardFileName);
playerCardImage1 = new ImageIcon(getClass().getResource("images/" + playerCards.get(0)));
playerCard1.setIcon(playerCardImage1);
replaceCount++;
if (replaceCount > 1) {
System.out.println("Disabling... the replace count: " + replaceCount);
disableAllReplaceButton();
}
// Debug
System.out.print("Player replace first card: ");
for (String card: playerCards) {
System.out.print(card);
System.out.print(" ");
}
System.out.println();
} else {
System.out.println("Replace count: " + replaceCount);
System.out.println("You exceeded replacement count");
}
}
}
/**
* Event handler for button replace card 2
*/
class ButtonReplaceCard2 implements ActionListener{
/**
* Replace player's second card.
* @param event mouse click event
*/
public void actionPerformed(ActionEvent event) {
if (replaceCount < 2) {
btnReplaceCard2.setEnabled(false);
String newCardFileName = deckOfCards.get(0);
deckOfCards.remove(0);
System.out.println("Deck of card now: " + deckOfCards.size());
playerCards.set(1, newCardFileName);
playerCardImage2 = new ImageIcon(getClass().getResource("images/" + playerCards.get(1)));
playerCard2.setIcon(playerCardImage2);
replaceCount++;
if (replaceCount > 1) {
System.out.println("Disabling... the replace count: " + replaceCount);
disableAllReplaceButton();
}
// Debug
System.out.print("Player replace second card: ");
for (String card: playerCards) {
System.out.print(card);
System.out.print(" ");
}
System.out.println();
} else {
System.out.println("Replace count: " + replaceCount);
System.out.println("You exceeded replacement count");
}
}
}
/**
* Event handler for button replace card 3
*/
class ButtonReplaceCard3 implements ActionListener{
/**
* Replace player's third card
* @param event mouse click event
*/
public void actionPerformed(ActionEvent event) {
if (replaceCount < 2) {
btnReplaceCard3.setEnabled(false);
String newCardFileName = deckOfCards.get(0);
deckOfCards.remove(0);
System.out.println("Deck of card now: " + deckOfCards.size());
playerCards.set(2, newCardFileName);
playerCardImage3 = new ImageIcon(getClass().getResource("images/" + playerCards.get(2)));
playerCard3.setIcon(playerCardImage3);
replaceCount++;
if (replaceCount > 1) {
System.out.println("Disabling... the replace count: " + replaceCount);
disableAllReplaceButton();
}
// Debug
System.out.print("Player replace third card: ");
for (String card: playerCards) {
System.out.print(card);
System.out.print(" ");
}
System.out.println();
} else {
System.out.println("Replace count: " + replaceCount);
System.out.println("You exceeded replacement count");
}
}
}
/**
* Hide all the card away from letting it be viewed by player
*/
public void hideCard() {
dealerCardImage1 = new ImageIcon(getClass().getResource("images/card_back.gif"));
dealerCardImage2 = new ImageIcon(getClass().getResource("images/card_back.gif"));
dealerCardImage3 = new ImageIcon(getClass().getResource("images/card_back.gif"));
playerCardImage1 = new ImageIcon(getClass().getResource("images/card_back.gif"));
playerCardImage2 = new ImageIcon(getClass().getResource("images/card_back.gif"));
playerCardImage3 = new ImageIcon(getClass().getResource("images/card_back.gif"));
dealerCard1.setIcon(dealerCardImage1);
dealerCard2.setIcon(dealerCardImage2);
dealerCard3.setIcon(dealerCardImage3);
playerCard1.setIcon(playerCardImage1);
playerCard1.setIcon(playerCardImage1);
playerCard2.setIcon(playerCardImage2);
playerCard3.setIcon(playerCardImage3);
}
/**
* A method to disable all replace button
*/
public void disableAllReplaceButton() {
btnReplaceCard1.setEnabled(false);
btnReplaceCard2.setEnabled(false);
btnReplaceCard3.setEnabled(false);
}
/**
* Populate 52 cards with their
* corresponding image file name
*/
public void populateCards() {
System.out.println("Populate card...");
deckOfCards.clear();
ArrayList<String> shapes = new ArrayList<>();
shapes.add("1"); // clubs
shapes.add("2"); // spades
shapes.add("3"); // diamonds
shapes.add("4"); // hearts
ArrayList<String> value = new ArrayList<>();
for (int i = 1; i <= 13; i++) {
value.add(Integer.toString(i));
}
for (String shape : shapes) {
for (String s : value) {
String imageName = "card_" + shape + s + ".gif";
deckOfCards.add(imageName);
}
}
System.out.println("Deck of card size: " + deckOfCards.size());
}
/**
* A method to distribute three cards for player
* @return An ArrayList containing three cards' image file name for the player
*/
public ArrayList<String> distributeCards() {
System.out.println("Card distributed");
ArrayList<String> card = new ArrayList<>();
for (int i = 0; i < 3; i++) {
card.add(deckOfCards.get(i));
}
deckOfCards.subList(0, 3).clear();
System.out.println("Deck of card now: "+ deckOfCards.size());
return card;
}
/**
* A method to slice off the image file name and get the value of the card from
* the file name
* @param cardDeck an ArrayList containing the image file name of the cards
* @return an ArrayList containing the integer value of the cards.
*/
public ArrayList<Integer> filterCardFileName (ArrayList<String> cardDeck) {
ArrayList<Integer> cardValue = new ArrayList<>();
for (String cardFileName: cardDeck) {
// E.g. card_31.gif will become 1.gif or card_411.gif will become 11.gif
String sliceString = cardFileName.substring(6);
// 1.gif will become 1 or 11.gif will become 11
sliceString = sliceString.substring(0, sliceString.length()-4);
int value = Integer.parseInt(sliceString);
cardValue.add(value);
}
return cardValue;
}
/**
* Method to check if the player wins or lose
* @return true if player win. false if player lose
*/
public boolean checkWin() {
ArrayList<Integer> playerCardValue = filterCardFileName(playerCards);
ArrayList<Integer> dealerCardValue = filterCardFileName(dealerCards);
// debug
System.out.print("Dealer: ");
for (int i = 0; i < 3; i++) {
System.out.print(dealerCardValue.get(i));
System.out.print(" ");
}
System.out.println();
System.out.print("Player: ");
for (int i = 0; i < 3; i++) {
System.out.print(playerCardValue.get(i));
System.out.print(" ");
}
System.out.println();
// Rule 1: The one with more special cards wins
int dealerSpecialCardCount = 0;
int playerSpecialCardCount = 0;
for (int i = 0; i < 3; i++) {
if (playerCardValue.get(i) > 10) {
playerSpecialCardCount++;
}
if (dealerCardValue.get(i) > 10) {
dealerSpecialCardCount++;
}
}
System.out.println("Checking rule one...");
if (playerSpecialCardCount > dealerSpecialCardCount) {
System.out.println("Player has more special card than dealer");
System.out.println("Player win");
return true;
} else if (dealerSpecialCardCount > playerSpecialCardCount) {
System.out.println("Dealer has more special card then player");
System.out.println("Player lose");
return false;
}
System.out.println("No outcome for rule one");
System.out.println("Checking rule two...");
// Rule 2: Add the face values of other cards and divide it by 10
int playerCardSum = 0;
int dealerCardSum = 0;
for (int i = 0; i < 3; i++) {
if (playerCardValue.get(i) <= 10) {
playerCardSum += playerCardValue.get(i);
}
}
for (int i = 0; i < 3; i++) {
if (dealerCardValue.get(i) <= 10) {
dealerCardSum += dealerCardValue.get(i);
}
}
if (playerCardSum % 10 > dealerCardSum % 10) {
System.out.println("Player has higher face value than dealer");
System.out.println("Player win");
return true;
} else if (dealerCardSum % 10 > playerCardSum % 10) {
System.out.println("Dealer has higher face value than player");
System.out.println("Player lose");
return false;
} else {
System.out.println("No outcome for rule two.");
System.out.println("Player lose as a result");
return false;
}
}
/**
* A method to start the game
*/
public void run() {
createComponent();
infoLabel.setText("Please place your bet!");
moneyLabel.setText("Amount of money you have is $"+ budget);
}
}