-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDealGameGUI.java
More file actions
executable file
·326 lines (280 loc) · 14.1 KB
/
Copy pathDealGameGUI.java
File metadata and controls
executable file
·326 lines (280 loc) · 14.1 KB
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
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.font.*;
import java.util.*;
import java.text.*;
/**
* DealGameGUI.java
*
* This class is used to provide a visual interface for a player in the CSC116
* "Deal or No Deal" Game
*
* @author Dan Longo
* @author Suzanne Balik
*/
public class DealGameGUI extends JFrame implements ActionListener {
/** width of frame */
public static final int WIDTH = 900;
/** height of frame */
public static final int HEIGHT = 700;
/** x location of upper-left corner of frame */
public static final int X_LOCATION = 100;
/** y location of upper-left corner of frame */
public static final int Y_LOCATION = 100;
/** number of rows for boxes */
public static final int BOX_ROWS = 6;
/** number of columns for boxes */
public static final int BOX_COLS = 5;
/** number of rows for monetary values */
public static final int VALUE_ROWS = 13;
/** number of columns for monetary values */
public static final int VALUE_COLS = 2;
/** Formatting object for monetary values */
private static final NumberFormat CURRENCY_FORMAT = NumberFormat.getCurrencyInstance();
/** Deal game */
private DealGame game;
/** Buttons that the player will use to indicate a box they want to select */
private JButton[] boxBtns;
/**
* Labels that will show the player which monetary values are left in the game
*/
private JLabel[] valueLbls;
/** Label that shows the current status of the game */
private JLabel statusLbl;
/** Container for all of the box buttons */
private JPanel boxPanel;
/** Container for the labels that show monetary values */
private JPanel valuePanel;
/**
* Font that will be used on labels with values that have been revealed during
* the game (the same text but not as dark and with a line through it)
*/
private static Font STRIKE_FONT;
/**
* Initializes the GUI and displays it for the user to interact with.
*
* @param testing true if in "testing mode", false otherwise
*/
public DealGameGUI(boolean testing) {
setSize(WIDTH, HEIGHT);
setLocation(X_LOCATION, Y_LOCATION);
setTitle("Deal or No Deal");
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container c = getContentPane();
game = new DealGame(testing);
statusLbl = new JLabel("Select Your Box By Clicking On It");
boxBtns = new JButton[DealGame.NUM_BOXES];
valueLbls = new JLabel[DealGame.NUM_BOXES];
boxPanel = new JPanel();
boxPanel.setLayout(new GridLayout(BOX_ROWS, BOX_COLS));
valuePanel = new JPanel();
valuePanel.setLayout(new GridLayout(VALUE_ROWS, VALUE_COLS));
Border b = BorderFactory.createLineBorder(Color.black);
valuePanel.setBorder(new TitledBorder(b, "Remaining Values", TitledBorder.CENTER,
TitledBorder.ABOVE_TOP));
// Set up the Font with the strike through
Map<TextAttribute, Boolean> attributes = new HashMap<TextAttribute, Boolean>();
attributes.put(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON);
STRIKE_FONT = new Font(attributes);
for (int i = 0; i < DealGame.NUM_BOXES; i++) {
boxBtns[i] = new JButton("Box " + (i + 1));
boxBtns[i].addActionListener(this);
boxPanel.add(boxBtns[i]);
valueLbls[i] = new JLabel(CURRENCY_FORMAT.format(DealGame.BOX_VALUES[i]));
}
// add a blank spot in the box Panel as a divider for the high score label
boxPanel.add(new JLabel(""));
boxPanel.add(new JLabel("High Score:", JLabel.RIGHT));
boxPanel.add(new JLabel(CURRENCY_FORMAT.format(game.getHighScore()), JLabel.CENTER));
// stagger the values so that they appear in two columns in the GUI
int lblCount = 0;
int nextPosition = 0;
while (lblCount < DealGame.NUM_BOXES) {
valuePanel.add(valueLbls[nextPosition]);
lblCount++;
if (lblCount % 2 == 1) {
nextPosition += VALUE_ROWS;
} else {
nextPosition -= VALUE_ROWS - 1;
}
}
c.add(statusLbl, BorderLayout.NORTH);
c.add(boxPanel, BorderLayout.CENTER);
c.add(valuePanel, BorderLayout.EAST);
setVisible(true);
}
/**
* Responds to the user clicking on buttons in the GUI
*
* @param e The event that has triggered this method (the button that was
* clicked)
*/
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < boxBtns.length; i++) {
if (e.getSource() == boxBtns[i]) {
boxBtns[i].setEnabled(false);
if (game.hasPlayerChosenBox()) {
game.selectBox(i);
double value = game.getValueInBox(i);
boxBtns[i].setText(CURRENCY_FORMAT.format(value));
// we need to cross out this value from the list
int labelNum = findLabelWithValue(value);
valueLbls[labelNum].setFont(STRIKE_FONT);
statusLbl.setText("Player opens Box " + (i + 1) + ", which contains "
+ CURRENCY_FORMAT.format(game.getValueInBox(i)) + ". "
+ game.getBoxesRemainingToOpenThisRound()
+ " boxes left to open this round.");
} else {
game.selectBox(i);
boxBtns[i].setText("Your Box");
statusLbl.setText(game.getBoxesRemainingToOpenThisRound()
+ " boxes left to open this round.");
}
if (game.isEndOfRound()) {
// prompt to make a deal
double offer = game.getCurrentOffer();
String strOffer = CURRENCY_FORMAT.format(offer);
int response = JOptionPane.showConfirmDialog(null,
"The Banker is offering you " + strOffer
+ " for your box. Do you accept?",
"Deal or No Deal?", JOptionPane.YES_NO_OPTION);
if (response == JOptionPane.NO_OPTION) {
// the user has selected "No"
if (game.getRound() == DealGame.NUM_ROUNDS - 1) {
int choice = JOptionPane.showConfirmDialog(null,
"It is the last round! Do "
+ "you want to keep your box (YES), or "
+ "swap it for the last one remaining " + "(NO)?",
"Deal or No Deal?", JOptionPane.YES_NO_OPTION);
// the game is over, display the results of the user opening their box
double value = game.getPlayerBoxValue();
String strValue = CURRENCY_FORMAT.format(value);
double otherValue = getValueOfLastBox();
String strOtherValue = CURRENCY_FORMAT.format(otherValue);
if (choice == JOptionPane.YES_OPTION) {
if (value >= otherValue) {
JOptionPane.showMessageDialog(null,
"You have stayed with your box, and earned " + strValue
+ ". The other box contained " + strOtherValue
+ ". You have made a GOOD DEAL!",
"Game Over", JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(null,
"You have stayed with your box, and earned " + strValue
+ ". The other box contained " + strOtherValue
+ ". You have made a BAD DEAL!",
"Game Over", JOptionPane.INFORMATION_MESSAGE);
}
if (game.isNewHighScore(value)) {
JOptionPane.showMessageDialog(null,
"You have set a new high score!!! " + strValue,
"New High Score!", JOptionPane.INFORMATION_MESSAGE);
}
} else {
if (otherValue >= value) {
JOptionPane.showMessageDialog(null,
"You have swapped boxes, and earned " + strOtherValue
+ ". Your original box contained " + strValue
+ ". You have made a GOOD DEAL!",
"Game Over", JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(null,
"You have swapped boxes, and earned " + strOtherValue
+ ". Your original box contained " + strValue
+ ". You have made a BAD DEAL!",
"Game Over", JOptionPane.INFORMATION_MESSAGE);
}
if (game.isNewHighScore(otherValue)) {
JOptionPane.showMessageDialog(null,
"You have set a new high score!!! " + strOtherValue,
"New High Score!", JOptionPane.INFORMATION_MESSAGE);
}
}
System.exit(0);
} else {
game.startNextRound();
statusLbl.setText(game.getBoxesRemainingToOpenThisRound()
+ " boxes left to open this round.");
}
} else {
// the user has selected "Yes"
// the game is over, display what their case contained and
// tell if it was a good deal or not
double value = game.getPlayerBoxValue();
String strValue = CURRENCY_FORMAT.format(value);
if (offer < value) {
JOptionPane.showMessageDialog(null,
"You have accepted the offer of " + strOffer
+ ". Your box contained " + strValue
+ ", so you have made a BAD DEAL!",
"Game Over", JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(null,
"You have accepted the offer of " + strOffer
+ ". Your box contained " + strValue
+ ", so you have made a GOOD DEAL!",
"Game Over", JOptionPane.INFORMATION_MESSAGE);
}
if (game.isNewHighScore(offer)) {
JOptionPane.showMessageDialog(null,
"You have set a new high score!!! " + strOffer,
"New High Score!", JOptionPane.INFORMATION_MESSAGE);
}
System.exit(0);
}
}
}
}
}
/**
* Searches through the array of labels and returns the index of the label that
* displays the given value
*
* @param value The monetary value displayed on the label we are looking for
* @return The index number of the label in the array that displays the given
* value
*/
private int findLabelWithValue(double value) {
String textVal = CURRENCY_FORMAT.format(value);
for (int i = 0; i < valueLbls.length; i++) {
if (valueLbls[i].getText().equals(textVal)) {
return i;
}
}
return 0;
}
/**
* Returns value of the first unopened box other than the player's box If all
* other boxes have been opened, this will be the last box
*
* @return value of first unopened box other than the player's box or 0.0 if all
* boxes other than the player's box are open
*/
private double getValueOfLastBox() {
double playerBoxValue = game.getPlayerBoxValue();
for (int i = 0; i < DealGame.NUM_BOXES; i++) {
double value = game.getValueInBox(i);
if (value != playerBoxValue && !game.isBoxOpen(i)) {
return value;
}
}
return 0.0;
}
/**
* Creates a new DealGameGUI object to begin a game.
*
* @param args command line arguments args[0] optional testing argument -t if in
* testing mode
*/
public static void main(String[] args) {
if (args.length == 1 && args[0].equals("-t")) {
new DealGameGUI(true);
} else if (args.length == 0) {
new DealGameGUI(false);
} else {
System.out.println("Usage: java DealGameGUI [-t]");
}
}
}