-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChoHanGame.java
477 lines (400 loc) · 15.3 KB
/
ChoHanGame.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
//package group26;
/**
* SER 215 ASU
* Final Project
* File: ChoHanGame.java
*
* Cho Han Game: In this game, you will place a bet on whether the sum
* of the dice rolled will be even or odd. You start with a balance of $500,
* and will win the game once your balance reaches $1000.
* If your balance reaches $0, you lose with $0.00 bonuses.
* For every successful consecutive win, you will earn a bonus amount of $1.
*
* @authors Robert McKinney, Heath Rackham, Tomas Vartija, and Tony Nguyen
* @version 1
*/
import java.util.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.text.DecimalFormat;
public class ChoHanGame
{
public static final double MIN_MONEY = 0;
public static final double MAX_MONEY = 1000;
public static final double INITIAL_MONEY = 500;
private double yourMoney = INITIAL_MONEY;
private double yourBet = 0;
private int numOfGuesses = 0;
private boolean quit = false;
DecimalFormat d_format = new DecimalFormat("#.##");
private ArrayList<Boolean> consecutiveWin = new ArrayList<Boolean>();
private JLabel showMoney = new JLabel("", JLabel.CENTER);
private JLabel betDisplay; // to display the number entered
private JLabel gameInfo = new JLabel("", JLabel.CENTER);
private JLabel bonusDisplay = new JLabel("", JLabel.CENTER);
//private JLabel[] diceDisplay = new JLabel[TOTAL_DICE];
private JLabel diceResults = new JLabel("", JLabel.CENTER);
private JButton betButton = new JButton("Choose Bet");
private JButton oddButton = new JButton("Odd");
private JButton evenButton = new JButton("Even");
private JButton newGameButton = new JButton("New Game");
private JButton quitButton = new JButton("Quit Game");
private ChoHanDice choHanDice = new ChoHanDice();
private int getNumOfGuesses() { return numOfGuesses; }
/**
*
* @return bonus money
*/
public double getBonuses() {
double result = 0;
int count;
int k;
for (int i = 0; i < consecutiveWin.size(); i++) {
count = 0;
k = i;
while (k < consecutiveWin.size() && consecutiveWin.get(k).equals(true)) {
count++;
k++;
}
if (count >= 2)
for (int j = 2; j <= count; j++)
result += 1; // add $1 bonus to consecutive win >=2
}
return result;
}
/**
*
* @return String shows up on gamInfo JLabel
*/
private String setGameInfo() {
String result = "";
if (!quit) {
if (yourMoney > MIN_MONEY && yourMoney < MAX_MONEY) {
result = "<html>" +
"You're about to play a game of Cho Han! In this game, you will place a <br>" +
"bet on whether the sum of the dice rolled will be even or odd. You start <br>" +
"with a balance of $500, and will win the game once your balance reaches <br>" +
"$1000. If your balance reaches $0, you lose with $0.00 bonuses. For every <br>" +
"successful consecutive win, you will earn a bonus amount of $1. <br>" +
"Please place your bet! </html>";
}
else if (yourMoney <= MIN_MONEY) {
result = "<html>You lost the game in " + getNumOfGuesses() + " bet(s).<br>" +
"</html>";
} else {
result = "<html> Congratuations! You won the game in " + getNumOfGuesses() +
" bet(s)!<br>Your balance is $" + d_format.format(yourMoney) + "<br>" +
"Your consecutive win bonus is $" + getBonuses() + "</html>";
}
} else {
result = "<html> Congratuations! You left the game after " + getNumOfGuesses() +
" bet(s)!<br>Your balance is $" + d_format.format(yourMoney) + "<br>" +
"Your consecutive win bonus is $" + getBonuses() + "</html>";
}
return result;
}
/**
*
* @param validBalance, a double parameter
* @return a valid input from user
*/
private double getInput(double validBalance) {
boolean validInput = false; // for input validation
double result;
String inputStr =
JOptionPane.showInputDialog("Enter a number [0 - " +
yourMoney + "]");
do
{
try {
result = Double.parseDouble(inputStr);
} catch (NumberFormatException ex) {
result = -1;
}
if (result < MIN_MONEY|| result > validBalance) {
inputStr =
JOptionPane.showInputDialog("Enter a number [" +
MIN_MONEY + " - " + d_format.format(validBalance) + "]");
} else {
JOptionPane.showMessageDialog(null, "You have entered " +
result);
validInput = true;
}
} while (!validInput);
return result;
}
/**
*
* @param yourMoney, a double parameter
* verifies that the game over or not based on yourMoney
*/
private void gameValidate(double yourMoney) {
if (yourMoney <= MIN_MONEY) {
betDisplay.setText("GAME OVER");
betDisplay.setForeground(Color.RED);
betDisplay.setOpaque(true);
betDisplay.setBackground(Color.WHITE);
betButton.setEnabled(false);
oddButton.setEnabled(false);
evenButton.setEnabled(false);
newGameButton.setEnabled(true);
// reset bonus money to zero, when lose
consecutiveWin = new ArrayList<Boolean>();
bonusDisplay.setText("Your bonus is: $" + getBonuses());
// update game infomation
gameInfo.setText(setGameInfo());
}
else if (yourMoney >= MAX_MONEY) {
betDisplay.setText("YOU ARE THE WINNER");
betDisplay.setForeground(Color.GREEN);
betDisplay.setOpaque(true);
betDisplay.setBackground(Color.BLACK);
betButton.setEnabled(false);
oddButton.setEnabled(false);
evenButton.setEnabled(false);
newGameButton.setEnabled(true);
// update game infomation
gameInfo.setText(setGameInfo());
} else {
newGameButton.setEnabled(false);
}
}
/**
*
* verifies that betMoney can't be bigger that current balance, yourMoney
*/
private void balanceValidate () {
while (yourBet > yourMoney) {
JOptionPane.showMessageDialog(null, "Can't bet more than balance " +
yourMoney);
yourBet = yourMoney;
betDisplay.setText("Your bet is as most as $" + yourBet);
// reset your bet
yourBet = 0;
yourBet = getInput(yourMoney);
}
}
/**
*
* @param x
* @param y
* @return blankLabel that adds more spaces
* between labels or buttons
*/
private JLabel blankLabel(int x, int y) {
JLabel blankLabel = new JLabel("", JLabel.CENTER);
blankLabel.setPreferredSize(new Dimension(x, y));
return blankLabel;
}
private void displayGUI()
{
// Initialize layout manager
JFrame frame = new JFrame("Cho-Han Game");
JPanel contentPane = new JPanel();
contentPane.setOpaque(true);
contentPane.setBackground(Color.CYAN);
frame.setContentPane(contentPane);
frame.setSize(745, 815);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationByPlatform(true);
frame.setVisible(true);
// initialize starting balance on showMoney JLabel
showMoney.setForeground(Color.WHITE);
showMoney.setPreferredSize(new Dimension(200, 30));
showMoney.setOpaque(true);
showMoney.setBackground(Color.BLACK);
showMoney.setText("Your balance: $" + d_format.format(yourMoney));
contentPane.add(showMoney, BorderLayout.PAGE_START);
// add a blank label to move showMoney JLabel to the left
contentPane.add(blankLabel(525, 30), BorderLayout.PAGE_START);
// initialize bonusDisplay JLabel
bonusDisplay.setForeground(Color.RED);
bonusDisplay.setPreferredSize(new Dimension(200, 30));
bonusDisplay.setOpaque(true);
bonusDisplay.setBackground(Color.BLACK);
bonusDisplay.setText("Your bonus is: $" + 0);
contentPane.add(bonusDisplay, BorderLayout.PAGE_START);
// add a blank label to move bonusDisplay JLabel to the left
contentPane.add(blankLabel(525, 30), BorderLayout.PAGE_START);
// add blank label to separate bonusDislay Jlabel with below components
contentPane.add(blankLabel(700, 50), BorderLayout.CENTER);
// Initialize diceResults JLabel
diceResults.setText("Make a bet, Then choose \"Odd\" or \"Even\"");
diceResults.setPreferredSize(new Dimension(300, 80));
diceResults.setForeground(Color.WHITE);
diceResults.setOpaque(true);
diceResults.setBackground(Color.BLACK);
contentPane.add(diceResults, BorderLayout.CENTER);
// add blank label to separate diceResults Jlabel with below components
contentPane.add(blankLabel(700, 30), BorderLayout.CENTER);
// add dice GUI to contentPane
Die[] dice = choHanDice.getDice();
for (Die die : dice) {
contentPane.add(die, BorderLayout.LINE_START);
contentPane.add(blankLabel(5, 60));
}
contentPane.add(blankLabel(700, 30), BorderLayout.CENTER);
// Initialize gameInfo JLabel
gameInfo.setText(setGameInfo());
gameInfo.setForeground(Color.WHITE);
gameInfo.setPreferredSize(new Dimension(545, 120));
gameInfo.setOpaque(true);
gameInfo.setBackground(Color.BLACK);
contentPane.add(gameInfo, BorderLayout.CENTER);
// add blank label to separate gameInfo Jlabel with below components
contentPane.add(blankLabel(700, 30), BorderLayout.CENTER);
// Initialize betDispaly JLabel
betDisplay = new JLabel("", JLabel.CENTER);
betDisplay.setText("Your bet is $" + yourBet);
betDisplay.setPreferredSize(new Dimension(280, 40));
betDisplay.setForeground(Color.WHITE);
betDisplay.setOpaque(true);
betDisplay.setBackground(Color.BLACK);
contentPane.add(betDisplay, BorderLayout.CENTER);
// add blank label to separate betDisplay Jlabel with below components
contentPane.add(blankLabel(700, 30), BorderLayout.CENTER);
// add a blank label to move oddButton to the left
contentPane.add(blankLabel(140, 40), BorderLayout.CENTER);
oddButton.setPreferredSize(new Dimension(80, 40));
//oddButton.setBackground(null);
oddButton.setEnabled(false);
contentPane.add(oddButton, BorderLayout.LINE_START);
// Register listener for oddButton
oddButton.addActionListener(new OddButtonListener());
// add a blank label to separate oddButton and evenButton
contentPane.add(blankLabel(150, 40), BorderLayout.CENTER);
evenButton.setPreferredSize(new Dimension(80, 40));
evenButton.setEnabled(false);
contentPane.add(evenButton);
// Register listener for evenButton
evenButton.addActionListener(new EvenButtonListener());
// add a blank label to separate evenButton and betButton
contentPane.add(blankLabel(150, 40), BorderLayout.CENTER);
betButton.setPreferredSize(new Dimension(140, 40));
contentPane.add(betButton);
// Register listener for betButton
betButton.addActionListener(new BetButtonListener());
// add blank label to separate odd/even/bet buttons with below components
contentPane.add(blankLabel(700, 5), BorderLayout.CENTER);
// add a blank label to move newGameButton to the left
newGameButton.setPreferredSize(new Dimension(140, 40));
contentPane.add(newGameButton);
// Register listener for newGameButton
newGameButton.addActionListener(new NewGameButtonListener());
quitButton.setPreferredSize(new Dimension(140, 40));
contentPane.add(quitButton);
// Register listener for quitButton
quitButton.addActionListener(new QuitButtonListener());
}
private class OddButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
balanceValidate();
// update random number for next guess
choHanDice.rollDice();
betDisplay.setText("Your bet is $" + yourBet);
if (choHanDice.sumOfDice() % 2 == 1) {
consecutiveWin.add(true);
yourMoney += yourBet;
diceResults.setText("Dice Total: " + choHanDice.sumOfDice() + "... You Win!");
diceResults.setForeground(Color.GREEN);
} else {
consecutiveWin.add(false);
yourMoney -= yourBet;
diceResults.setText("Dice Total: " + choHanDice.sumOfDice() + "... You Lose!");
diceResults.setForeground(Color.RED);
}
// update bonus money
bonusDisplay.setText("Your bonus is: $" + getBonuses());
// update numOfGuesses
numOfGuesses++;
// check for game over
gameValidate(yourMoney);
// update balance
showMoney.setText("Your balance: $" + d_format.format(yourMoney));
}
}
private class EvenButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
balanceValidate();
// update random number for next guess
choHanDice.rollDice();
betDisplay.setText("Your bet is $" + yourBet);
if (choHanDice.sumOfDice() % 2 == 0) {
consecutiveWin.add(true);
yourMoney += yourBet;
diceResults.setText("Dice Total: " + choHanDice.sumOfDice() + "... You Win!");
diceResults.setForeground(Color.GREEN);
} else {
consecutiveWin.add(false);
yourMoney -= yourBet;
diceResults.setText("Dice Total: " + choHanDice.sumOfDice() + "... You Lose!");
diceResults.setForeground(Color.RED);
}
// update bonus money
bonusDisplay.setText("Your bonus is: $" + getBonuses());
// update numOfGuesses
numOfGuesses++;
// check for game over
gameValidate(yourMoney);
// update balance
showMoney.setText("Your balance: $" + d_format.format(yourMoney));
}
}
private class BetButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
oddButton.setEnabled(true);
evenButton.setEnabled(true);
yourBet = getInput(yourMoney);
betDisplay.setText("Your bet is $" + yourBet);
}
}
private class NewGameButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
yourMoney = INITIAL_MONEY;
yourBet = 0;
quit = false;
// enable all buttons
betButton.setEnabled(true);
// update balance
showMoney.setText("Your balance: $" + d_format.format(yourMoney));
// update bet display
betDisplay.setText("Your bet is $" + yourBet);
betDisplay.setForeground(Color.WHITE);
betDisplay.setOpaque(true);
betDisplay.setBackground(Color.BLACK);
// reset bonus money to zero, when playing new game
consecutiveWin = new ArrayList<Boolean>();
bonusDisplay.setText("Your bonus is: $" + getBonuses());
// reset numOfGuesses to zero
numOfGuesses = 0;
// reset quit
quit = false;
// reset gameInfo JLabel
gameInfo.setText(setGameInfo());
}
}
private class QuitButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
quit = true;
oddButton.setEnabled(false);
evenButton.setEnabled(false);
betButton.setEnabled(false);
newGameButton.setEnabled(true);
// reset gameInfo JLabel if quit
gameInfo.setText(setGameInfo());
}
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new ChoHanGame().displayGUI();
}
});
}
}