-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGrid.java
More file actions
420 lines (355 loc) · 13.7 KB
/
Grid.java
File metadata and controls
420 lines (355 loc) · 13.7 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
package ChessBot;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
public class Grid {
private int[] grid;
private boolean isTurnWhite;
private boolean whiteIsChecked;
private boolean blackIsChecked;
private boolean whiteKingsideCastle, whiteQueensideCastle, blackKingsideCastle, blackQueensideCastle;
private int halfMoves;
private int enPassantTarget;
private HashSet<Integer> whiteAttackedSquares;
private HashSet<Integer> blackAttackedSquares;
private int whiteKingPosition;
private int blackKingPosition;
private static HashMap<Character, Integer> fenPieces;
public final static int[] fenPieceValues = new int[] {1, 2, 3, 4, 5, 6, 9, 10, 11, 12, 13, 14};
private final static char[] fenPieceKeys = new char[] {'P','B','N','R','Q','K','p','b','n','r','q','k'};
private final static String rowLetters = "abcdefgh";
private final static String pieceChars = "--BNRQK--BNRQK";
private final List<String> movesDone = new ArrayList<>();
public Grid() {}
public void readFromFenString(String fen) {
grid = new int[64];
whiteIsChecked = false;
blackIsChecked = false;
List<String> info = new ArrayList<>(Arrays.asList(fen.split(" ")));
String positions = info.get(0);
int gridPoint = 0;
for (int i=0; i<positions.length(); i++) {
char chr = positions.charAt(i);
if (Character.isDigit(chr)) {
gridPoint += chr - '0';
} else if (!(chr == '/')) {
grid[gridPoint] = fenPieces.get(chr);
gridPoint++;
}
}
isTurnWhite = info.get(1).equals("w");
enPassantTarget = convertPawnString(info.get(3));
halfMoves = Integer.valueOf(info.get(4));
String castles = info.get(2);
whiteKingsideCastle = castles.contains("K");
whiteQueensideCastle = castles.contains("Q");
blackKingsideCastle = castles.contains("k");
blackQueensideCastle = castles.contains("q");
for (int i=0; i<64; i++) {
if (grid[i] == 6) {
whiteKingPosition = i;
} else if (grid[i] == 14) {
blackKingPosition = i;
}
}
}
public static void initConstants() {
fenPieces = new HashMap<>();
for (int i=0; i<fenPieceKeys.length; i++) {
fenPieces.put(fenPieceKeys[i], fenPieceValues[i]);
}
}
public void changeTurn() {
isTurnWhite = isTurnWhite ? false : true;
}
public static int convertPawnString(String pawn) {
if (pawn.equals("-")) {
return -1;
}
System.out.println(pawn);
int row = rowLetters.indexOf(pawn.charAt(0));
return 64 - Integer.valueOf(pawn.charAt(1)) * 8 + row;
}
private static Move castleMove(Move move, boolean isTurnWhite) {
if (move.isKingsideCastle()) {
if (isTurnWhite) {
return new Move(63, 61);
}
return new Move(7, 5);
}
if (isTurnWhite) {
return new Move(56, 59);
}
return new Move(0, 3);
}
public void findAttackedSquares() {
boolean originalTurn = isTurnWhite;
isTurnWhite = true;
whiteAttackedSquares = new HashSet<>();
List<Move> nextMoves = Moves.getAllLegalMoves(this, false, false);
for (Move m : nextMoves) {
whiteAttackedSquares.add(m.endSquare);
}
blackIsChecked = whiteAttackedSquares.contains(blackKingPosition);
isTurnWhite = false;
blackAttackedSquares = new HashSet<>();
nextMoves = Moves.getAllLegalMoves(this, false, false);
for (Move m : nextMoves) {
blackAttackedSquares.add(m.endSquare);
}
whiteIsChecked = blackAttackedSquares.contains(whiteKingPosition);
isTurnWhite = originalTurn;
}
public AntiMove makeMove(Move move, boolean updateScreen) {
if (updateScreen && halfMoves < 5) {
addMove(move);
}
final int rookValue = isTurnWhite ? 4 : 12;
final int enemyRookValue = 16 - rookValue;
final int pawnOffset = isTurnWhite ? 8 : -8;
final boolean prevWhiteKingside = whiteKingsideCastle;
final boolean prevWhiteQueenside = whiteQueensideCastle;
final boolean prevBlackKingside = blackKingsideCastle;
final boolean prevBlackQueenside = blackQueensideCastle;
final int prevEnPassantTarget = enPassantTarget;
final HashSet<Integer> prevWhiteAttackedSquares = whiteAttackedSquares;
final HashSet<Integer> prevBlackAttackedSquares = blackAttackedSquares;
final boolean wasWhiteCheck = whiteIsChecked;
final boolean wasBlackCheck = blackIsChecked;
final int startValue = grid[move.startSquare];
final int endValue = grid[move.endSquare];
if (startValue == 6 || move.startSquare == 63) {
whiteKingsideCastle = false;
} if (startValue == 6 || move.startSquare == 56) {
whiteQueensideCastle = false;
} if (startValue == 14 || move.startSquare == 7) {
blackKingsideCastle = false;
} if (startValue == 14 || move.startSquare == 0) {
blackQueensideCastle = false;
}
if (endValue == enemyRookValue) {
if (move.endSquare == 63) {
whiteKingsideCastle = false;
} else if (move.endSquare == 56) {
whiteQueensideCastle = false;
} else if (move.endSquare == 7) {
blackKingsideCastle = false;
} else if (move.endSquare == 0) {
blackQueensideCastle = false;
}
}
if (move.isCastleGeneral()) {
final Move castle = castleMove(move, isTurnWhite);
grid[castle.endSquare] = rookValue;
grid[castle.startSquare] = 0;
}
int takenPiece;
if (move.getIsEnPassant()) {
final int enPassantPos = move.endSquare + pawnOffset;
takenPiece = grid[enPassantPos];
grid[enPassantPos] = 0;
} else {
takenPiece = grid[move.endSquare];
}
if (move.getIsDoublePawn()) {
enPassantTarget = move.endSquare + pawnOffset;
} else {
enPassantTarget = -1;
}
if (move.getPromotionValue() > 0) {
grid[move.endSquare] = move.getPromotionValue();
} else {
grid[move.endSquare] = startValue;
}
grid[move.startSquare] = 0;
if (startValue == 6) {
whiteKingPosition = move.endSquare;
} else if (startValue == 14) {
blackKingPosition = move.endSquare;
}
findAttackedSquares();
changeTurn();
if (updateScreen) {
GUI.update(this);
halfMoves++;
}
return new AntiMove(takenPiece, prevEnPassantTarget, prevWhiteKingside, prevWhiteQueenside, prevBlackKingside, prevBlackQueenside, prevWhiteAttackedSquares, prevBlackAttackedSquares, wasWhiteCheck, wasBlackCheck);
}
public void unmakeMove(Move move, AntiMove antiMove, boolean updateScreen) {
final int pawnOffset = isTurnWhite ? -8 : 8;
final int pawnValue = isTurnWhite ? 9 : 1;
final int takenPiece = antiMove.getTakenPiece();
enPassantTarget = antiMove.getPrevEnPassantTarget();
whiteKingsideCastle = antiMove.getPrevWhiteKingside();
whiteQueensideCastle = antiMove.getPrevWhiteQueenside();
blackKingsideCastle = antiMove.getPrevBlackKingside();
blackQueensideCastle = antiMove.getPrevBlackQueenside();
whiteAttackedSquares = antiMove.getAttackedSquares(true);
blackAttackedSquares = antiMove.getAttackedSquares(false);
whiteIsChecked = antiMove.wasCheck(true);
blackIsChecked = antiMove.wasCheck(false);
if (grid[move.endSquare] == 6) {
whiteKingPosition = move.startSquare;
} else if (grid[move.endSquare] == 14) {
blackKingPosition = move.startSquare;
}
if (move.isCastleGeneral()) {
final Move antiCastle = castleMove(move, !isTurnWhite);
grid[antiCastle.startSquare] = grid[antiCastle.endSquare];
grid[antiCastle.endSquare] = 0;
}
if (move.getIsEnPassant()) {
grid[move.endSquare + pawnOffset] = takenPiece;
}
if (move.getPromotionValue() > 0) {
grid[move.startSquare] = pawnValue;
} else {
grid[move.startSquare] = grid[move.endSquare];
}
if (!move.getIsEnPassant()) {
grid[move.endSquare] = takenPiece;
} else {
grid[move.endSquare] = 0;
}
changeTurn();
if (updateScreen) {
GUI.update(this);
halfMoves--;
}
}
public boolean getIsWhiteTurn() {
return isTurnWhite;
}
public void setIsTurnWhite(boolean newValue) {
isTurnWhite = newValue;
}
public int[] getGridValues() {
return grid;
}
public boolean getIsChecked(boolean checkForWhite) {
if (checkForWhite) {
return whiteIsChecked;
}
return blackIsChecked;
}
public int getEnPassantTarget() {
return enPassantTarget;
}
public int getHalfMoves() {
return halfMoves;
}
public HashSet<Integer> getAttackedSquares(boolean checkForWhite) {
if (checkForWhite) {
return whiteAttackedSquares;
}
return blackAttackedSquares;
}
public int getKingPos(boolean white) {
if (white) {
return whiteKingPosition;
}
return blackKingPosition;
}
public boolean canCastle(boolean white, boolean kingside) {
if (white && kingside) {
return whiteKingsideCastle;
} else if (white) {
return whiteQueensideCastle;
} else if (!white && kingside) {
return blackKingsideCastle;
}
return blackQueensideCastle;
}
private boolean isKnightMove(int start, int end) {
final int x = start % 8 - end % 8;
final int y = start / 8 - end / 8;
return (int) Math.round(1000 * Math.sqrt(x*x + y*y)) == 2236;
}
public Move codeToMove(String code) {
final int pawnOffset = isTurnWhite ? 8 : -8;
final int pawnValue = isTurnWhite ? 1 : 9;
final int bishopValue = isTurnWhite ? 2 : 10;
final int knightValue = isTurnWhite ? 3 : 11;
final int queenValue = isTurnWhite ? 5 : 13;
final int kingValue = isTurnWhite ? 6 : 14;
int startSquare = -1;
int endSquare;
if (Character.isLowerCase(code.charAt(0))){
if (code.contains("x")) {
endSquare = codeToPos(code.substring(2, 3));
} else {
endSquare = codeToPos(code);
}
startSquare = endSquare;
while (!(grid[startSquare] == pawnValue)) {
startSquare += pawnOffset;
}
} else {
final int pieceValue = pieceChars.indexOf(code.charAt(0));
endSquare = codeToPos(code.substring(code.length() - 2, code.length()));
if (pieceValue == kingValue || pieceValue == queenValue) {
for (int i=0; i<64; i++) {
if (grid[i] == pieceValue) {
startSquare = i;
}
}
} else if (pieceValue == bishopValue) {
for (int i=0; i<64; i++) {
if (grid[i] == pieceValue && endSquare % 2 == i % 2) {
startSquare = i;
}
}
} else {
final boolean hasRankGiven = code.length() > 3 && !code.contains("x");
boolean distanceMatches, rowMatches;
for (int i=0; i<64; i++) {
distanceMatches = !hasRankGiven && isKnightMove(i, endSquare);
rowMatches = hasRankGiven && (i % 8 == rowLetters.indexOf(code.charAt(1)) - 1);
if (grid[i] == knightValue && (distanceMatches || rowMatches)) {
startSquare = i;
}
}
}
}
return new Move(startSquare, endSquare);
}
public int codeToPos(String code) {
final int rank = rowLetters.indexOf(code.charAt(0));
final int row = Integer.parseInt("" + code.charAt(1));
return rank + (8 - row) * 8;
}
public String posToCode(int square) {
final char column = rowLetters.charAt(square % 8);
final char row = (char) ((8 - square / 8) + '0');
return "" + column + row;
}
public void addMove(Move move) {
final int pawnValue = isTurnWhite ? 1 : 9;
String stringMove;
if (grid[move.startSquare] == pawnValue) {
final String code = posToCode(move.endSquare);
if (move.endSquare % 8 == move.startSquare % 8) {
stringMove = code;
} else {
stringMove = code.charAt(0) + "x" + code.charAt(1);
}
} else {
final StringBuilder sb = new StringBuilder("");
sb.append(pieceChars.charAt(grid[move.startSquare]));
if (grid[move.endSquare] > 0) {
sb.append("x");
}
sb.append(posToCode(move.endSquare));
stringMove = sb.toString();
}
if (isTurnWhite ? blackIsChecked : whiteIsChecked) {
stringMove += "+";
}
movesDone.add(stringMove);
}
public List<String> getMoves() {
return movesDone;
}
}