-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuildDialogue.java
More file actions
574 lines (454 loc) · 16.3 KB
/
BuildDialogue.java
File metadata and controls
574 lines (454 loc) · 16.3 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
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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
/**
* Open a Dialogue menu to select where the user wants to build
*/
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.Dialog;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Graphics;
import java.awt.Dimension;
import java.io.IOException;
public class BuildDialogue extends JPanel implements ActionListener {
JDialog dialogue;
int mode;
Texture background = new Texture("img/buildbackground.png");
Board board;
// which player is building
int player;
// the parent so we can reference player info
Catan parent;
// the result will be saved here so wherever this class was created from can
// read it
int[] result = { -1, -1 };
public BuildDialogue(JFrame frame, int _mode, Board _board, int _player, Catan _parent) throws IOException {
super();
// set mode to whatever was provided
mode = _mode;
// and the reference to the board
board = _board;
// and the player number
player = _player;
// and the parent
parent = _parent;
// create the dialogue for configuring
dialogue = new JDialog(frame, "Player " + player + ": Build", Dialog.ModalityType.DOCUMENT_MODAL);
dialogue.setSize(416, 438);
// add this panel to the dialogue to draw to
dialogue.add(this);
// make the dialogue NOT resizable so the positioning works
dialogue.setResizable(false);
setOpaque(true);
setPreferredSize(new Dimension(200, 200));
// set layout manager to NONE so that buttons can be absolutely positioned
setLayout(null);
// depending on the mode, set different button positions
switch (mode) {
// if mode is zero, add road positions
case 0:
addRoadButtons(false);
break;
// if mode is one, add settlement buttons
case 1:
addSettlementButtons(false);
break;
// if mode is two, add settlement buttons but it's cities this time
case 2:
addSettlementButtons(false);
break;
// case three is initializing the settlements, so use free build mode
case 3:
addSettlementButtons(true);
break;
// case four is for building a road beside the latest settlement built
case 4:
addRoadButtons(true);
break;
// case five is for moving the robber
case 5:
addRobberButtons();
break;
// finally, case 6 is upgrading to a city
case 6:
addCityButtons();
break;
}
dialogue.setVisible(true);
}
/**
* check to see if the road can be built, based on whether or not it's bordering
* another row
*/
public boolean isRoadBuildable(int x, int y, int player) {
// IMMEDIATELY, if the road is already built on, return false
if (board.roads[y][x] != 0) {
return false;
}
if (y % 2 == 0) {
// if y is even, check whether the roads beside border it,
// and the roads above or below at either one of its intersections
// is there a road to the right?
if (x + 1 < board.roads[y].length && board.roads[y][x + 1] == player) {
return true;
}
// is there a road to the left?
if (x > 0 && board.roads[y][x - 1] == player) {
return true;
}
// is there a road below?
int roundedX;
// it needs to be rounded differently depending on whether it's above or below
// halfway
if (y < 6) {
roundedX = Math.round(x / (float) 2.0);
} else {
// this is the same as flooring the result, integer division just ignores the
// decimal
roundedX = x / 2;
}
if (y + 1 < board.roads.length && roundedX < board.roads[y + 1].length
&& board.roads[y + 1][roundedX] == player) {
return true;
}
// how about above?
// it needs to be rounded differently depending on whether it's above or below
// halfway
if (y > 5) {
roundedX = Math.round(x / (float) 2.0);
} else {
// this is the same as flooring the result, integer division just ignores the
// decimal
roundedX = x / 2;
}
if (y > 0 && roundedX < board.roads[y - 1].length
&& board.roads[y - 1][roundedX] == player) {
return true;
}
} else {
// for odd rows, we're looking at the vertical roads
// possible locations for borders are top left, top right,
// bottom left, and bottom right.
// first, check whether the two above have roads
// the formula differs for the top half and bottom half because they have
// different offsets
int roundedX;
if (y < 6) {
roundedX = x * 2 - 1;
} else {
roundedX = x * 2;
}
// now check top left
if (y > 0 && roundedX >= 0 && roundedX < board.roads[y - 1].length
&& board.roads[y - 1][roundedX] == player) {
return true;
}
// and top right
if (y > 0 && roundedX + 1 < board.roads[y - 1].length
&& board.roads[y - 1][roundedX + 1] == player) {
return true;
}
// finally, check whether there are roads below
if (y >= 5) {
roundedX = x * 2 - 1;
} else {
roundedX = x * 2;
}
// check bottom left
if (y > 0 && roundedX >= 0 && roundedX < board.roads[y + 1].length
&& board.roads[y + 1][roundedX] == player) {
return true;
}
// and bottom right
if (y > 0 && roundedX + 1 < board.roads[y + 1].length
&& board.roads[y + 1][roundedX + 1] == player) {
return true;
}
}
return false;
}
/**
* in free build, if the road is beside the newly created settlement, return
* false
*/
public boolean isRoadBorderingSettlement(int x, int y, int player) {
// get the last build (will be the previous settlement) from the player
int[] lastBuild = parent.players[player - 1].latestBuild;
int bx = lastBuild[0];
int by = lastBuild[1];
// is this the right of the settlement?
if (y % 2 == 0 && y / 2 == by && x == bx) {
return true;
}
// how about the left?
if (y % 2 == 0 && y / 2 == by && x + 1 == bx) {
return true;
}
// now, depending on where in the board, check above and below
if (y % 2 == 1) {
// there is a potential settlement spot above and below
// check above
// first top half
if (y < 6 && y / 2 == by && x * 2 == bx) {
return true;
}
// then bottom half
else if (y >= 6 && y / 2 == by && x * 2 + 1 == bx) {
return true;
}
// now check below
// first top half
if (y < 5 && y / 2 + 1 == by && x * 2 + 1 == bx) {
return true;
}
// then bottom half
else if (y >= 5 && y / 2 + 1 == by && x * 2 == bx) {
return true;
}
}
// AND THAT'S IT!!!!!!!!!!
// WOOOOO!!!!
// Building detection all finished :)
// if nothing's been found yet, it's not a valid build space.
return false;
}
/**
* check whether that position already has a settlement, and if it's a valid
* place for this player to build
*
* Free Build refers to the start of the game when a settlement is able to be
* placed anywhere. During most gameplay, free build is disabled.
*/
public boolean isSettlementBuildable(int x, int y, int player, boolean freeBuild) {
// check whether this space is already filled
if (board.settlements[y][x] != 0 || board.cities[y][x] != 0) {
return false;
}
// now check whether any of the bordering spaces are already taken, in which
// case it can't be built there
// check right
if (x + 1 < board.settlements[y].length && board.settlements[y][x + 1] != 0) {
return false;
}
// and left
if (x > 0 && board.settlements[y][x - 1] != 0) {
return false;
}
// and above IF above is connected
if (y > 0) {
// different methods depending on whether we're in the top or bottom halves
if (x % 2 == 1 && y < 3) {
// top half so the row above has two less settlements
if (x != 0 && x - 1 < board.settlements[y - 1].length && board.settlements[y - 1][x - 1] != 0) {
return false;
}
} else if (x % 2 == 0 && y == 3) {
// middle row so row above is the same length
if (x < board.settlements[y - 1].length && board.settlements[y - 1][x] != 0) {
return false;
}
} else if (x % 2 == 0 && y > 3 && y < 6) {
// bottom half so the row above has two more settlements
if (x + 1 < board.settlements[y - 1].length && board.settlements[y - 1][x + 1] != 0) {
return false;
}
}
}
// FINALLY, check below IF connected
if (y + 1 < board.settlements.length) {
// different methods depending on whether we're in the top or bottom halves
if (x % 2 == 0 && y < 2 && y >= 0) {
// top half so the row below has two more settlements
if (x + 1 < board.settlements[y + 1].length && board.settlements[y + 1][x + 1] != 0) {
return false;
}
} else if (x % 2 == 0 && y == 2) {
// middle row so row below is the same length
if (x < board.settlements[y + 1].length && board.settlements[y + 1][x] != 0) {
return false;
}
} else if (x % 2 == 1 && y > 2) {
// bottom half so the row below has two less settlements
if (x != 0 && x - 1 < board.settlements[y + 1].length && board.settlements[y + 1][x - 1] != 0) {
return false;
}
}
}
// now that we've made sure each settlement is at least 2 away from the other,
// make sure it's connected to a road. BUT ONLY IF free build is disabled.
if (freeBuild) {
// if free build mode is on and the settlement is at least 2 away from any
// other, we're good to go this is a valid space
return true;
}
// if we're still going here, free build is NOT on and we have to make sure
// roads are connected to the settlement
if (y >= 0 && y < 6) {
// check to the left
if (x > 0 && x < board.roads[y * 2].length && board.roads[y * 2][x - 1] == player) {
return true;
}
// check to the right
if (x >= 0 && x < board.roads[y * 2].length && board.roads[y * 2][x] == player) {
return true;
}
// now the part that depends on top or bottom half
// IF the spot is connected by a road on the bottom half, check it
if (y < 3 && x % 2 == 0) {
// it is in fact connected by a road
if (x >= 0 && x < board.settlements[y].length && board.roads[y * 2 + 1][(int) Math.ceil(x / 2.0)] == player) {
return true;
}
}
if (y >= 3 && x % 2 == 1 && y < 5) {
if (x >= 0 && x < board.settlements[y].length && board.roads[y * 2 + 1][x / 2] == player) {
return true;
}
}
// ok now if it's connected by a road above!!
if (y > 0 && y < 3 && x % 2 == 1) {
if (x >= 0 && x < board.settlements[y].length && board.roads[y * 2 - 1][x / 2] == player) {
return true;
}
}
if (y >= 3 && x % 2 == 0) {
if (x >= 0 && x < board.settlements[y].length && board.roads[y * 2 - 1][(int) Math.ceil(x / 2.0)] == player) {
return true;
}
}
}
return false;
}
/**
* Add buttons for building roads
*/
public void addRoadButtons(boolean freeBuild) {
// create buttons for each possible road space
// create a list of how many roads there are per row
int[] roadsPerRow = { 6, 4, 8, 5, 10, 6, 10, 5, 8, 4, 6 };
// how much to offset each row on the x axis
int[] rowOffset = { 100, 85, 70, 50, 35, 15, 35, 50, 70, 85, 100 };
for (int y = 0; y < 11; y++) {
for (int x = 0; x < roadsPerRow[y]; x++) {
JButton button = new JButton("+");
button.setBounds(rowOffset[y] + x * (35 + y % 2 * 35), 40 + y * 30, 20, 20);
// set the action command to the coordinates where to build so it may be figured
// out in the action listener
button.setActionCommand(x + "," + y);
button.addActionListener(this);
// determine whether this is a valid space to build on
// use different function depending on whether or not it's free build mode
if (freeBuild) {
button.setEnabled(isRoadBorderingSettlement(x, y, player));
} else {
button.setEnabled(isRoadBuildable(x, y, player));
}
add(button);
}
}
}
/**
* Add buttons for building a road or settlement
*/
public void addSettlementButtons(boolean freeBuild) {
// create buttons for each possible settlement space
// create a list of how many settlements there are per row
int[] settlementsPerRow = { 7, 9, 11, 11, 9, 7 };
// how much to offset each row on the x axis
int[] rowOffset = { 90, 55, 20, 20, 55, 90 };
for (int y = 0; y < 6; y++) {
for (int x = 0; x < settlementsPerRow[y]; x++) {
JButton button = new JButton("+");
// if the column is odd, offset the y position
// if y > 2, the offset should be flipped
int yOffset;
if (y < 3) {
yOffset = x % 2 * -20;
} else {
yOffset = (x + 1) % 2 * -20;
}
button.setBounds(rowOffset[y] + x * 35, 40 + yOffset + y * 65, 20, 20);
// set the action command to the coordinates where to build so it may be figured
// out in the action listener
button.setActionCommand(x + "," + y);
button.addActionListener(this);
button.setEnabled(isSettlementBuildable(x, y, player, freeBuild));
add(button);
}
}
}
/**
* Add buttons for building a road or settlement
*/
public void addCityButtons() {
// create buttons for each possible settlement space
// create a list of how many settlements there are per row
int[] settlementsPerRow = { 7, 9, 11, 11, 9, 7 };
// how much to offset each row on the x axis
int[] rowOffset = { 90, 55, 20, 20, 55, 90 };
for (int y = 0; y < 6; y++) {
for (int x = 0; x < settlementsPerRow[y]; x++) {
JButton button = new JButton("+");
// if the column is odd, offset the y position
// if y > 2, the offset should be flipped
int yOffset;
if (y < 3) {
yOffset = x % 2 * -20;
} else {
yOffset = (x + 1) % 2 * -20;
}
button.setBounds(rowOffset[y] + x * 35, 40 + yOffset + y * 65, 20, 20);
// set the action command to the coordinates where to build so it may be figured
// out in the action listener
button.setActionCommand(x + "," + y);
button.addActionListener(this);
button.setEnabled(parent.board.settlements[y][x] == player);
add(button);
}
}
}
/**
* Add buttons for moving the robber
*/
public void addRobberButtons() {
dialogue.setTitle("Move Robber");
// create buttons for each possible settlement space
// create a list of how many settlements there are per row
int[] tilesPerRow = { 3, 4, 5, 4, 3 };
// how much to offset each row on the x axis
int[] tileOffset = { 110, 75, 40, 75, 110 };
for (int y = 0; y < 5; y++) {
for (int x = 0; x < tilesPerRow[y]; x++) {
JButton button = new JButton("+");
button.setBounds(tileOffset[y] + x * 70, 50 + y * 65, 40, 40);
// set the action command to the coordinates where to build so it may be figured
// out in the action listener
button.setActionCommand(x + "," + y);
button.addActionListener(this);
button.setEnabled(x != parent.robberLocation[0] || y != parent.robberLocation[1]);
add(button);
}
}
}
@Override
public void paintComponent(Graphics gl) {
// when its painted, just draw the picture
gl.drawImage(background.img, 0, 0, getWidth(), getHeight(), null, null);
}
/**
* Handle button presses
*/
@Override
public void actionPerformed(ActionEvent event) {
// a button has been pressed! Figure out its x and y positions
String[] coordStrings = event.getActionCommand().split(",");
// now get the actual coordinates
int x = Integer.parseInt(coordStrings[0]);
int y = Integer.parseInt(coordStrings[1]);
result[0] = x;
result[1] = y;
// now we can close the dialogue :)
dialogue.dispose();
}
}