-
Notifications
You must be signed in to change notification settings - Fork 0
/
MonopolyGUI.java
387 lines (348 loc) · 14.3 KB
/
MonopolyGUI.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
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
import java.util.List;
/** The GUI for monopoly (Controller).
* @author Kevin Lowe
*/
public class MonopolyGUI implements ActionListener {
/** The Monopoly game where I get the information from. */
private Monopoly _game;
/** My frame. */
private JFrame _frame;
/** My main panel that is in _frame. */
private MainPanel _panel;
/** Pop up JFrame */
private JFrame _popUpFrame;
/** Mortgage Property Pop Up Panel */
private MortgagePropertyPanel _mortPanel;
/** Upgrading and Selling House Pop up Panel */
private HousePanel _housePanel;
/** Initializes the panels and buttons. */
public MonopolyGUI(int players) {
_game = new Monopoly(players, this);
_panel = new MainPanel(_game, this);
_panel.setBounds(0, 0, 800, 650);
_frame = new JFrame("Monopoly");
_frame.setLayout(null);
_frame.add(_panel);
_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
_frame.setSize(800, 650);
_frame.setResizable(false);
_frame.setVisible(true);
}
/** Returns my panel. */
public MainPanel panel() {
return _panel;
}
/** Takes care of all main board button actions. */
@Override
public void actionPerformed(ActionEvent e) {
switch (e.getActionCommand()) {
case "New Game":
_game = new Monopoly(_game.getNumPlayers(), this);
resetGames(_game);
_panel.status().addLine("New Game Started");
_panel.status().repaint();
_panel.board().repaint();
_panel.players().repaint();
_panel.buttons().roll().setText("Roll Dice");
break;
case "Quit":
System.exit(0);
break;
case "About":
buyPropertyPopUp("Mediterranean Avenue");
break;
case "Roll Dice":
_panel.buttons().roll().setEnabled(false);
if (_game.current().isJailed()) {
rollDiceJail();
} else {
rollDice();
}
break;
case "Mortgage":
mortgagePopUp();
break;
case "Buy/Sell Houses":
houses();
break;
case "Trade":
JOptionPane.showMessageDialog(null, "Will Come Soon");
break;
case "Buy":
_game.current().buyProperty((Property)_game.current().location().piece());
_panel.status().addLine("Player "+ _game.current().getID() + " bought "
+ _game.current().location().piece().name());
_panel.status().repaint();
_panel.board().repaint();
_panel.players().repaint();
_popUpFrame.dispose();
break;
case "No":
_popUpFrame.dispose();
break;
case "Mortgage/Unmortgage Property":
if (!(_mortPanel.property().mortgageToggle(_game.current()))) {
_panel.status().addLine("Not able to Mortgage/Unmortgage");
_panel.status().repaint();
} else {
_panel.status().addLine("Mortgaged/Unmortgaged " + _mortPanel.property().name());
_panel.status().repaint();
_panel.board().repaint();
_panel.players().repaint();
}
_popUpFrame.dispose();
break;
case "Exit":
_popUpFrame.dispose();
break;
case "Upgrade House":
if (_housePanel.property() != null) {
_game.current().upgradeProperty(_housePanel.property());
}
break;
case "Sell House":
if (_housePanel.property() != null) {
_game.current().sellHouse(_housePanel.property());
}
break;
case "End Turn":
endTurn();
break;
}
}
/** Reset all _game variables for the different panels with
* the new game. */
public void resetGames(Monopoly newGame) {
_panel.setGame(newGame);
_panel.players().setGame(newGame);
_panel.board().setGame(newGame);
}
/** Handles the GUI dice roll for a normal player's turn. */
private void rollDice() {
SwingWorker<Void, Void> mover = new SwingWorker<Void, Void>() {
private boolean wasSentToJail = false;
@Override
protected Void doInBackground() throws Exception {
Player current = _game.current();
int[] rolls = current.rolls();
rolls[0] = current.rollDice();
String line = "Player " + current.getID() + " rolled a ";
_panel.status().addLine(line + rolls[0]);
publish();
Thread.sleep(500);
rolls[1] = current.rollDice();
_panel.status().addLine(line + rolls[1]);
publish();
if (rolls[0] == rolls[1]) {
current.setDoubles(current.doubleTurns() + 1);
if (current.doubleTurns() == 3) {
String line1 = "3 doubles, Player " +
current.getID() + " has been sent to jail";
current.inJail(true);
current.jumpPlayer("Jail");
_game.nextPlayer();
wasSentToJail = true;
String line2 = "Player " + _game.current().getID()
+ "'s turn";
_panel.status().addLine(line1);
_panel.status().addLine(line2);
} else {
current.movePlayer(current.getLastRoll());
}
} else {
current.movePlayer(current.getLastRoll());
current.setDoubles(0);
}
if ((current.location().piece().name().equals(
"Jail") && (current.isJailed()))) {
wasSentToJail = true;
}
return null;
}
protected void process(List<Void> chunks) {
_panel.status().repaint();
_panel.board().repaint();
}
protected void done() {
_panel.status().repaint();
_panel.board().repaint();
_panel.players().repaint();
if (!wasSentToJail) {
Player current = _game.current();
int[] rolls = current.rolls();
if (rolls[0] != rolls[1]) {
_panel.buttons().roll().setText("End Turn");
}
}
_panel.buttons().roll().setEnabled(true);
}
};
mover.execute();
}
/** Handles the GUI dice roll for when the player is in jail. */
private void rollDiceJail() {
SwingWorker<Void, Void> mover = new SwingWorker<Void, Void>() {
@Override
protected Void doInBackground() throws Exception {
Player current = _game.current();
String answer = jailedPopUp(current);
if (answer.equals("Pay $50")) {
current.loseMoney(50);
publish();
current.inJail(false);
current.setDoubles(0);
rollDice();
} else if (answer.equals("Roll Dice")) {
int[] rolls = current.rolls();
rolls[0] = current.rollDice();
String line = "Player " + current.getID() +
" rolled a ";
_panel.status().addLine(line + rolls[0]);
publish();
Thread.sleep(500);
rolls[1] = current.rollDice();
_panel.status().addLine(line + rolls[1]);
publish();
if (rolls[0] == rolls[1]) {
String line1 = "Doubles! Player " +
current.getID() + " is free.";
_panel.status().addLine(line1);
current.inJail(false);
current.setDoubles(0);
current.movePlayer(current.getLastRoll());
} else {
current.setTurns(current.jailedTurns() - 1);
if (current.jailedTurns() == 0) {
current.loseMoney(50);
publish();
current.inJail(false);
current.movePlayer(current.getLastRoll());
}
}
_panel.buttons().roll().setText("End Turn");
_panel.buttons().roll().setEnabled(true);
} else {
current.jailFree(false);
current.inJail(false);
rollDice();
}
return null;
}
protected void process(List<Void> chunks) {
_panel.status().repaint();
_panel.board().repaint();
_panel.players().repaint();
}
};
mover.execute();
}
/** Handles the pop up for when the player is in Jail */
private String jailedPopUp(Player current) {
Object[] possibleChoicesFree = {"Pay $50", "Roll Dice", "Use Get out of Jail Free Card"};
Object[] possibleChoices = {"Pay $50", "Roll Dice"};
String question = "You have " + current.jailedTurns() + " turns in Jail\n"
+ "Choose one of the following\n";
int selectedValue;
if (current.jailFree()) {
selectedValue = (int) JOptionPane.showOptionDialog(null, question, "In Jail",
JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, possibleChoicesFree, null);
} else {
selectedValue = (int) JOptionPane.showOptionDialog(null, question, "In Jail",
JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, possibleChoices, null);
}
// Pay $50
if (selectedValue == 0) {
return "Pay $50";
// Roll Dice
} else if (selectedValue == 1) {
return "Roll Dice";
// Use Jail Free Card
} else {
return "Jail Free";
}
}
/** Handles the pop up for Buying Property */
public void buyPropertyPopUp(String property) {
//TODO Create the Frame
_popUpFrame = new JFrame("Buying Property");
BuyPropertyPanel buyPanel = new BuyPropertyPanel(property, this);
buyPanel.setBounds(0, 0, 350, 325);
_popUpFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
_popUpFrame.add(buyPanel);
_popUpFrame.setLayout(null);
_popUpFrame.setSize(350, 325);
_popUpFrame.setResizable(false);
_popUpFrame.setLocation(213, 200);
_popUpFrame.setVisible(true);
}
private void auctioningPopUp() {
//TODO Optional
}
/** Handles the case when mortgage is pressed */
private void mortgagePopUp() {
_popUpFrame = new JFrame("Mortgaging Property");
_mortPanel = new MortgagePropertyPanel(this, _game);
_popUpFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
_mortPanel.setBounds(0, 0, 800, 650);
_popUpFrame.add(_mortPanel);
_popUpFrame.setLayout(null);
_popUpFrame.setSize(800, 650);
_popUpFrame.setResizable(false);
_popUpFrame.setVisible(true);
}
/** Handles the case when trade is pressed */
private void tradePopUp() {
// Object[] possibleChoices2 = {"Player 2", "Player 1"};
// Object[] possibleChoices3 = {"Player 3", "Player 2", "Player 1"};
// Object[] possibleChoices4 = {"Player 4", "Player 3", "Player 2", "Player 1"};
// int numPlayers = _game.getNumPlayers();
// String question = "Which Player do you want to trade with";
// if (numPlayers == 2) {
// int selectedValue = (int) JOptionPane.showOptionDialog(null, question, "In Jail",
// JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, possibleChoices2, null);
// } else if (numPlayers == 3) {
// int selectedValue = (int) JOptionPane.showOptionDialog(null, question, "In Jail",
// JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, possibleChoices3, null);
// } else {
// int selectedValue = (int) JOptionPane.showOptionDialog(null, quesstion, "In Jail",
// JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, possibleChoices4, null);
// }
}
/** Case when about is pressed */
private void aboutPopUp() {
//TODO
}
/** Case when you upgrade property */
private void houses() {
_popUpFrame = new JFrame("Upgrading/Selling Houses");
_housePanel = new HousePanel(this, _game);
_housePanel.setBounds(0, 0, 800, 650);
_popUpFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
_popUpFrame.add(_housePanel);
_popUpFrame.setLayout(null);
_popUpFrame.setSize(800, 650);
_popUpFrame.setResizable(false);
_popUpFrame.setVisible(true);
}
/** Handles the end turn button. */
private void endTurn() {
Player before = _game.current();
_game.nextPlayer();
Player after = _game.current();
_panel.status().addLine("Player " + before.getID() +
" has ended their turn.");
_panel.status().addLine("Player " + after.getID() + "'s turn.");
_panel.status().repaint();
_panel.players().repaint();
_panel.board().repaint();
_panel.buttons().roll().setText("Roll Dice");
}
}