-
Notifications
You must be signed in to change notification settings - Fork 1
/
Board.java
285 lines (272 loc) · 7.61 KB
/
Board.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
import java.util.*;
public class Board {
protected int grid_size; // A variable to store the
protected Piece pieces[]; // Array List to store all 32 pieces in the
public int grid_matrix[][]; // The grid of the board
public void board_construct(int size)
{
createPieces();
this.grid_size = 8; // by default. On changing to custom, make this.grid_size = size
this.grid_matrix = new int[8][8];
for (int i = 0; i < 2; i++) // height parameter
{
for (int j = 0; j < 8; j++) // width parameter
{
this.grid_matrix[j][i] = 1;
this.grid_matrix[j][8 - i -1] = 1;
}
}
}
public void createPieces()
{
pieces = new Piece[32];
//pieces.ensureCapacity(32);
for (int i = 0; i < 8; i++)
{
Pawn pawn_white = new Pawn(0, i, 1);
pieces[2*i] = pawn_white;
Pawn pawn_black = new Pawn(1, i, 6);
pieces[2*i + 1] = pawn_black;
}
Rook rook_white1 = new Rook(0, 0, 0);
Rook rook_white2 = new Rook(0, 7, 0);
pieces[16] = rook_white1;
pieces[18] = rook_white2;
Rook rook_black1 = new Rook(1, 0, 7);
Rook rook_black2 = new Rook(1, 7, 7);
pieces[17] = rook_black1;
pieces[19] = rook_black2;
Knight knight_white1 = new Knight(0, 1, 0);
Knight knight_white2 = new Knight(0, 6, 0);
pieces[20]= knight_white1;
pieces[22] = knight_white2;
Knight knight_black1 = new Knight(1, 1, 7);
Knight knight_black2 = new Knight(1, 6, 7);
pieces[21] = knight_black1;
pieces[23] = knight_black2;
Bishop bishop_white1 = new Bishop(0, 2, 0);
Bishop bishop_white2 = new Bishop(0, 5, 0);
pieces[24] = bishop_white1;
pieces[26] = bishop_white2;
Bishop bishop_black1 = new Bishop(1, 2, 7);
Bishop bishop_black2 = new Bishop(1, 5, 7);
pieces[25] = bishop_black1;
pieces[27] = bishop_black2;
Queen white_1 = new Queen(0, 3, 0);
Queen black_1 = new Queen(1, 3, 7);
pieces[28] = white_1;
pieces[29] = black_1;
King white_2 = new King(0, 4, 0);
King black_2 = new King(1, 4, 7);
pieces[30] = white_2;
pieces[31] = black_2;
}
public boolean isPieceThere(int x, int y) // edit !
{
if (grid_matrix[x][y]==1)
{
return true;
}
return false;
}
public void Move(int x,int y, Piece piece)
{ int curr_x = piece.getCoordinates().get(0);
int curr_y = piece.getCoordinates().get(1);
if(piece.isMoveLegal(x, y))
{
if(piece.getType()==1) //King
{
if(grid_matrix[x][y] == 0) //chances of bugs
{
grid_matrix[curr_x][curr_y] = 0;
piece.setCoordinates(x, y);
grid_matrix[x][y] = 1;
checkAndUndo(piece);
}
}
}
if(piece.isMoveLegal(x, y))
{
if(piece.getType()==2)
{
if(!((grid_matrix[x][y]==1)&& (piece.isVerticallyObstructed(x, y, grid_matrix)||piece.isHorizontallyObstructed(x, y, grid_matrix)|| piece.isDiagonallyObstructed(x, y, grid_matrix)))) //chances of bugs
{
grid_matrix[curr_x][curr_y] = 0;
piece.setCoordinates(x, y);
grid_matrix[x][y] = 1;
checkAndUndo(piece);
}
}
}
if(piece.isMoveLegal(x, y))
{
if(piece.getType()==5)
{
if(!(piece.isDiagonallyObstructed(x, y, grid_matrix)) && grid_matrix[x][y]==0)
{
grid_matrix[curr_x][curr_y]=0;
piece.setCoordinates(x, y);
grid_matrix[x][y] = 1;
checkAndUndo(piece);
}
}
}
if(piece.isMoveLegal(x, y) && grid_matrix[x][y]==0)
{
if(piece.getType()==4)
{
grid_matrix[curr_x][curr_y]=0;
piece.setCoordinates(x, y);
grid_matrix[x][y] = 1;
checkAndUndo(piece); // if check, then we undo the move
}
}
if(piece.isMoveLegal(x, y) && grid_matrix[x][y]==0)
{
if(piece.getType()==3)
{
if((piece.isVerticallyObstructed(x, y, grid_matrix) || piece.isHorizontallyObstructed(x, y, grid_matrix)))
{
grid_matrix[curr_x][curr_y]=0;
piece.setCoordinates(x, y);
grid_matrix[x][y] = 1;
checkAndUndo(piece);
}
}
}
if(piece.isMoveLegal(x, y) && grid_matrix[x][y]==0)
{
if(piece.getType()==6) //Pawn
{
if(!(piece.isVerticallyObstructed(x, y, grid_matrix )))
{
grid_matrix[curr_x][curr_y]=0;
piece.setCoordinates(x, y);
grid_matrix[x][y] = 1;
checkAndUndo(piece);
piece.setHasMovedOnce();
}
}
}
}
public void checkAndUndo(Piece piece)
{ int curr_x = piece.getCoordinates().get(0);
int curr_y = piece.getCoordinates().get(1);
if(piece.color==1)
{
if(isCheck(pieces[31])) //check if the black king is in check, if so undo the step.
{
piece.setCoordinates(curr_x, curr_y);
}
}
else
{
if(isCheck(pieces[30])) //check if the white king is in check, if so undo the step.
{
piece.setCoordinates(curr_x, curr_y);
}
}
}
public void killPiece(int x,int y, Piece killerpiece,Piece victimpiece)
{
if(killerpiece.isMoveLegal(x, y) && grid_matrix[x][y]==1)
{ ArrayList<Integer> curr_coordinates = killerpiece.getCoordinates();
int curr_x = curr_coordinates.get(0);
int curr_y = curr_coordinates.get(1);
killerpiece.setCoordinates(x, y);
grid_matrix[curr_x][curr_y]=0; //set to unoccupied
victimpiece.setCoordinates(-1, -1); //Throw the dead piece off the board
}
}
public boolean isCheck(Piece king)
{
int coordinates_king_x = king.getCoordinates().get(0); //get coordinates of king
int coordinates_king_y = king.getCoordinates().get(1);
for(int i=0;i<32;i++)
{
if(pieces[i].getColor()!= king.getColor())
{
if(pieces[i].isMoveLegal(coordinates_king_x, coordinates_king_y))
{
if(pieces[i].getType()==2)
{
if (!((pieces[i].isVerticallyObstructed(coordinates_king_x, coordinates_king_y, grid_matrix)||pieces[i].isHorizontallyObstructed(coordinates_king_x, coordinates_king_y, grid_matrix)|| pieces[i].isDiagonallyObstructed(coordinates_king_x, coordinates_king_y, grid_matrix))))
{
return true; //check by queen
}
return false;
}
if(pieces[i].getType()==3)
{
if(!pieces[i].isVerticallyObstructed(coordinates_king_x, coordinates_king_y, grid_matrix))
{
return true;
}
return false;
}
if(pieces[i].getType()==4)
{
return true;
}
if(pieces[i].getType()==5)
{
if(!pieces[i].isDiagonallyObstructed(coordinates_king_x, coordinates_king_y, grid_matrix))
{
return true;
}
return false;
}
if(pieces[i].getType()==6) //Pawn
{
if(!(pieces[i].isVerticallyObstructed(coordinates_king_x, coordinates_king_y, grid_matrix )))
{
return true;
}
return false;
}
}
else
{
return false;
}
}
else
{
return false;
}
}
return false;
}
public boolean checkMate()
{ ArrayList<Integer> curr = pieces[30].getCoordinates();
int curr_x = curr.get(0);
int curr_y = curr.get(1);
if (isCheck(pieces[30]))
{
Move(curr_x + 1, curr_y, pieces[30]);
Move(curr_x + 1, curr_y + 1, pieces[30]);
Move(curr_x, curr_y + 1, pieces[30]);
Move(curr_x - 1, curr_y + 1, pieces[30]);
Move(curr_x - 1, curr_y, pieces[30]);
Move(curr_x - 1, curr_y - 1, pieces[30]);
Move(curr_x, curr_y - 1, pieces[30]);
Move(curr_x + 1, curr_y - 1, pieces[30]);
ArrayList<Integer> temp = pieces[30].getCoordinates();
int temp_x = temp.get(0);
int temp_y = temp.get(1);
if ((curr_x == temp_x) && (curr_y == temp_y))
{ pieces[30].setCoordinates(curr_x, curr_y);//Get the king to its original position
return true;
}
else
{
pieces[30].setCoordinates(curr_x, curr_y);
return false;
}
}
else
{ pieces[30].setCoordinates(curr_x, curr_y);
return false;
}
}
}