Repository navigation
Expand file tree
/
Copy pathValidateMoves.java
More file actions
373 lines (309 loc) · 14.5 KB
/
Copy pathValidateMoves.java
File metadata and controls
373 lines (309 loc) · 14.5 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
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class ValidateMoves {
public void pawn(Board board, Positon from, Positon to) throws IllegalMove {
Piece pawn = board.getPiece(from);
if (pawn == null || pawn.getName() != PIECES.PAWN) {
System.out.println("Not a pawn");
return;
}
// index validation
if (Math.abs(from.getIndex() - to.getIndex()) > 1) {
throw new IllegalMove("Index invalid");
}
// rank validations
// to will always be greater then from : done
if (pawn.getColor() == COLORS.WHITE ? from.getRank() >= to.getRank() : from.getRank() <= to.getRank()) {
throw new IllegalMove("Cannot Move downwards or parallel !!");
}
// cannot move pawn more then 2 squares : done
if (Math.abs(to.getRank() - from.getRank()) > 2) {
throw new IllegalMove("Cannot move that many steps !!");
}
// if moving two square and it is not first move of the pawn : done
if (Math.abs(from.getRank() - to.getRank()) == 2
&& from.getRank() != (pawn.getColor() == COLORS.WHITE ? Board.WHITE_PAWN_RANK
: Board.BLACK_PAWN_RANK)) {
throw new IllegalMove("Cannot move that many square !!");
}
// if moving straight : no pieces should on that square
if (from.getIndex() == to.getIndex()) { // done
if (board.getPiece(to) != null) { // done
throw new IllegalMove("Already occupied square !!");
}
// if moving two square checking if there is something in the middle
if (Math.abs(to.getRank() - from.getRank()) == 2) { // done
if (board.getPiece(
new Positon(pawn.getColor() == COLORS.WHITE ? from.getRank() + 1 : from.getIndex() - 1,
from.getIndex())) != null) {// done
throw new IllegalMove("Something is in the middle ! cannot move !!");
}
}
} else {
// if not moving straight : must be a capture or en passant
if (board.getPiece(to) == null
&& board.getPiece(new Positon(from.getRank(), to.getIndex())) == null) {
throw new IllegalMove("Illegal move !!"); // done
}
// if there is no black piece then pawn cannot go diagonally
if (board.getPiece(to) != null) {
if (board.getPiece(to).getColor() != (pawn.getColor() == COLORS.WHITE ? COLORS.BLACK : COLORS.WHITE)) {
throw new IllegalMove("Cannot capture !!");
} // done
} else {
// if its en passent then pawn must be on fifth rank
if (from.getRank() != (pawn.getColor() == COLORS.WHITE ? 5 : 4)
|| board.getPiece(new Positon(from.getRank(), to.getIndex()))
.getColor() != (pawn.getColor() == COLORS.WHITE ? COLORS.BLACK : COLORS.WHITE)
|| board.getPiece(new Positon(from.getRank(), to.getIndex())).getName() != PIECES.PAWN ||
Chess.prevMove.getTo() != new Positon(from.getRank(), to.getIndex()) ||
Chess.prevMove.getPiece() != PIECES.PAWN) {
throw new IllegalMove("Illegal en passent !!"); // done
}
}
}
if (from.getRank() == (pawn.getColor() == COLORS.WHITE ? 5 : 4) && board.getPiece(to) == null
&& Math.abs(from.getIndex() - to.getIndex()) == 1
&& board.getPiece(new Positon(from.getRank(), to.getIndex()))
.getColor() == (pawn.getColor() == COLORS.WHITE ? COLORS.BLACK : COLORS.WHITE)
&& board.getPiece(new Positon(from.getRank(), to.getIndex())).getName() == PIECES.PAWN) {
board.getMap().get(from.getRank()).get(to.getIndex()).setPiece(null);// done
}
}
// This function will return all the valid moves of the pawn
public List<Positon> pawn(Board board, Positon positon) {
Piece pawn = board.getPiece(positon);
List<Positon> validMoves = new ArrayList<>();
if (pawn == null || pawn.getName() != PIECES.PAWN) {
System.out.println("Not a pawn");
return validMoves;
}
// try + 1, + 2, and captures and en passent
try {
this.pawn(board, positon,
new Positon(pawn.getColor() == COLORS.WHITE ? positon.getRank() + 1 : positon.getRank() - 1,
positon.getIndex()));
validMoves.add(
new Positon(pawn.getColor() == COLORS.WHITE ? positon.getRank() + 1 : positon.getRank() - 1,
positon.getIndex()));
} catch (Exception e) {
// System.out.println(e.getMessage());
}
try {
this.pawn(board, positon,
new Positon(pawn.getColor() == COLORS.WHITE ? positon.getRank() + 2 : positon.getRank() - 2,
positon.getIndex()));
validMoves.add(
new Positon(pawn.getColor() == COLORS.WHITE ? positon.getRank() + 2 : positon.getRank() - 2,
positon.getIndex()));
} catch (Exception e) {
// System.out.println(e.getMessage());
}
try {
// left side move
if (positon.getIndex() > 0) {
Positon left = new Positon(
pawn.getColor() == COLORS.WHITE ? positon.getRank() + 1 : positon.getRank() - 1,
positon.getIndex() - 1);
this.pawn(board, positon, left);
validMoves.add(left);
}
} catch (Exception e) {
// System.out.println(e.getMessage());
}
try {
// right side move
if (positon.getIndex() < Board.CHESS_CONSTANT - 1) {
Positon right = new Positon(
pawn.getColor() == COLORS.WHITE ? positon.getRank() + 1 : positon.getRank() - 1,
positon.getIndex() + 1);
this.pawn(board, positon, right);
validMoves.add(right);
}
} catch (Exception e) {
// System.out.println(e.getMessage());
}
return validMoves;
}
public void rook(Board board, Positon from, Positon to) throws IllegalMove {
Piece rook = board.getPiece(from);
if (rook == null || (rook.getName() != PIECES.ROOK && rook.getName() != PIECES.QUEEN)) {
System.out.println("Not a Rook");
return;
}
if (from.equals(to)) {
throw new IllegalMove("Cannot move to same square !!");
}
// it must be the same rank or file
if (from.getIndex() != to.getIndex() && from.getRank() != to.getRank()) {
throw new IllegalMove("Illegal move.");
}
// if to have some piece then it must be of opposite color
if (board.getPiece(to) != null) {
if (rook.getColor() == board.getPiece(to).getColor()) {
throw new IllegalMove("Cannot capture own pieces");
}
}
Map<Integer, List<Square>> innerMap = board.getMap();
// checking if moving horizontal or vertical ??
if (from.getIndex() == to.getIndex()) {
// checking if going down to up
if (from.getRank() < to.getRank()) {
for (int i = from.getRank() + 1; i < to.getRank(); i++) {
Square currentSquare = innerMap.get(i).get(from.getIndex());
if (currentSquare.getPiece() != null) {
System.out.println(currentSquare);
throw new IllegalMove("Cannot jump over pieces !!");
}
}
} else {
// going up to down
for (int i = from.getRank() - 1; i > to.getRank(); i--) {
Square currentSquare = innerMap.get(i).get(from.getIndex());
if (currentSquare.getPiece() != null) {
throw new IllegalMove("Cannot jump over pieces !!");
}
}
}
} else if (from.getRank() == to.getRank()) {
List<Square> rank = innerMap.get(from.getRank());
// going left to right
if (from.getIndex() < to.getIndex()) {
for (int i = from.getIndex() + 1; i < to.getIndex(); i++) {
if (rank.get(i).getPiece() != null) {
throw new IllegalMove("Cannot jump over pieces !!");
}
}
} else {
for (int i = from.getIndex() - 1; i > to.getIndex(); i--) {
if (rank.get(i).getPiece() != null) {
throw new IllegalMove("Cannot jump over piece !!");
}
}
}
}
}
public void knight(Board board, Positon from, Positon to) throws IllegalMove, InvalidPosition {
Piece knight = board.getPiece(from);
if (knight == null || knight.getName() != PIECES.KNIGHT) {
System.out.println("Not a night or not valid from !!");
return;
}
List<Positon> validMoves = Utils.validKnightMoves(from);
if (!validMoves.contains(to)) {
throw new IllegalMove("Knight can only move in L shape !!");
}
if (board.getPiece(to) != null && board.getPiece(to).getColor() == knight.getColor()) {
throw new IllegalMove("Square already occupied !!");
}
}
public void bishop(Board board, Positon from, Positon to) throws IllegalMove, InvalidPosition {
Piece bishop = board.getPiece(from);
if (bishop == null || (bishop.getName() != PIECES.BISHOP && bishop.getName() != PIECES.QUEEN)) {
System.out.println("Not a bishop");
return;
}
// cannot go to same square
if (from.equals(to)) {
throw new IllegalMove("Cannot go to same square !!");
}
List<Positon> validMoves = null;
if (bishop.getName() == PIECES.BISHOP) {
validMoves = Utils.validBishopMoves(from);
}
if (validMoves != null) {
if (!validMoves.contains(to)) {
throw new IllegalMove("Illegal square !!");
}
}
// checking if the to is valid ?
// cannot capture our own pieces
if (board.getPiece(to) != null && bishop.getColor() == board.getPiece(to).getColor()) {
throw new IllegalMove("Cannot capture own Pieces !!");
}
Map<Integer, List<Square>> innerMap = board.getMap();
// going upper right
if (from.getIndex() < to.getIndex() && from.getRank() < to.getRank()) {
int j = from.getIndex() + 1;
for (int i = from.getRank() + 1; i < to.getRank() && j < to.getIndex(); i++) {
Square currentSquare = innerMap.get(i).get(j++);
if (currentSquare.getPiece() != null) {
throw new IllegalMove("Cannot jump pieces !!");
}
}
} else if (from.getRank() < to.getRank() && from.getIndex() > to.getIndex()) {
// going upper left
int j = from.getIndex() - 1;
for (int i = from.getRank() + 1; i < to.getRank() && j > to.getIndex(); i++) {
Square currnetSquare = innerMap.get(i).get(j--);
if (currnetSquare.getPiece() != null) {
throw new IllegalMove("Cannot jump pieces !!");
}
}
} else if (from.getRank() > to.getRank() && from.getIndex() < to.getIndex()) {
// going lower right
int j = from.getIndex() + 1;
for (int i = from.getRank() - 1; i > to.getRank() && j < to.getIndex(); i--) {
Square currnetSquare = innerMap.get(i).get(j++);
if (currnetSquare.getPiece() != null) {
throw new IllegalMove("Cannot jump pieces !!");
}
}
} else if (from.getRank() > to.getRank() && from.getIndex() > to.getIndex()) {
// going lower left
int j = from.getIndex() - 1;
for (int i = from.getRank() - 1; i > to.getRank() && j > to.getIndex(); i--) {
Square currentSquare = innerMap.get(i).get(j--);
if (currentSquare.getPiece() != null) {
throw new IllegalMove("Cannot jump this pieces !!");
}
}
}
}
public void queen(Board board, Positon from, Positon to) throws IllegalMove, InvalidPosition {
Piece queen = board.getPiece(from);
if (queen == null || queen.getName() != PIECES.QUEEN) {
System.out.println("Not a queen");
return;
}
List<Positon> validBishopMoves = Utils.validBishopMoves(from);
if (validBishopMoves.contains(to)) {
this.bishop(board, from, to);
} else {
this.rook(board, from, to);
}
}
public void king(Board board, Positon from, Positon to) throws InvalidPosition, IllegalMove {
Piece king = board.getPiece(from);
if (king == null || king.getName() != PIECES.KING) {
System.out.println("Not a king !!");
return;
}
List<Positon> validMoves = Utils.validKingMoves(board, from);
if (!validMoves.contains(to)) {
throw new IllegalMove("Illegal king move !!");
}
}
public static void validate(Board board, Positon from, Positon to) throws IllegalMove, InvalidPosition {
if (board.getPiece(from) == null) {
System.out.println("No piece of from position");
return;
}
ValidateMoves validateMoves = new ValidateMoves();
Piece piece = board.getPiece(from);
if (piece.getName() == PIECES.PAWN) {
validateMoves.pawn(board, from, to);
} else if (piece.getName() == PIECES.ROOK) {
validateMoves.rook(board, from, to);
} else if (piece.getName() == PIECES.KNIGHT) {
validateMoves.knight(board, from, to);
} else if (piece.getName() == PIECES.BISHOP) {
validateMoves.bishop(board, from, to);
} else if (piece.getName() == PIECES.QUEEN) {
validateMoves.queen(board, from, to);
} else if (piece.getName() == PIECES.KING) {
validateMoves.king(board, from, to);
}
}
}