-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyGui.java
338 lines (294 loc) · 13.7 KB
/
MyGui.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
import javax.swing.*;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.event.MenuEvent;
import javax.swing.event.MenuListener;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
public class MyGui {
int Total_money = 100;
public static void main(String[] args) {
MyGui gui = new MyGui();
gui.go();
}
public void go() {
// Card Label
JLabel label_Image1 = new JLabel();
JLabel label_Image2 = new JLabel();
JLabel label_Image3 = new JLabel();
JLabel label_Image4 = new JLabel();
JLabel label_Image5 = new JLabel();
JLabel label_Image6 = new JLabel();
label_Image1.setBorder(new CompoundBorder(label_Image1.getBorder(), new EmptyBorder(10,10,10,10)));
label_Image2.setBorder(new CompoundBorder(label_Image2.getBorder(), new EmptyBorder(10,10,10,10)));
label_Image3.setBorder(new CompoundBorder(label_Image3.getBorder(), new EmptyBorder(10,10,10,10)));
label_Image4.setBorder(new CompoundBorder(label_Image4.getBorder(), new EmptyBorder(10,10,10,10)));
label_Image5.setBorder(new CompoundBorder(label_Image5.getBorder(), new EmptyBorder(10,10,10,10)));
label_Image6.setBorder(new CompoundBorder(label_Image6.getBorder(), new EmptyBorder(10,10,10,10)));
//Replace Button
JButton btn_rpcard1 = new JButton("Replace Card 1");
JButton btn_rpcard2 = new JButton("Replace Card 2");
JButton btn_rpcard3 = new JButton("Replace Card 3");
// Game button
JButton btn_start = new JButton("Start");
JButton btn_result = new JButton("Result");
// Game Label
JLabel label_bet = new JLabel("Bet: $");
JLabel label_money = new JLabel(String.valueOf(Total_money));
//Text field
JTextField txt_inputbet = new JTextField(10);
//Info label
JLabel label_info = new JLabel("Please place your bet! Amount of money you have:$");
//ImageIcon set
ImageIcon Image1 = new ImageIcon("Images/card_back.gif");
ImageIcon Image2 = new ImageIcon("Images/card_back.gif");
ImageIcon Image3 = new ImageIcon("Images/card_back.gif");
ImageIcon Image4 = new ImageIcon("Images/card_back.gif");
ImageIcon Image5 = new ImageIcon("Images/card_back.gif");
ImageIcon Image6 = new ImageIcon("Images/card_back.gif");
//Label set ImageIcon
label_Image1.setIcon(Image1);
label_Image2.setIcon(Image2);
label_Image3.setIcon(Image3);
label_Image4.setIcon(Image4);
label_Image5.setIcon(Image5);
label_Image6.setIcon(Image6);
//Panel Init
JPanel MainPanel = new JPanel();
JPanel DealerPanel = new JPanel();
JPanel PlayerPanel = new JPanel();
JPanel RpCardBtnPanel = new JPanel();
JPanel ButtonPanel = new JPanel();
JPanel InfoPanel = new JPanel();
//DealerPanel Init
DealerPanel.add(label_Image1);
DealerPanel.add(label_Image2);
DealerPanel.add(label_Image3);
//PlayerPanel Init
PlayerPanel.add(label_Image4);
PlayerPanel.add(label_Image5);
PlayerPanel.add(label_Image6);
//RpCardBtnPanel Init
RpCardBtnPanel.add(btn_rpcard1);
RpCardBtnPanel.add(btn_rpcard2);
RpCardBtnPanel.add(btn_rpcard3);
//ButtonPanel Init
ButtonPanel.add(label_bet);
ButtonPanel.add(txt_inputbet);
ButtonPanel.add(btn_start);
ButtonPanel.add(btn_result);
//InfoPanel Init
InfoPanel.add(label_info);
InfoPanel.add(label_money);
//MainPanel Init
MainPanel.setLayout(new GridLayout(5,1));
MainPanel.add(DealerPanel);
MainPanel.add(PlayerPanel);
MainPanel.add(RpCardBtnPanel);
MainPanel.add(ButtonPanel);
MainPanel.add(InfoPanel);
//Set Background Colour
DealerPanel.setBackground(Color.green);
PlayerPanel.setBackground(Color.green);
RpCardBtnPanel.setBackground(Color.green);
//Frame Init
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(MainPanel);
//JMenuBar Init
JMenuBar menuBar = new JMenuBar();
frame.setJMenuBar(menuBar);
JMenu Control = new JMenu("Control");
menuBar.add(Control);
JMenuItem exit = new JMenuItem("Exit");
Control.add(exit);
exit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
JMenu help = new JMenu("Help");
menuBar.add(help);
help.addMenuListener(new MenuListener() {
@Override
public void menuSelected(MenuEvent e) {
JOptionPane.showMessageDialog(frame," Rules to determine who has better cards:\n" +
"J, Q, K are regarded as special cards.\n" +
"Rule 1: The one with more special cards wins.\n" +
"Rule 2: If both have the same number of special cards, add the face values of the other card(s) and take the remainder after dividing the sum by 10. The one with a bigger remainder wins. (Note: Ace = 1).\n" +
"Rule 3: The dealer wins if both rule 1 and rule 2 cannot distinguish the winner.");
}
@Override
public void menuDeselected(MenuEvent e) {
}
@Override
public void menuCanceled(MenuEvent e) {
}
});
//frame setting
frame.setTitle("A Simple Card Game");
frame.setSize(700, 700);
frame.setVisible(true);
//game init
Game game = new Game();
btn_rpcard1.setEnabled(false);
btn_rpcard2.setEnabled(false);
btn_rpcard3.setEnabled(false);
btn_result.setEnabled(false);
//Btn_rpcard1 Listener
btn_rpcard1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (game.gameStatus){
game.replacePlayerCard(1);
Card Card1 = game.getPlayerHand().get(0);
ImageIcon tmp = new ImageIcon("Images/card_"+ Card1.rank + Card1.suit+".gif");
label_Image4.setIcon(tmp);
btn_rpcard1.setEnabled(false);
if (game.player.getReplace_time()==0){
btn_rpcard1.setEnabled(false);
btn_rpcard2.setEnabled(false);
btn_rpcard3.setEnabled(false);
}
}
}
});
//Btn_rpcard2 Listener
btn_rpcard2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (game.gameStatus){
game.replacePlayerCard(2);
Card Card2 = game.getPlayerHand().get(1);
ImageIcon tmp = new ImageIcon("Images/card_"+ Card2.rank + Card2.suit+".gif");
label_Image5.setIcon(tmp);
btn_rpcard2.setEnabled(false);
if (game.player.getReplace_time()==0){
btn_rpcard1.setEnabled(false);
btn_rpcard2.setEnabled(false);
btn_rpcard3.setEnabled(false);
}
}
}
});
//Btn_rpcard3 Listener
btn_rpcard3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (game.gameStatus){
game.replacePlayerCard(3);
Card Card3 = game.getPlayerHand().get(2);
ImageIcon tmp = new ImageIcon("Images/card_"+ Card3.rank + Card3.suit+".gif");
label_Image6.setIcon(tmp);
btn_rpcard3.setEnabled(false);
if (game.player.getReplace_time()==0){
btn_rpcard1.setEnabled(false);
btn_rpcard2.setEnabled(false);
btn_rpcard3.setEnabled(false);
}
}
}
});
//Btn_start Listener
btn_start.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (game.gameStatus == false) {
try {
int bet = Integer.parseInt(txt_inputbet.getText());
if (bet <= 0 || bet > Total_money) {
JOptionPane.showMessageDialog(frame, "WARNING : The bet you place must be a Positive Integer");
} else {
label_money.setText(String.valueOf(Total_money));
game.Start();
label_info.setText("Your current bet is:$"+bet+" Amount of money you have:$");
game.setGame_bet(bet);
btn_start.setEnabled(false);
txt_inputbet.setEditable(false);
btn_rpcard1.setEnabled(true);
btn_rpcard2.setEnabled(true);
btn_rpcard3.setEnabled(true);
btn_result.setEnabled(true);
ImageIcon CardBack = new ImageIcon("Images/card_back.gif");
label_Image1.setIcon(CardBack);
label_Image2.setIcon(CardBack);
label_Image3.setIcon(CardBack);
ArrayList<Card> playerHand = game.getPlayerHand();
int counter = 0;
for (Card i : playerHand) {
if (counter == 0) {
ImageIcon tmp = new ImageIcon("Images/card_" + i.rank + i.suit + ".gif");
label_Image4.setIcon(tmp);
counter++;
} else if (counter == 1) {
ImageIcon tmp = new ImageIcon("Images/card_" + i.rank + i.suit + ".gif");
label_Image5.setIcon(tmp);
counter++;
} else if (counter == 2) {
ImageIcon tmp = new ImageIcon("Images/card_" + i.rank + i.suit + ".gif");
label_Image6.setIcon(tmp);
counter++;
}
}
}
} catch (NumberFormatException a) {
JOptionPane.showMessageDialog(frame, "WARNING : The bet you place must be a Positive Integer");
}
}
}
});
//Btn_result Listener
btn_result.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ArrayList<Card> DealerHand = game.getDealerhand();
int counter = 0;
for (Card i : DealerHand) {
if (counter == 0) {
ImageIcon tmp = new ImageIcon("Images/card_" + i.rank + i.suit + ".gif");
label_Image1.setIcon(tmp);
counter++;
} else if (counter == 1) {
ImageIcon tmp = new ImageIcon("Images/card_" + i.rank + i.suit + ".gif");
label_Image2.setIcon(tmp);
counter++;
} else if (counter == 2) {
ImageIcon tmp = new ImageIcon("Images/card_" + i.rank + i.suit + ".gif");
label_Image3.setIcon(tmp);
counter++;
}
}
if (game.gameStatus){
Boolean Player_win=game.result();
if (Player_win){
JOptionPane.showMessageDialog(frame, "Congratulations! You win this round!");
Total_money+=game.getGame_bet();
}else {
JOptionPane.showMessageDialog(frame, "Sorry! The Dealer wins this round!");
Total_money-=game.getGame_bet();
}
label_money.setText(String.valueOf(Total_money));
label_info.setText("Please place your bet! Amount of money you have:$");
txt_inputbet.setEditable(true);
btn_start.setEnabled(true);
txt_inputbet.setEditable(true);
btn_rpcard1.setEnabled(false);
btn_rpcard2.setEnabled(false);
btn_rpcard3.setEnabled(false);
btn_result.setEnabled(false);
if (Total_money == 0){
label_money.setText("");
JOptionPane.showMessageDialog(frame,
"Game over!\n" +
"You have no more money!\n" +
"Please start a new game!"
);
label_info.setText("You have no more money! Please start a new game!");
txt_inputbet.setEditable(false);
btn_start.setEnabled(false);
btn_result.setEnabled(false);
btn_rpcard1.setEnabled(false);
btn_rpcard2.setEnabled(false);
btn_rpcard3.setEnabled(false);
}
}
}
});
}
}