-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameBoard.java
423 lines (392 loc) · 14.5 KB
/
GameBoard.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
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
import java.awt.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
/**
* Write a description of class board here.
*
* @author Team 3
* @version 1.0
*/
public class GameBoard extends JPanel{
private GamePiece[][] board = new GamePiece[8][10];
protected Laser laser = new Laser();
private int laserSegmentsDrawn = 0;
public static final int tileSize = 50, borderSize = 1;
private int topBorder, leftBorder;
protected String currentPlayer = "white";
protected ArrayList<int[]> whiteOnlyTiles, redOnlyTiles;
/**
* Basic constructor for the GameBoard class initializes the board to
* the classic setup
*/
public GameBoard(){
whiteOnlyTiles = new ArrayList<int[]>();
whiteOnlyTiles.add(new int[]{0,1});
whiteOnlyTiles.add(new int[]{7,1});
whiteOnlyTiles.add(new int[]{0,9});
whiteOnlyTiles.add(new int[]{1,9});
whiteOnlyTiles.add(new int[]{2,9});
whiteOnlyTiles.add(new int[]{3,9});
whiteOnlyTiles.add(new int[]{4,9});
whiteOnlyTiles.add(new int[]{5,9});
whiteOnlyTiles.add(new int[]{6,9});
whiteOnlyTiles.add(new int[]{7,9});
redOnlyTiles = new ArrayList<int[]>();
redOnlyTiles.add(new int[]{0,8});
redOnlyTiles.add(new int[]{7,8});
redOnlyTiles.add(new int[]{0,0});
redOnlyTiles.add(new int[]{1,0});
redOnlyTiles.add(new int[]{2,0});
redOnlyTiles.add(new int[]{3,0});
redOnlyTiles.add(new int[]{4,0});
redOnlyTiles.add(new int[]{5,0});
redOnlyTiles.add(new int[]{6,0});
redOnlyTiles.add(new int[]{7,0});
setBoardClassic();
}
@Override
/**
* Paints the board and handles drawing the laser
*
* @param g The graphics object for the applet
*/
public void paintComponent(Graphics g){
super.paintComponent(g);
topBorder = (this.getHeight()-((tileSize+borderSize)*8))/2;
leftBorder = (this.getWidth()-((tileSize+borderSize)*10))/2;
//draw white only tiles
for(int[] p : whiteOnlyTiles){
g.setColor(new Color(180,180,180));
g.fillRect(leftBorder+(p[1]*(tileSize+borderSize)),
topBorder+(p[0]*(tileSize+borderSize)),tileSize,tileSize);
}
//draw red only tules
for(int[] p : redOnlyTiles){
g.setColor(new Color(255,85,80));
g.fillRect(leftBorder+(p[1]*(tileSize+borderSize)),
topBorder+(p[0]*(tileSize+borderSize)),tileSize,tileSize);
}
//draw grid
for(int row=0; row < 8; row++)
for(int col=0; col < 10; col++){
Graphics2D g2 = (Graphics2D) g;
g2.setStroke(new BasicStroke(borderSize));
g2.setColor(Color.BLACK);
g2.drawRect(leftBorder-borderSize+(col*(tileSize+borderSize)),
topBorder-borderSize+(row*(tileSize+borderSize)),
tileSize+borderSize,
tileSize+borderSize);
}
//draw pieces
for(int row=0; row < 8; row++)
for(int col=0; col < 10; col++){
board[row][col].
setXYpos(leftBorder+(col*(tileSize+borderSize)),
topBorder+(row*(tileSize+borderSize)));
if(!(board[row][col] instanceof NullPiece)){
Image gpImage =
Toolkit.getDefaultToolkit().
getImage(board[row][col].getImage());
g.drawImage(
gpImage, leftBorder+(col*(tileSize+borderSize)),
topBorder+
(row*(tileSize+borderSize)),tileSize,tileSize, this);
}
}
//draw laser
if(laser.showLaser){
ArrayList<Point> points =
getPxPointFromTileList(laser.
getTraversedTiles(board,currentPlayer));
for(int i=0; i < points.size()-1; i++){
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.BLUE);
g2.setStroke(new BasicStroke(3));
g2.drawLine(
(int)points.get(i).getX(),
(int)points.get(i).getY(),
(int)points.get(i+1).getX(),
(int)points.get(i+1).getY()
);
}
}
}
/**
* Method that tells you the piece located at the given coordinates
*
* @param row The row component of the coordinates
* @param col The column component of the coordinates
*
* @return The piece located at the given coordinates
*/
public GamePiece getPiece(int row, int col){
return board[row][col] ;
}
/**
* Method to retrieve the current state of the board array
*
* @return The current board array
*/
public GamePiece[][] getBoard(){
return board;
}
/**
* rotates a given game peice on the board counterclockwise
*
* @param row the row of the peice intended to be rotated
* @param col the column of the peice intended to be rotated
* @return true if successful false if not
*/
public boolean rotatePieceLeft(int row, int col){
if(row >= 0 && row < 8 && col >=0 && col < 10){
board[row][col].rotateLeft();
repaint();
return true;
}else{
return false;
}
}
/**
* rotates a given game peice on the board clockwise
*
* @param row the row of the peice intended to be rotated
* @param col the column of the peice intended to be rotated
* @return true if successful false if not
*/
public boolean rotatePieceRight(int row, int col){
if(row >= 0 && row < 8 && col >=0 && col < 10){
board[row][col].rotateRight();
repaint();
return true;
}else{
return false;
}
}
/**
* Method for piece movement
*
* @param tileA current coordinates of the piece
* @param tileB destination coordinates of the piece
*/
public void movePiece(int[] tileA, int[] tileB){
GamePiece gpA = board[tileA[0]][tileA[1]];
GamePiece gpB = board[tileB[0]][tileB[1]];
board[tileA[0]][tileA[1]] = gpB;
board[tileB[0]][tileB[1]] = gpA;
repaint();
}
/**
* Method for obelisk piece stacking movement
*
* @param tileA current coordinates of the obelisk piece to be moved
* @param tileB location of the stack
*/
public void stackObelisk(int[] tileA, int[] tileB){
Obelisk ob = (Obelisk) board[tileA[0]][tileA[1]];
if(ob.stacked)
((Obelisk)board[tileA[0]][tileA[1]]).stacked = false;
else
board[tileA[0]][tileA[1]] = new NullPiece();
((Obelisk)board[tileB[0]][tileB[1]]).stacked = true;
}
/**
* Method for obelisk piece unstacking movement
*
* @param tileA current coordinates of the obelisk piece to be moved
* @param tileB location of the new lone obelisk
*/
public void unstackObelisk(int[] tileA, int[] tileB){
GamePiece gpA = board[tileA[0]][tileA[1]];
board[tileB[0]][tileB[1]] = new Obelisk(gpA.team,gpA.direction);
((Obelisk)board[tileA[0]][tileA[1]]).stacked = false;
}
/**
* Generates the ArrayList of points the laser will be drawn in
*
* @param tiles contains all the tiles on the board
* @return the coordinates the laser will be drawn on
*/
public ArrayList<Point> getPxPointFromTileList(ArrayList<int[]> tiles){
ArrayList<Point> pointsPx = new ArrayList<Point>();
for(int i=0; i < tiles.size(); i++){
int[] set = tiles.get(i);
Point pt = board[set[0]][set[1]].getCenterPoint();
if(set[0] == 0 && set[1] == 0){
pt = new Point((int)pt.getX(),
(int)pt.getY()-((tileSize/2)-borderSize));
}else if(set[0] == 7 && set[1] == 9){
pt = new Point((int)pt.getX(),
(int)pt.getY()+((tileSize/2)-borderSize));
}
pointsPx.add(pt);
}
if(laser.effect.equals("missed")){
int[] set = tiles.get(tiles.size()-1);
int lastDir = laser.lastDirection;
Point pt = board[set[0]][set[1]].getCenterPoint();
if(lastDir == 0){
pt = new Point((int)pt.getX(),(int)pt.getY()-tileSize);
}
if(lastDir == 90){
pt = new Point((int)pt.getX()+tileSize,(int)pt.getY());
}
if(lastDir == 180){
pt = new Point((int)pt.getX(),(int)pt.getY()+tileSize);
}
if(lastDir == 270){
pt = new Point((int)pt.getX()-tileSize,(int)pt.getY());
}
pointsPx.add(pt);
}
return pointsPx;
}
/**
* Removes a piece from the game board at the given coordinates
*
* @param row The row component of the coordinates
* @param col The column component of the coordinates
*/
public void removePiece(int row, int col){
GamePiece gp = board[row][col];
if(gp instanceof Obelisk){
Obelisk ob = (Obelisk) gp;
if(ob.stacked){
ob.stacked = false;
}else{
board[row][col] = new NullPiece();
}
}else
board[row][col] = new NullPiece();
}
/**
* This method clears all pieces from the board
*/
public void clearBoard(){
for(int row=0; row < 8; row++){
for(int col=0; col < 10; col++){
board[row][col] = new NullPiece();
}
}
}
/**
* Sets the board to the classic setup
*/
public void setBoardClassic(){
clearBoard();
board[3][0] = new Pyramid("red",0);
board[4][0] = new Pyramid("red",90);
board[1][2] = new Pyramid("red",180);
board[0][7] = new Pyramid("red",90);
board[3][7] = new Pyramid("red",90);
board[4][7] = new Pyramid("red",0);
board[5][6] = new Pyramid("red",90);
board[0][4] = new Obelisk("red",0,true);
board[0][6] = new Obelisk("red",0,true);
board[3][4] = new Djed("red",0);
board[3][5] = new Djed("red",90);
board[0][5] = new Pharaoh("red",0);
board[4][9] = new Pyramid("white",180);
board[3][9] = new Pyramid("white",270);
board[6][7] = new Pyramid("white",0);
board[7][2] = new Pyramid("white",270);
board[4][2] = new Pyramid("white",270);
board[3][2] = new Pyramid("white",180);
board[2][3] = new Pyramid("white",270);
board[7][5] = new Obelisk("white",0,true);
board[7][3] = new Obelisk("white",0,true);
board[4][4] = new Djed("white",90);
board[4][5] = new Djed("white",0);
board[7][4] = new Pharaoh("white",0);
}
/**
* Sets the board to the Imhotep setup
*/
public void setBoardImhotep(){
clearBoard();
board[3][0] = new Pyramid("red",0);
board[4][0] = new Pyramid("red",90);
board[2][6] = new Pyramid("red",0);
board[5][6] = new Pyramid("red",90);
board[3][8] = new Pyramid("red",90);
board[4][8] = new Pyramid("red",0);
board[4][5] = new Pyramid("red",270);
board[0][4] = new Obelisk("red",0,true);
board[0][6] = new Obelisk("red",0,true);
board[3][5] = new Djed("red",90);
board[0][7] = new Djed("red",90);
board[0][5] = new Pharaoh("red",0);
board[4][9] = new Pyramid("white",180);
board[3][9] = new Pyramid("white",270);
board[2][3] = new Pyramid("white",270);
board[5][3] = new Pyramid("white",180);
board[4][1] = new Pyramid("white",270);
board[3][1] = new Pyramid("white",180);
board[3][4] = new Pyramid("white",90);
board[7][5] = new Obelisk("white",0,true);
board[7][3] = new Obelisk("white",0,true);
board[4][4] = new Djed("white",90);
board[7][2] = new Djed("white",90);
board[7][4] = new Pharaoh("white",0);
}
/**
* Sets the board to the Dynasty setup
*/
public void setBoardDynasty(){
clearBoard();
board[2][0] = new Pyramid("red",0);
board[3][0] = new Pyramid("red",90);
board[0][4] = new Pyramid("red",180);
board[2][4] = new Pyramid("red",180);
board[4][3] = new Pyramid("red",270);
board[0][6] = new Pyramid("red",90);
board[4][5] = new Pyramid("red",90);
board[0][5] = new Obelisk("red",0,true);
board[2][5] = new Obelisk("red",0,true);
board[3][2] = new Djed("red",0);
board[2][6] = new Djed("red",90);
board[1][5] = new Pharaoh("red",0);
board[5][9] = new Pyramid("white",180);
board[4][9] = new Pyramid("white",270);
board[3][6] = new Pyramid("white",90);
board[5][5] = new Pyramid("white",0);
board[7][5] = new Pyramid("white",0);
board[7][3] = new Pyramid("white",180);
board[3][4] = new Pyramid("white",270);
board[7][4] = new Obelisk("white",0,true);
board[5][4] = new Obelisk("white",0,true);
board[4][7] = new Djed("white",0);
board[5][3] = new Djed("white",90);
board[6][4] = new Pharaoh("white",0);
}
/**
* Allows Players to set the board
*/
public void setBoardCustom(){
clearBoard();
board[0][0] = new Pharaoh("red",0);
board[1][0] = new Djed("red",0);
board[2][0] = new Djed("red",0);
board[3][0] = new Obelisk("red",0,true);
board[4][0] = new Obelisk("red",0,true);
board[5][0] = new Pyramid("red",0);
board[6][0] = new Pyramid("red",0);
board[7][0] = new Pyramid("red",0);
board[2][1] = new Pyramid("red",0);
board[3][1] = new Pyramid("red",0);
board[4][1] = new Pyramid("red",0);
board[5][1] = new Pyramid("red",0);
board[0][9] = new Pharaoh("white",0);
board[1][9] = new Djed("white",0);
board[2][9] = new Djed("white",0);
board[3][9] = new Obelisk("white",0,true);
board[4][9] = new Obelisk("white",0,true);
board[5][9] = new Pyramid("white",0);
board[6][9] = new Pyramid("white",0);
board[7][9] = new Pyramid("white",0);
board[2][8] = new Pyramid("white",0);
board[3][8] = new Pyramid("white",0);
board[4][8] = new Pyramid("white",0);
board[5][8] = new Pyramid("white",0);
}
}