forked from sagnew/Chess
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoard.java
More file actions
393 lines (336 loc) · 13.1 KB
/
Copy pathBoard.java
File metadata and controls
393 lines (336 loc) · 13.1 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
import java.io.IOException;
public class Board {
public Piece[][] board = new Piece[8][8];
public Board(){
this.initialize();
}
private void initialize(){
for(int x = 0; x<board.length; x++){
for(int y = 0; y<board[0].length; y++){
board[x][y] = null;
}
}
// White pawns
for(int x=0; x<8; x++){
board[1][x] = new Pawn("white");
}
// Black pawns
for(int x=0; x<8; x++){
board[6][x] = new Pawn("black");
}
//Rooks
board[0][0] = new Rook("white");
board[0][7] = new Rook("white");
board[7][7] = new Rook("black");
board[7][0] = new Rook("black");
//Knights
board[0][1] = new Knight("white");
board[0][6] = new Knight("white");
board[7][6] = new Knight("black");
board[7][1] = new Knight("black");
//Bishops
board[0][2] = new Bishop("white");
board[0][5] = new Bishop("white");
board[7][2] = new Bishop("black");
board[7][5] = new Bishop("black");
//Queens
board[0][3] = new Queen("white");
board[7][3] = new Queen("black");
//Kings
board[0][4] = new King("white");
board[7][4] = new King("black");
}
/**
* Checks to see if the king of the given color is in check. This will be called twice after every move
* @param color
* @return boolean value corresponding to the king being in check
*/
public boolean isInCheck(String color){
int[] kingPos = getKingPos(color);
int row = kingPos[0];
int col = kingPos[1];
for(int x = 0; x<board.length; x++){
for(int y = 0; y<board[0].length; y++){
if(board[x][y] != null){
if(board[x][y].validateMove(board, x, y, row, col) && !board[x][y].getColor().equals(color)){
return true;
}
}
}
}
return false;
}
/**
* Performs the move, and modifies the actual board
*
* @throws IOException
*/
public void performMove(String move, String color, boolean actuallyMove) throws IOException{
int[] moveArray = parseInput(move);
//System.out.println(board[moveArray[0]][moveArray[1]]);
if(board[moveArray[0]][moveArray[1]] == null){
throw new IOException();
}
if(!board[moveArray[0]][moveArray[1]].getColor().equals(color)){
throw new IOException();
}
if(board[moveArray[2]][moveArray[3]] != null){
if(board[moveArray[2]][moveArray[3]].getColor().equals(color)){
throw new IOException();
}
}
if(board[moveArray[0]][moveArray[1]].validateMove(board, moveArray[0], moveArray[1], moveArray[2], moveArray[3])){
//This means the move was valid
if(isInCheck(color)){
//Because the current player put him/herself in check
//I need to find out what is going to happen to the board in this case
System.out.println("1");
throw new IOException();
}
if(actuallyMove){
//Switch the two spots on the board because the move was valid
board[moveArray[2]][moveArray[3]] = board[moveArray[0]][moveArray[1]];
board[moveArray[0]][moveArray[1]] = null;
}
if(board[moveArray[2]][moveArray[3]] != null){
if(board[moveArray[2]][moveArray[3]].getClass().isInstance(new King("white"))){
if(actuallyMove){
((King) board[moveArray[2]][moveArray[3]]).hasMoved = true;
}
//This Piece is a King
if(((King) board[moveArray[2]][moveArray[3]]).castled){
if(moveArray[3] - moveArray[1] == 2){
board[moveArray[2]][moveArray[3] - 1] = board[moveArray[2]][moveArray[3] + 1];
board[moveArray[2]][moveArray[3] + 1] = null;
}else{
board[moveArray[2]][moveArray[3] + 1] = board[moveArray[2]][moveArray[3] - 1];
board[moveArray[2]][moveArray[3] - 1] = null;
}
((King) board[moveArray[2]][moveArray[3]]).castled = false;
}
}
}
}else{
throw new IOException();
}
//Rules dealing with pawns
if(actuallyMove){
Piece piece = board[moveArray[2]][moveArray[3]];
//This way it gets toggled the next time it moves
piece.ep_able = false;
if(piece != null){
if(piece.getClass().isInstance(new Pawn("white"))){
//The piece is a pawn
piece.hasMoved = true;
//Promotion
Piece replacement;
if(move.split(" ").length < 3){
move += " s";
}
if(piece.getColor().equals("white")){
if(moveArray[2] == 7){
switch(move.split(" ")[2].charAt(0)){
case 'N': replacement = new Knight("white"); break;
case 'B': replacement = new Bishop("white"); break;
default: replacement = new Queen("white"); break;
}
board[moveArray[2]][moveArray[3]] = replacement;
}
}else{
if(moveArray[2] == 0){
switch(move.split(" ")[2].charAt(0)){
case 'N': replacement = new Knight("black"); break;
case 'B': replacement = new Bishop("black"); break;
default: replacement = new Queen("black"); break;
}
board[moveArray[2]][moveArray[3]] = replacement;
}
}
//En passante capture
/*int newCol = moveArray[3];
int newRow = moveArray[2];
if(inSixthRank(color, newRow)){
if(newCol + 1 < 8){
if(board[newRow][newCol + 1] != null){
if(board[newRow][newCol + 1].getClass().isInstance(new Pawn("white"))){
if(board[][].ep_able){
board[][] = null;
}
}
}
}else if(newCol - 1 > 0){
if(board[newRow][newCol - 1] != null){
if(board[newRow][newCol - 1].getClass().isInstance(new Pawn("white"))){
if(board[][].ep_able){
board[][] = null;
}
}
}
}
}*/
}
}
}
}
private boolean inSixthRank(String color, int rank){
if(color.equals("white")){
return rank == 5;
}else{
return rank == 2;
}
}
/**
* Parses the user's string input for a move
* @param move
* @return An array of size 4 with the initial x, y positions and the final x, y positions in that order
*/
public static int[] parseInput(String move){
int[] returnArray = new int[4];
String[] split = move.split(" ");
returnArray[1] = charToInt(Character.toLowerCase(split[0].charAt(0)));
returnArray[0] = Integer.parseInt(move.charAt(1) + "") - 1;
returnArray[3] = charToInt(Character.toLowerCase(split[1].charAt(0)));
returnArray[2] = Integer.parseInt(split[1].charAt(1) + "") - 1;
return returnArray;
}
/**
* Returns an integer corresponding to the user input
*/
public static int charToInt(char ch){
switch(ch){
case 'a': return 0;
case 'b': return 1;
case 'c': return 2;
case 'd': return 3;
case 'e': return 4;
case 'f': return 5;
case 'g': return 6;
case 'h': return 7;
default: return 8;
}
}
private int[] getKingPos(String color){
int row = 0, col = 0;
for(int x = 0; x<board.length; x++){
for(int y = 0; y<board[0].length; y++){
if(board[x][y] != null){
if(board[x][y].getClass().isInstance(new King("white")) && board[x][y].getColor().equals(color)){
row = x;
col = y;
}
}
}
}
int[] returnArray = new int[2];
returnArray[0] = row;
returnArray[1] = col;
return returnArray;
}
/**
* Checks to see if any moves are possible. If not, then it is either a checkmate or stalemate, depending on whether or not anyone is currently in check.
* @return
*/
public boolean canAnyPieceMakeAnyMove(String color){
Piece[][] oldBoard = board.clone();
for(int x = 0; x<board.length; x++){
for(int y = 0; y<board[0].length; y++){
//Check this piece against every other piece...
for(int w = 0; w<board.length; w++){
for(int z = 0; z<board[0].length; z++){
try{
if(board[x][y] != null){
if(board[x][y].getColor().equals(color)){
//System.out.println(coordinatesToMoveString(x, y, w, z));
performMove(coordinatesToMoveString(x, y, w, z), board[x][y].getColor(), false);
board = oldBoard;
return true;
}
}
board = oldBoard;
} catch(Exception e){
board = oldBoard;
}
}
}
}
}
board = oldBoard;
return false;
}
private String coordinatesToMoveString(int row, int col, int newRow, int newCol){
String returnString = "";
switch(col){
case 0: returnString += 'a'; break;
case 1: returnString += 'b'; break;
case 2: returnString += 'c'; break;
case 3: returnString += 'd'; break;
case 4: returnString += 'e'; break;
case 5: returnString += 'f'; break;
case 6: returnString += 'g'; break;
case 7: returnString += 'h'; break;
default: returnString += 'a'; break;
}
int addInt = row + 1;
returnString += addInt + "";
returnString += " ";
switch(newCol){
case 0: returnString += 'a'; break;
case 1: returnString += 'b'; break;
case 2: returnString += 'c'; break;
case 3: returnString += 'd'; break;
case 4: returnString += 'e'; break;
case 5: returnString += 'f'; break;
case 6: returnString += 'g'; break;
case 7: returnString += 'h'; break;
default: returnString += 'a'; break;
}
addInt = newRow + 1;
returnString += addInt + "";
//System.out.println(row + " " + col + " " + newRow + " " + newCol + " " + returnString);
return returnString;
}
/**
* Checks to see if there is a stalemate
* @return true if stalemated, false otherwise
*/
public boolean staleMate(String color){
return false;
}
public String toString(){
String string = "";
int fileCount = 0;
for(Piece[] pieces: board){
int rankCount = 0;
for(Piece piece: pieces){
if(piece==null){
if(fileCount%2 == 0){
if(rankCount%2 == 0){
string += "##";
}else{
string += " ";
}
}else{
if(rankCount%2 == 0){
string += " ";
}else{
string += "##";
}
}
}else{
string += piece;
}
string += " ";
rankCount++;
}
fileCount++;
string += "\n";
}
String reverseString = "";
reverseString += " a b c d e f g h \n";
String[] stringSplit = string.split("\n");
for(int x = stringSplit.length-1; x >= 0; x--){
reverseString += x+1 + " " + stringSplit[x] + "\n";
}
return reverseString;
}
}