-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCreateAndRunGame.java
379 lines (329 loc) · 10 KB
/
CreateAndRunGame.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
package monopoly;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.*;
public class CreateAndRunGame implements ActionListener, Runnable {
private JFrame frame;
private JPanel mainPanel;
private MainBoardPanel mbp;
private JPanel btm;
private JPanel lft;
private JPanel tp;
private JPanel rt;
private int resX;
private int resY;
private int players;
private int computers;
private boolean doubles;
private int diceRoll;
private int hr1;
private int hr2;
private int hr3;
private int hr4;
private int hr5;
private int hr6;
private int hr7;
private GamePiece currentPlayer;
private int timeToRoll = 0;
private JButton roll;
private JButton trade;
private JButton property;
private JButton saveAndQuit;
Thread th;
public CreateAndRunGame(String resolution, int players, int computers, int hr1, int hr2, int hr3, int hr4, int hr5, int hr6, int hr7){ //New Game
if(resolution.equals("1920x1080")){
resX = 1920;
resY = 1030;
}else{
String[] temp = resolution.split("x");
resX = Integer.parseInt(temp[0]);
resY = Integer.parseInt(temp[1]);
}
this.players = players;
this.computers = computers;
this.hr1 = hr1;
this.hr2 = hr2;
this.hr3 = hr3;
this.hr4 = hr4;
this.hr5 = hr5;
this.hr6 = hr6;
this.hr7 = hr7;
frame = new JFrame();
frame.setSize(resX, resY);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Monopoly");
frame.setVisible(true);
mainPanel = new JPanel();
mainPanel.setBackground(Color.WHITE);
mainPanel.setLayout(new GridBagLayout());
GridBagConstraints top = new GridBagConstraints();
top.fill = GridBagConstraints.BOTH;
top.gridwidth = 4;
top.gridx = 0;
top.gridy = 0;
top.weightx = 1;
top.weighty = .10;
top.anchor = GridBagConstraints.NORTH;
GridBagConstraints bl = new GridBagConstraints();
bl.fill = GridBagConstraints.BOTH;
bl.gridwidth = 3;
bl.gridx = 0;
bl.gridy = 2;
bl.weightx = .90;
bl.weighty = .15;
bl.anchor = GridBagConstraints.SOUTH;
GridBagConstraints br = new GridBagConstraints();
br.fill = GridBagConstraints.BOTH;
br.gridwidth = 1;
br.gridx = 3;
br.gridy = 2;
br.weightx = .10;
br.weighty = .15;
br.anchor = GridBagConstraints.SOUTH;
GridBagConstraints cent = new GridBagConstraints();
cent.fill = GridBagConstraints.BOTH;
cent.gridwidth = 2;
cent.gridx = 1;
cent.gridy = 1;
cent.weightx = .80;
cent.weighty = .75;
cent.anchor = GridBagConstraints.CENTER;
GridBagConstraints left = new GridBagConstraints();
left.fill = GridBagConstraints.BOTH;
left.gridx = 0;
left.gridy = 1;
left.weightx = .1;
left.weighty = .80;
left.anchor = GridBagConstraints.WEST;
GridBagConstraints right = new GridBagConstraints();
right.fill = GridBagConstraints.BOTH;
right.gridx = 3;
right.gridy = 1;
right.weightx = .10;
right.weighty = .80;
right.anchor = GridBagConstraints.EAST;
tp = new JPanel();
tp.setBackground(Color.YELLOW);
lft = new JPanel();
lft.setBackground(Color.GREEN);
mbp = new MainBoardPanel(resX, resY, this.players, this.computers, frame);
mbp.setBackground(Color.WHITE);
rt = new JPanel();
rt.setBackground(Color.BLUE);
btm = new JPanel();
btm.setBackground(Color.RED);
JPanel o = new JPanel();
o.setBackground(Color.ORANGE);
mainPanel.add(tp, top);
mainPanel.add(lft, left);
mainPanel.add(mbp, cent);
mainPanel.add(rt, right);
mainPanel.add(btm, bl);
mainPanel.add(o, br);
int tempw = o.getWidth();
int temph = o.getHeight();
o.setPreferredSize(new Dimension(tempw, temph));
o.setMaximumSize(new Dimension(tempw, temph));
o.setMinimumSize(new Dimension(tempw, temph));
o.setBackground(Color.BLACK);
roll = new JButton("Roll");
roll.setActionCommand("r");
roll.addActionListener(this);
roll.setEnabled(false);
trade = new JButton("Trade");
trade.setActionCommand("t");
trade.addActionListener(this);
property = new JButton("Property");
property.setActionCommand("p");
property.addActionListener(this);
saveAndQuit = new JButton("Save/Quit");
saveAndQuit.setActionCommand("S+Q");
saveAndQuit.addActionListener(this);
if(resX == 1920){
roll.setPreferredSize(new Dimension(150, 30));
trade.setPreferredSize(new Dimension(150, 30));
property.setPreferredSize(new Dimension(150, 30));
saveAndQuit.setPreferredSize(new Dimension(150, 30));
}else if(resX == 1280){
roll.setPreferredSize(new Dimension(100, 30));
trade.setPreferredSize(new Dimension(100, 30));
property.setPreferredSize(new Dimension(100, 30));
saveAndQuit.setPreferredSize(new Dimension(100, 30));
}else if(resX == 1024){
roll.setPreferredSize(new Dimension(100, 20));
trade.setPreferredSize(new Dimension(100, 20));
property.setPreferredSize(new Dimension(100, 20));
saveAndQuit.setPreferredSize(new Dimension(100, 20));
}else{
roll.setPreferredSize(new Dimension(85, 15));
trade.setPreferredSize(new Dimension(85, 15));
property.setPreferredSize(new Dimension(85, 15));
saveAndQuit.setPreferredSize(new Dimension(88, 15));
}
o.add(roll);
o.add(trade);
o.add(property);
o.add(saveAndQuit);
frame.add(mainPanel);
frame.setContentPane(mainPanel);
frame.setVisible(true);
Thread th = new Thread(this);
th.start();
roll.setEnabled(true);
}
public CreateAndRunGame(String filePath){ //Load Game
}
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("r")){
if(timeToRoll == 1){
synchronized (this) {
diceRoll = mbp.rollDice();
doubles = mbp.checkForDoubles();
this.notify();
roll.setEnabled(false);
}
}
}else if(e.getActionCommand().equals("t")){
}else if(e.getActionCommand().equals("p")){
}else if(e.getActionCommand().equals("S+Q")){
Object[] sqOptions = new Object[]{"Save and Quit", "Just Save", "Just Quit", "Cancel"};
int sqo = JOptionPane.showOptionDialog(null, "What would you like to do?", "Save and Quit?", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE,null, sqOptions, null);
if(sqo != 3 && sqo != 1){
if(sqo == 0){
}else if(sqo == 1){
}else if(sqo == 2){
System.exit(0);
}
}
}else{
}
}
public void run() {
ArrayList<Player> players = mbp.getPlayers();
ArrayList<Computer> computers = mbp.getComputers();
ArrayList<GamePiece> gamePieces = new ArrayList<GamePiece>();
for(int i = 0; i < players.size(); i++){
gamePieces.add(players.get(i));
}
for(int i = 0; i < computers.size(); i++){
gamePieces.add(computers.get(i));
}
do{
for(int i = 0; i < gamePieces.size(); i++){
currentPlayer = gamePieces.get(i);
for(int jj = 0; jj < gamePieces.size(); jj++){
if(gamePieces.size() == 2){
lft.setBackground(Color.BLACK);
tp.setBackground(Color.BLACK);
btm.setBackground(currentPlayer.getColor());
if(jj == 0){
if(gamePieces.get(jj + 1).equals(currentPlayer)){
}else{
rt.setBackground(gamePieces.get(jj + 1).getColor());
}
}else{
if(gamePieces.get(jj - 1).equals(currentPlayer)){
}else{
rt.setBackground(gamePieces.get(jj - 1).getColor());
}
}
}else if(gamePieces.size() == 3){
lft.setBackground(Color.BLACK);
btm.setBackground(currentPlayer.getColor());
if(jj == 0){
if(gamePieces.get(jj).equals(currentPlayer)){
rt.setBackground(gamePieces.get(jj + 1).getColor());
tp.setBackground(gamePieces.get(jj + 2).getColor());
}
}else if(jj == 1){
if(gamePieces.get(jj).equals(currentPlayer)){
rt.setBackground(gamePieces.get(jj + 1).getColor());
tp.setBackground(gamePieces.get(jj - 1).getColor());
}
}else{
if(gamePieces.get(jj).equals(currentPlayer)){
rt.setBackground(gamePieces.get(jj - 2).getColor());
tp.setBackground(gamePieces.get(jj - 1).getColor());
}
}
}else if(gamePieces.size() == 4){
btm.setBackground(currentPlayer.getColor());
if(jj == 0){
if(gamePieces.get(jj).equals(currentPlayer)){
rt.setBackground(gamePieces.get(jj + 1).getColor());
tp.setBackground(gamePieces.get(jj + 2).getColor());
lft.setBackground(gamePieces.get(jj + 3).getColor());
}
}else if(jj == 1){
if(gamePieces.get(jj).equals(currentPlayer)){
rt.setBackground(gamePieces.get(jj + 1).getColor());
tp.setBackground(gamePieces.get(jj + 2).getColor());
lft.setBackground(gamePieces.get(jj - 1).getColor());
}
}else if(jj == 2){
if(gamePieces.get(jj).equals(currentPlayer)){
rt.setBackground(gamePieces.get(jj + 1).getColor());
tp.setBackground(gamePieces.get(jj - 2).getColor());
lft.setBackground(gamePieces.get(jj - 1).getColor());
}
}else if(jj == 3){
if(gamePieces.get(jj).equals(currentPlayer)){
rt.setBackground(gamePieces.get(jj - 3).getColor());
tp.setBackground(gamePieces.get(jj - 2).getColor());
lft.setBackground(gamePieces.get(jj - 1).getColor());
}
}
}
}
if(gamePieces.get(i) instanceof Player){
roll.setEnabled(true);
synchronized (this) {
try {
timeToRoll = 1;
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
timeToRoll = 0;
mbp.move(currentPlayer, diceRoll);
mbp.turnOperations(currentPlayer, this);
}else{
roll.setEnabled(true);
timeToRoll = 1;
roll.doClick();
timeToRoll = 0;
mbp.move(currentPlayer, diceRoll);
mbp.turnOperations(currentPlayer, this);
}
}
}while(true);
}
public int getHr1(){
return hr1;
}
public int getHr2(){
return hr2;
}
public int getHr3(){
return hr3;
}
public int getHr4(){
return hr4;
}
public int getHr5(){
return hr5;
}
public int getHr6(){
return hr6;
}
public int getHr7(){
return hr7;
}
}