Repository navigation
Expand file tree
/
Copy pathCheckAndMate.java
More file actions
218 lines (179 loc) · 8.57 KB
/
Copy pathCheckAndMate.java
File metadata and controls
218 lines (179 loc) · 8.57 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
import java.util.HashMap;
import java.util.ArrayList;
public class CheckAndMate {
private static King findKing(Figure[][] board, int color) {
for(int i=0; i<board.length; i++) {
for(int j=0; j<board[i].length; j++) {
if(board[i][j] != null && board[i][j].getColor() == color && board[i][j].getType() == "King") {
return (King) board[i][j];
}
}
}
throw new IllegalStateException("No king found");
}
public static Object[] findAtkPiece(Figure[][] board, int color, int oppositeColor) {
King enemyKing = findKing(board, oppositeColor);
int[] kingCoords = Conversion.convertPosToIndex(enemyKing.getPos());
//principal directions
int directions[][] = {
{1, 0}, {1, 1}, {0, 1}, {-1, 1},
{-1, 0}, {-1, -1}, {0, -1}, {1, -1}
};
for (int[] dir : directions) {
ArrayList<String> attackPath = new ArrayList<>();
for (int j = 1; j < 8; j++) {
int row = kingCoords[0] + j * dir[0];
int col = kingCoords[1] + j * dir[1];
if (row < 0 || row >= 8 || col < 0 || col >= 8) {
break;
}
Figure piece = board[row][col];
if (piece != null) {
if (piece.getColor() == oppositeColor) break; //ally piece blocks
if (piece.getColor() == color) {
//check if this piece can attack the king
ArrayList<String> enemyMoves = MovesHandling.findMoves(piece, board);
if (enemyMoves.contains(enemyKing.getPos())) {
attackPath.add(Conversion.convertIndexToPos(row, col));
Object[] obj = {(Figure) piece, attackPath};
return obj;
}
}
break;
}
attackPath.add(Conversion.convertIndexToPos(row, col));
}
}
//knight check
int[][] knightMoves = {
{2, 1}, {2, -1}, {-2, 1}, {-2, -1},
{1, 2}, {1, -2}, {-1, 2}, {-1, -2}
};
for (int[] move : knightMoves) {
int row = kingCoords[0] + move[0];
int col = kingCoords[1] + move[1];
if (row >= 0 && row < 8 && col >= 0 && col < 8 &&
board[row][col] != null &&
board[row][col].getColor() == color &&
board[row][col] instanceof Knight) {
Object[] obj = {(Figure) board[row][col], Conversion.convertIndexToPos(row, col)};
return obj;
}
}
return null;
}
private static ArrayList<String> kingMoves(Figure[][] board, int oppositeColor, int color) {
King king = findKing(board, oppositeColor);
int[] kingCoords = Conversion.convertPosToIndex(king.getPos());
ArrayList<String> eligibleMoves = MovesHandling.checkKingMoves(king, king.getColor(), kingCoords[0], kingCoords[1], board);
if(eligibleMoves.isEmpty()) {
return null;
}
//find the attacking figures path and if it intersects with the kings eligible moves, exclude those moves
ArrayList<String> attackPath = (ArrayList<String>) findAtkPiece(board, color, oppositeColor)[1];
Figure attackFigure = (Figure) findAtkPiece(board, color, oppositeColor)[0];
String type = attackFigure.getType();
switch (type) {
case "Bishop":
attackPath = MovesHandling.checkBishopMoves(color, attackFigure, board);
break;
case "Rook":
attackPath = MovesHandling.checkRookMoves(color, attackFigure, board);
break;
case "Queen":
attackPath = MovesHandling.checkQueenMoves(color, attackFigure, board);
break;
default:
break;
}
eligibleMoves.removeAll(attackPath);
//find if any of the remaining moves have enemy pieces applying check on those squares
//if so exclude those moves as well
for(int i=0; i<eligibleMoves.size(); i++) {
int[] tempKingCoords = Conversion.convertPosToIndex(eligibleMoves.get(i));
board[tempKingCoords[0]][tempKingCoords[1]] = king;
board[kingCoords[0]][kingCoords[1]] = null;
String originalKingPos = king.getPos();
king.setPos(eligibleMoves.get(i));
Object[] attackDetails = findAtkPiece(board, color, oppositeColor);
if (attackDetails == null) {
king.setPos(originalKingPos);
board[tempKingCoords[0]][tempKingCoords[1]] = null;
board[kingCoords[0]][kingCoords[1]] = king;
continue;
}
attackPath = (ArrayList<String>) attackDetails[1];
if(attackPath == null) {
king.setPos(originalKingPos);
board[tempKingCoords[0]][tempKingCoords[1]] = null;
board[kingCoords[0]][kingCoords[1]] = king;
continue;
}
attackFigure = (Figure) attackDetails[0];
if(attackFigure == null) {
king.setPos(originalKingPos);
board[tempKingCoords[0]][tempKingCoords[1]] = null;
board[kingCoords[0]][kingCoords[1]] = king;
continue;
}
type = attackFigure.getType();
switch (type) {
case "Bishop":
attackPath = MovesHandling.checkBishopMoves(color, attackFigure, board);
break;
case "Rook":
attackPath = MovesHandling.checkRookMoves(color, attackFigure, board);
break;
case "Queen":
attackPath = MovesHandling.checkQueenMoves(color, attackFigure, board);
break;
default:
break;
}
eligibleMoves.removeAll(attackPath);
king.setPos(originalKingPos);
board[tempKingCoords[0]][tempKingCoords[1]] = null;
board[kingCoords[0]][kingCoords[1]] = king;
}
if(eligibleMoves.isEmpty() || eligibleMoves == null) {
return null;
}
return eligibleMoves;
}
private static HashMap<String, ArrayList<String>> pieceCheckmateBlock(Figure[][] board, int oppositeColor, int color) {
//find the attack path array
Object[] attackDetails = findAtkPiece(board, color, oppositeColor);
if (attackDetails == null) {
return null;
}
ArrayList<String> attackPath = (ArrayList<String>) attackDetails[1];
if (attackPath == null || attackPath.isEmpty()) {
return null;
}
//map blocking pieces (like the bishop) and their valid moves
HashMap<String, ArrayList<String>> blockingPieces = new HashMap<>();
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
if (board[i][j] != null && board[i][j].getColor() == oppositeColor && board[i][j].getType() != "King") {
ArrayList<String> possibleMoves = MovesHandling.findMoves(board[i][j], board);
//retain only moves that intersect with the attack path
possibleMoves.retainAll(attackPath);
if (!possibleMoves.isEmpty()) {
//Use the blocking piece as the key in the new map
blockingPieces.put(board[i][j].getPos(), new ArrayList<>(possibleMoves));
}
}
}
}
return blockingPieces;
}
public static HashMap<String, ArrayList<String>> combineMovesHelper(Figure[][] board, int oppositeColor, int color) {
HashMap<String, ArrayList<String>> blockingPieces = pieceCheckmateBlock(board, oppositeColor, color);
if(kingMoves(board, oppositeColor, color) != null) {
ArrayList<String> kingMoves = kingMoves(board, oppositeColor, color);
blockingPieces.put(findKing(board, oppositeColor).getPos(), kingMoves);
}
System.out.println(blockingPieces);
return blockingPieces;
}
}