-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMonopolyDriver.java
More file actions
345 lines (305 loc) · 12 KB
/
Copy pathMonopolyDriver.java
File metadata and controls
345 lines (305 loc) · 12 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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
package IT178Monopoly;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.ArrayList;
public class MonopolyDriver implements ActionListener {
Graphics g;
JFrame f;
Property[] properties;
int score1, playersTurn;
MyCanvas board;
JButton b, c1, b1, b2, b3, b4, b5, b6;
Property prop = new Property();
ArrayList<Player> players = new ArrayList<Player>();
JButton[] buttons = new JButton[6];
String result;
private static JLabel[] comments = new JLabel[5];
private static JTextArea[] playerNames = new JTextArea[4];
Font labelFont;
private MonopolyDriver() {
f = new JFrame();
f.setSize(1024, 900);
f.setLayout(null);
getPlayers();
score1 = 0;
result = " ";
properties = new Property[40];
populateProperty();
board = new MyCanvas();
}
/**
* Draws the board initially. All other updates should be done from
* ActionListener.
*/
private void drawBoard() {
f.add(board);
b = new JButton("Roll");
// c1 = new JButton("Roll");
b1 = new JButton("Chance #1 ?");
b2 = new JButton("Chance #2 ?");
b3 = new JButton("Chance #3 ?");
b4 = new JButton("Community Chest");
b.setBounds(900, 100, 100, 40);
b1.setBounds(900, 150, 100, 40);
b2.setBounds(900, 200, 100, 40);
b3.setBounds(900, 250, 100, 40);
b4.setBounds(900, 300, 200, 40);
b.addActionListener(this);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
f.add(b);
f.add(b1);
f.add(b2);
f.add(b3);
f.add(b4);
f.validate();
f.setVisible(true);
f.repaint();
}
private void getPlayers() {
for (int x = 0; x < playerNames.length; x++) {
playerNames[x] = new JTextArea();
f.add(playerNames[x]);
}
playerNames[0].setBounds(150, 100, 100, 20);
playerNames[1].setBounds(150, 200, 100, 20);
playerNames[2].setBounds(150, 300, 100, 20);
playerNames[3].setBounds(150, 400, 100, 20);
comments[0] = new JLabel("Welcome to Monopoly!");
comments[1] = new JLabel("Name of Player 1");
comments[2] = new JLabel("Name of Player 2");
comments[3] = new JLabel("Name of Player 3");
comments[4] = new JLabel("Name of Player 4");
labelFont = comments[0].getFont();
comments[0].setFont(new Font(labelFont.getName(), Font.PLAIN, 22));
comments[0].setBounds(50, 50, 300, 30);
comments[1].setBounds(60, 80, 150, 30);
comments[2].setBounds(60, 150, 150, 30);
comments[3].setBounds(60, 250, 150, 30);
comments[4].setBounds(60, 380, 150, 30);
f.add(comments[0]);
f.add(comments[1]);
f.add(comments[2]);
f.add(comments[3]);
f.add(comments[4]);
b = new JButton("Done");
b.setBounds(650, 100, 100, 40);
b.addActionListener(this);
f.add(b);
f.validate();
f.setVisible(true);
f.repaint();
}
private void populateProperty() {
properties[0] = new Property("Go", 60, 2,false);
properties[1] = new Property("Mediteranitan Ave.", 60, 4,true);
properties[2] = new Property("Community Chest", 0, 0,false);
properties[3] = new Property("Baltic Ave.", 200, 6,true);
properties[4] = new Property("Income Tax", 0, 30,false);
properties[5] = new Property("Reading Railroad", 100, 50,true);
properties[6] = new Property("Oriental Ave.", 100, 6,true);
properties[7] = new Property("Chance 1", 0, 0,false);
properties[8] = new Property("Vermont Ave.", 100, 6,true);
properties[9] = new Property("Connecticut Ave.", 120, 8,true);
properties[10] = new Property("Jail", 0, 0,false);
properties[11] = new Property("St. Charles Place", 140, 10,true);
properties[12] = new Property("Electric Company", 150, 20,true);
properties[13] = new Property("States Ave.", 140, 10,true);
properties[14] = new Property("Virginia Ave.", 160, 12,true);
properties[15] = new Property("Pennsylvania Railroad", 200, 50,true);
properties[16] = new Property("St. James Place", 180, 14,true);
properties[17] = new Property("Community Chest", 0, 0,false);
properties[18] = new Property("Tennessee Ave.", 180, 14,true);
properties[19] = new Property("New York Ave.", 200, 16,true);
properties[20] = new Property("Free Parking", 0, 0,false);
properties[21] = new Property("Kentucky Ave.", 220, 18,true);
properties[22] = new Property("Chance 2", 0, 0,false);
properties[23] = new Property("Indiana Ave.", 220, 18,true);
properties[24] = new Property("Illinois Ave.", 240, 20,true);
properties[25] = new Property("B & O Railroad", 200, 50,true);
properties[26] = new Property("Atlantic Ave.", 260, 22,true);
properties[27] = new Property("Ventnor Ave.", 260, 22,true);
properties[28] = new Property("Water Works", 150, 30,true);
properties[29] = new Property("Marvin Gardens", 280, 24,true);
properties[30] = new Property("Go To Jail", 0, 0,false);
properties[31] = new Property("Pacific Ave.", 300, 26,true);
properties[32] = new Property("North Carolina Ave.", 300, 26,true);
properties[33] = new Property("Community Chest", 0, 0,false);
properties[34] = new Property("Pennsylvania Ave.", 320, 28,true);
properties[35] = new Property("Short Line", 200, 50,true);
properties[36] = new Property("Chance 3", 0, 0,false);
properties[37] = new Property("Park Place", 350, 35,true);
properties[38] = new Property("Luxury Tax", 0, 75,false);
properties[39] = new Property("Boardwalk", 400, 50,true);
}
/**
* Button listener e is object that was accessed, cast it to whatever it
* needs to be (JButton)
*/
public void actionPerformed(ActionEvent e) {
JButton objectPressed = (JButton) e.getSource();
// If in the intro screen
if (objectPressed.getText() == "Done") {
f.remove(b);
for (int x = 0; x < playerNames.length; x++) {
if (!playerNames[x].getText().equals("")) {
players.add(new Player(playerNames[x].getText()));
}
System.out.println("location:" + players.get(0).getName());
f.remove(playerNames[x]);
}
for (int x = 0; x < comments.length; x++) {
f.remove(comments[x]);
}
drawBoard();
}
// if in the main screen
if (objectPressed.getText() == "Roll") {
int roll = ((int) (Math.random() * 6) + 1) + ((int) (Math.random() * 6) + 1);
players.get(playersTurn).move(roll);
result = properties[players.get(playersTurn).getLocation()].landedOn(players.get(playersTurn));
if (players.get(playersTurn).getCash() < 0) { // remove player if
// broke
result += " " + players.get(playersTurn).getName() + " has gone broke!";
for (Property property : properties) { //remove ownership of all properties owned by broke player
if (property.ownedBy() == players.get(playersTurn).getName()) {
property.removeOwner();
}
}
players.remove(players.get(playersTurn));
}
if (playersTurn >= players.size() - 1) { // on last player's turn
// revert back to player
// 1
playersTurn = 0;
} else {
playersTurn++;
}
}
// Calls chance method when buttons are pressed.
if (objectPressed.getText() == "Chance #1 ?"|| objectPressed.getText() == "Chance #2 ?"|| objectPressed.getText() == "Chance #3 ?"||
objectPressed.getText() == "Community Chests") {
prop.chance(players.get(playersTurn));
}
f.revalidate();
board.revalidate();
board.repaint();
}
private void run() {
/*
* Main run program. In an event driven program like this, it's not necessarily needed.
*/
}
public static void main(String[] args) {
MonopolyDriver play = new MonopolyDriver();
play.run();
}
// board class.
private class MyCanvas extends Canvas {
static final long serialVersionUID = 999;
public MyCanvas() {
setBackground(Color.GRAY);
setSize(900, 900);
}
public void paint(Graphics g) {
Color newGreen = new Color(204,255,229);
setBackground(Color.WHITE);
g.setFont(new Font("TimesRoman", Font.PLAIN, 15));
for (int x = 0; x < 11; x++) {
g.setColor(newGreen);
g.fillRect(10 + (80 * x), 20, 80, 75);
g.fillRect(10 + (80 * x), 725, 80, 75);
g.setColor(Color.BLACK);
g.drawRect(10 + (80 * x), 20, 80, 75); // left side
g.drawRect(10 + (80 * x), 725, 80, 75); // right side
// Draw spaces 20-30
if (properties[x + 20].getName().length() < 10) {
g.drawString(properties[x + 20].getName(), 11 + (x * 80), 45);
} else {
g.drawString(
properties[x + 20].getName().substring(0, properties[x + 20].getName().lastIndexOf(" ")),
11 + (x * 80), 45);
g.drawString(
properties[x + 20].getName().substring(properties[x + 20].getName().lastIndexOf(" ") + 1),
11 + (x * 80), 57);
}
if (properties[x+20].ownedBy() != "unowned")
g.drawString(properties[x + 20].ownedBy(), 11 + (x * 80), 93);
// Draw first 10 spaces
if (properties[10 - x].getName().length() < 10) {
g.drawString(properties[10 - x].getName(), 11 + (x * 80), 747);
} else {
g.drawString(
properties[10 - x].getName().substring(0, properties[10 - x].getName().lastIndexOf(" ")),
11 + (x * 80), 747);
g.drawString(
properties[10 - x].getName().substring(properties[10 - x].getName().lastIndexOf(" ") + 1),
11 + (x * 80), 767);
}
if (properties[10 - x].ownedBy() != "unowned")
g.drawString(properties[10 - x].ownedBy(), 11 + (x * 80), 798);
}
for (int x = 0; x < 9; x++) {
g.setColor(newGreen);
g.fillRect(10, 95 + (70 * x), 80, 70);
g.fillRect(810, 95 + (70 * x), 80, 70);
g.setColor(Color.BLACK);
g.drawRect(10, 95 + (70 * x), 80, 70); // top row
g.drawRect(810, 95 + (70 * x), 80, 70); // bottom row
// Draw spaces 11-19
if (properties[19 - x].getName().length() < 10)
g.drawString(properties[19 - x].getName(), 10, 120 + (x * 70));
else {
g.drawString(
properties[19 - x].getName().substring(0, properties[19 - x].getName().lastIndexOf(' ')),
10, 120 + (x * 70));
g.drawString(
properties[19 - x].getName().substring(properties[19 - x].getName().lastIndexOf(' ') + 1),
10, 132 + (x * 70));
}
if (properties[19 - x].ownedBy() != "unowned")
g.drawString(properties[19 - x].ownedBy(), 10, 163 + (x * 70));
// Draw spaces 31-38
if (properties[31 + x].getName().length() < 10)
g.drawString(properties[31 + x].getName(), 810, 120 + (x * 70));
else {
g.drawString(
properties[31 + x].getName().substring(0, properties[31 + x].getName().lastIndexOf(' ')),
810, 120 + (x * 70));
g.drawString(
properties[31 + x].getName().substring(properties[31 + x].getName().lastIndexOf(' ') + 1),
810, 132 + (x * 70));
}
if (properties[31 + x].ownedBy() != "unowned")
g.drawString(properties[31 + x].ownedBy(), 810, 163 + (x * 70));
}
// Display the location of the players
g.setColor(Color.RED);
for (int x = 0; x < players.size(); x++) {
g.drawString((x + 1) + ": " + players.get(x).getName() + " $" + players.get(x).getCash(), 200,
350 + (x * 15));
if (players.get(x).getLocation() < 11)
g.drawString("" + (x + 1), 810 - (players.get(x).getLocation() * 80) + (15 * x), 780); // bottom
// row
else if (players.get(x).getLocation() < 20)
g.drawString("" + (x + 1), 10 + (15 * x), 780 - ((players.get(x).getLocation() - 10) * 70)); // left
// side
else if (players.get(x).getLocation() < 30)
g.drawString("" + (x + 1), ((players.get(x).getLocation() - 20) * 80) + 10 + (15 * x), 80);// top
// row
else
g.drawString("" + (x + 1), 820 + (15 * x), ((players.get(x).getLocation() - 30) * 70) + 80);
}
g.setFont(new Font("TimesRoman", Font.PLAIN, 20));
if (!result.equals("OK")) {
g.drawString(result, 100, 200);
}
System.out
.println(players.get(playersTurn).getName() + "location" + players.get(playersTurn).getLocation());
g.setColor(Color.BLACK);
}
}
}