-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameBoard.java
More file actions
358 lines (293 loc) · 8.35 KB
/
Copy pathGameBoard.java
File metadata and controls
358 lines (293 loc) · 8.35 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
/**
* Data structure that maintains the location of each Piece in the game.
*/
public class GameBoard
{
private Square[][] board;
private String[] fileNames = {"a", "b", "c", "d", "e", "f", "g", "h"};
//bottom pieces
Rook bottomLeftRook;
Knight bottomLeftKnight;
Bishop bottomLeftBishop;
Queen bottomQueen;
King bottomKing;
Bishop bottomRightBishop;
Knight bottomRightKnight;
Rook bottomRightRook;
Pawn bottomPawn1;
Pawn bottomPawn2;
Pawn bottomPawn3;
Pawn bottomPawn4;
Pawn bottomPawn5;
Pawn bottomPawn6;
Pawn bottomPawn7;
Pawn bottomPawn8;
//top pieces
Rook topLeftRook;
Knight topLeftKnight;
Bishop topLeftBishop;
Queen topQueen;
King topKing;
Bishop topRightBishop;
Knight topRightKnight;
Rook topRightRook;
Pawn topPawn1;
Pawn topPawn2;
Pawn topPawn3;
Pawn topPawn4;
Pawn topPawn5;
Pawn topPawn6;
Pawn topPawn7;
Pawn topPawn8;
// a8 | b8 | ... | h8
// ___|____|_____|___
// a7 | b7 | ... | h7
// ___|____|_____|___
// ...|... | ... |...
// ___|____|_____|___
// a1 | b1 | ... | h1
// For example, sqaure a8 on the board is represented as board[0][0], b8
// as board[0][1], h7 as board[1][7], etc.
//
// row = rank, column = file
/**
* Constructor for objects of class GameBoard. Initialize all starting
* piece locations.
*/
public GameBoard()
{
board = new Square[8][8];
for(int i = 0; i < 8; i++)
{
for(int j = 0; j < 8; j++)
{
board[i][j] = new Square(8-i, fileNames[j]);
}
}
//set bottom pieces
bottomLeftRook = new Rook("white");
board[7][0].setPiece(bottomLeftRook);
bottomLeftKnight = new Knight("white");
board[7][1].setPiece(bottomLeftKnight);
bottomLeftBishop = new Bishop("white");
board[7][2].setPiece(bottomLeftBishop);
bottomQueen = new Queen("white");
board[7][3].setPiece(bottomQueen);
bottomKing = new King("white");
board[7][4].setPiece(bottomKing);
bottomRightBishop = new Bishop("white");
board[7][5].setPiece(bottomRightBishop);
bottomRightKnight = new Knight("white");
board[7][6].setPiece(bottomRightKnight);
bottomRightRook = new Rook("white");
board[7][7].setPiece(bottomRightRook);
bottomPawn1 = new Pawn("white");
board[6][0].setPiece(bottomPawn1);
bottomPawn2 = new Pawn("white");
board[6][1].setPiece(bottomPawn2);
bottomPawn3 = new Pawn("white");
board[6][2].setPiece(bottomPawn3);
bottomPawn4 = new Pawn("white");
board[6][3].setPiece(bottomPawn4);
bottomPawn5 = new Pawn("white");
board[6][4].setPiece(bottomPawn5);
bottomPawn6 = new Pawn("white");
board[6][5].setPiece(bottomPawn6);
bottomPawn7 = new Pawn("white");
board[6][6].setPiece(bottomPawn7);
bottomPawn8 = new Pawn("white");
board[6][7].setPiece(bottomPawn8);
//set top pieces
topLeftRook = new Rook("black");
board[0][0].setPiece(topRightRook);
topLeftKnight = new Knight("black");
board[0][1].setPiece(topRightKnight);
topLeftBishop = new Bishop("black");
board[0][2].setPiece(topRightBishop);
topQueen = new Queen("black");
board[0][3].setPiece(topQueen);
topKing = new King("black");
board[0][4].setPiece(topKing);
topRightBishop = new Bishop("black");
board[0][5].setPiece(topRightBishop);
topRightKnight = new Knight("black");
board[0][6].setPiece(topRightKnight);
topRightRook = new Rook("black");
board[0][7].setPiece(topRightRook);
topPawn1 = new Pawn("black");
board[1][0].setPiece(topPawn1);
topPawn2 = new Pawn("black");
board[1][1].setPiece(topPawn2);
topPawn3 = new Pawn("black");
board[1][2].setPiece(topPawn3);
topPawn4 = new Pawn("black");
board[1][3].setPiece(topPawn4);
topPawn5 = new Pawn("black");
board[1][4].setPiece(topPawn5);
topPawn6 = new Pawn("black");
board[1][5].setPiece(topPawn6);
topPawn7 = new Pawn("black");
board[1][6].setPiece(topPawn7);
topPawn8 = new Pawn("black");
board[1][7].setPiece(topPawn8);
}
public Piece getPieceAt(int rank, String file)
{
int row = 8 - rank;
int col = getFileNamesIndex(file);
return board[row][col].getPiece();
}
public Square getSquareAbove(Square s){
if(s == null)
{
return null;
}
int row = 8 - s.getRank();
int col = getFileNamesIndex(s.getFile());
if(row - 1 >= 0)
{
return board[row - 1][col];
}
else
{
return null;
}
}
public Square getSquareAbove(Square s, int n) {
if(s == null) {
return null;
}
Square temp = s;
for(int i = 0; i < n; i++) {
temp = getSquareAbove(temp);
if(temp == null) {
return null;
}
}
return temp;
}
public Square getSquareBelow(Square s){
if(s == null)
{
return null;
}
int row = 8 - s.getRank();
int col = getFileNamesIndex(s.getFile());
if(row + 1 < 8)
{
return board[row + 1][col];
}
else
{
return null;
}
}
public Square getSquareBelow(Square s, int n) {
if(s == null) {
return null;
}
Square temp = s;
for(int i = 0; i < n; i++) {
temp = getSquareBelow(temp);
if(temp == null) {
return null;
}
}
return temp;
}
public Square getSquareLeft(Square s){
if(s == null)
{
return null;
}
int row = 8 - s.getRank();
int col = getFileNamesIndex(s.getFile());
if(col - 1 >= 0)
{
return board[row][col - 1];
}
else
{
return null;
}
}
public Square getSquareLeft(Square s, int n) {
if(s == null) {
return null;
}
Square temp = s;
for(int i = 0; i < n; i++) {
temp = getSquareLeft(temp);
if(temp == null) {
return null;
}
}
return temp;
}
public Square getSquareRight(Square s){
if(s == null)
{
return null;
}
int row = 8 - s.getRank();
int col = getFileNamesIndex(s.getFile());
if(col + 1 < 8)
{
return board[row][col + 1];
}
else
{
return null;
}
}
public Square getSquareRight(Square s, int n) {
if(s == null) {
return null;
}
Square temp = s;
for(int i = 0; i < n; i++) {
temp = getSquareRight(temp);
if(temp == null) {
return null;
}
}
return temp;
}
public int getFileNamesIndex(String f){
if(f.equals("a")){
return 0;
}
else if(f.equals("b")){
return 1;
}
else if(f.equals("c")){
return 2;
}
else if(f.equals("d")){
return 3;
}
else if(f.equals("e")){
return 4;
}
else if(f.equals("f")){
return 5;
}
else if(f.equals("g")){
return 6;
}
else{
return 7;
}
}
public void movePiece(Square oldSquare, Square newSquare) {
Piece piece = oldSquare.getPiece();
oldSquare.setPiece(null);
newSquare.setPiece(piece);
}
public void capturePiece(Square attackSquare, Square captureSquare) {
Piece attacker = attackSquare.getPiece();
attackSquare.setPiece(null);
captureSquare.setPiece(null);
captureSquare.setPiece(attacker);
}
}