-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChess.cpp
More file actions
286 lines (251 loc) · 10.8 KB
/
Copy pathChess.cpp
File metadata and controls
286 lines (251 loc) · 10.8 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
#include "Chess.h"
#include <cmath>
#include <algorithm>
Color pieceColor(Piece p) {
if (p == EMPTY) return NONE;
return (p <= wK) ? WHITE : BLACK;
}
Color opponent(Color c) { return (c == WHITE) ? BLACK : WHITE; }
bool inBounds(int r, int c) { return r >= 0 && r < 8 && c >= 0 && c < 8; }
void copyBoard(const Board src, Board dst) {
for (int r = 0; r < 8; r++)
for (int c = 0; c < 8; c++)
dst[r][c] = src[r][c];
}
GameState initializeBoard() {
GameState gs;
for (int r = 0; r < 8; r++)
for (int c = 0; c < 8; c++)
gs.board[r][c] = EMPTY;
Piece backRank[8] = { wR, wN, wB, wQ, wK, wB, wN, wR };
Piece blackRank[8] = { bR, bN, bB, bQ, bK, bB, bN, bR };
for (int c = 0; c < 8; c++) {
gs.board[7][c] = backRank[c];
gs.board[6][c] = wP;
gs.board[0][c] = blackRank[c];
gs.board[1][c] = bP;
}
gs.turn = WHITE;
gs.gameOver = false;
gs.whiteInCheck = false;
gs.blackInCheck = false;
gs.enPassantSq = std::nullopt;
gs.castle = { true, true, true, true };
return gs;
}
std::vector<Move> getRookMoves(const Board b, int row, int col, Color color) {
std::vector<Move> moves;
int dirs[4][2] = { {0,1},{0,-1},{1,0},{-1,0} };
for (auto& d : dirs) {
int r = row + d[0], c = col + d[1];
while (inBounds(r, c)) {
if (b[r][c] == EMPTY) { moves.push_back({ {row,col},{r,c} }); }
else { if (pieceColor(b[r][c]) != color) moves.push_back({ {row,col},{r,c} }); break; }
r += d[0]; c += d[1];
}
}
return moves;
}
std::vector<Move> getBishopMoves(const Board b, int row, int col, Color color) {
std::vector<Move> moves;
int dirs[4][2] = { {1,1},{1,-1},{-1,1},{-1,-1} };
for (auto& d : dirs) {
int r = row + d[0], c = col + d[1];
while (inBounds(r, c)) {
if (b[r][c] == EMPTY) { moves.push_back({ {row,col},{r,c} }); }
else { if (pieceColor(b[r][c]) != color) moves.push_back({ {row,col},{r,c} }); break; }
r += d[0]; c += d[1];
}
}
return moves;
}
std::vector<Move> getQueenMoves(const Board b, int row, int col, Color color) {
auto r = getRookMoves(b, row, col, color);
auto bi = getBishopMoves(b, row, col, color);
r.insert(r.end(), bi.begin(), bi.end());
return r;
}
std::vector<Move> getKnightMoves(const Board b, int row, int col, Color color) {
std::vector<Move> moves;
int jumps[8][2] = { {-2,-1},{-2,1},{-1,-2},{-1,2},{1,-2},{1,2},{2,-1},{2,1} };
for (auto& j : jumps) {
int r = row + j[0], c = col + j[1];
if (inBounds(r, c) && pieceColor(b[r][c]) != color)
moves.push_back({ {row,col},{r,c} });
}
return moves;
}
std::vector<Move> getPawnMoves(const Board b, int row, int col, Color color,
const std::optional<Square>& epSq) {
std::vector<Move> moves;
int dir = (color == WHITE) ? -1 : 1;
int startR = (color == WHITE) ? 6 : 1;
int promR = (color == WHITE) ? 0 : 7;
int r1 = row + dir;
if (!inBounds(r1, col)) return moves;
auto addPromo = [&](int tr, int tc, bool isEP = false) {
Piece promos[4] = {
(color == WHITE ? wQ : bQ),(color == WHITE ? wR : bR),
(color == WHITE ? wB : bB),(color == WHITE ? wN : bN)
};
for (Piece p : promos) { Move m = { {row,col},{tr,tc},p }; m.enPassant = isEP; moves.push_back(m); }
};
if (b[r1][col] == EMPTY) {
if (r1 == promR) addPromo(r1, col);
else {
moves.push_back({ {row,col},{r1,col} });
if (row == startR && b[r1 + dir][col] == EMPTY)
moves.push_back({ {row,col},{r1 + dir,col} });
}
}
for (int dc : { -1, 1 }) {
int c = col + dc;
if (!inBounds(r1, c)) continue;
bool normalCap = (b[r1][c] != EMPTY && pieceColor(b[r1][c]) != color);
bool epCap = (epSq && epSq->r == r1 && epSq->c == c);
if (normalCap || epCap) {
if (r1 == promR) addPromo(r1, c, epCap && !normalCap);
else { Move m = { {row,col},{r1,c} }; m.enPassant = (epCap && !normalCap); moves.push_back(m); }
}
}
return moves;
}
bool isSquareAttacked(const Board b, int row, int col, Color byColor,
const std::optional<Square>& ep);
std::vector<Move> getKingMoves(const Board b, int row, int col, Color color,
const CastleRights& cr, const std::optional<Square>& ep) {
std::vector<Move> moves;
for (int dr = -1; dr <= 1; dr++)
for (int dc = -1; dc <= 1; dc++) {
if (!dr && !dc) continue;
int r = row + dr, c = col + dc;
if (inBounds(r, c) && pieceColor(b[r][c]) != color)
moves.push_back({ {row,col},{r,c} });
}
Color opp = opponent(color);
int backRow = (color == WHITE) ? 7 : 0;
bool canK = (color == WHITE) ? cr.wK : cr.bK;
bool canQ = (color == WHITE) ? cr.wQ : cr.bQ;
if (row == backRow && col == 4) {
if (canK &&
b[backRow][5] == EMPTY && b[backRow][6] == EMPTY &&
pieceColor(b[backRow][7]) == color &&
!isSquareAttacked(b, backRow, 4, opp, ep) &&
!isSquareAttacked(b, backRow, 5, opp, ep) &&
!isSquareAttacked(b, backRow, 6, opp, ep)) {
Move m = { {row,col},{backRow,6} }; m.castleK = true; moves.push_back(m);
}
if (canQ &&
b[backRow][3] == EMPTY && b[backRow][2] == EMPTY && b[backRow][1] == EMPTY &&
pieceColor(b[backRow][0]) == color &&
!isSquareAttacked(b, backRow, 4, opp, ep) &&
!isSquareAttacked(b, backRow, 3, opp, ep) &&
!isSquareAttacked(b, backRow, 2, opp, ep)) {
Move m = { {row,col},{backRow,2} }; m.castleQ = true; moves.push_back(m);
}
}
return moves;
}
std::vector<Move> getPseudoMoves(const Board b, int row, int col, Color color,
const std::optional<Square>& ep, const CastleRights& cr) {
Piece p = b[row][col];
if (p == wP || p == bP) return getPawnMoves(b, row, col, color, ep);
if (p == wR || p == bR) return getRookMoves(b, row, col, color);
if (p == wB || p == bB) return getBishopMoves(b, row, col, color);
if (p == wQ || p == bQ) return getQueenMoves(b, row, col, color);
if (p == wN || p == bN) return getKnightMoves(b, row, col, color);
if (p == wK || p == bK) return getKingMoves(b, row, col, color, cr, ep);
return {};
}
Square findKingPosition(const Board b, Color color) {
Piece king = (color == WHITE) ? wK : bK;
for (int r = 0; r < 8; r++) for (int c = 0; c < 8; c++) if (b[r][c] == king) return { r,c };
return { -1,-1 };
}
bool isSquareAttacked(const Board b, int row, int col, Color byColor,
const std::optional<Square>& ep) {
CastleRights none = { false,false,false,false };
for (int r = 0; r < 8; r++) for (int c = 0; c < 8; c++) {
if (b[r][c] == EMPTY || pieceColor(b[r][c]) != byColor) continue;
auto moves = getPseudoMoves(b, r, c, byColor, ep, none);
for (auto& m : moves) if (m.to.r == row && m.to.c == col) return true;
}
return false;
}
bool isKingInCheck(const Board b, Color color, const std::optional<Square>& ep) {
Square kp = findKingPosition(b, color);
if (!kp.valid()) return false;
return isSquareAttacked(b, kp.r, kp.c, opponent(color), ep);
}
void simulateMove(const Board src, Board dst, const Move& m) {
copyBoard(src, dst);
Piece piece = dst[m.from.r][m.from.c];
dst[m.to.r][m.to.c] = piece;
dst[m.from.r][m.from.c] = EMPTY;
if (m.enPassant) dst[m.from.r][m.to.c] = EMPTY;
if (m.castleK) { dst[m.to.r][5] = dst[m.to.r][7]; dst[m.to.r][7] = EMPTY; }
if (m.castleQ) { dst[m.to.r][3] = dst[m.to.r][0]; dst[m.to.r][0] = EMPTY; }
if (m.promotion != EMPTY) dst[m.to.r][m.to.c] = m.promotion;
}
bool isLegalMove(const Board b, const Move& m, Color color, const std::optional<Square>& ep) {
Board nb; simulateMove(b, nb, m);
return !isKingInCheck(nb, color, std::nullopt);
}
std::vector<Move> filterLegalMoves(const Board b, int r, int c, Color color,
const std::optional<Square>& ep, const CastleRights& cr) {
auto pseudo = getPseudoMoves(b, r, c, color, ep, cr);
std::vector<Move> legal;
for (auto& m : pseudo) if (isLegalMove(b, m, color, ep)) legal.push_back(m);
return legal;
}
std::vector<Move> getAllPossibleMoves(const Board b, Color color,
const std::optional<Square>& ep, const CastleRights& cr) {
std::vector<Move> all;
for (int r = 0; r < 8; r++) for (int c = 0; c < 8; c++) {
if (b[r][c] == EMPTY || pieceColor(b[r][c]) != color) continue;
auto mv = filterLegalMoves(b, r, c, color, ep, cr);
all.insert(all.end(), mv.begin(), mv.end());
}
return all;
}
bool isCheckmate(const Board b, Color color, const std::optional<Square>& ep, const CastleRights& cr) {
return isKingInCheck(b, color, ep) && getAllPossibleMoves(b, color, ep, cr).empty();
}
bool isStalemate(const Board b, Color color, const std::optional<Square>& ep, const CastleRights& cr) {
return !isKingInCheck(b, color, ep) && getAllPossibleMoves(b, color, ep, cr).empty();
}
void makeMove(GameState& gs, const Move& m, std::vector<HistoryEntry>& history) {
HistoryEntry e;
copyBoard(gs.board, e.board);
e.turn = gs.turn; e.castle = gs.castle; e.enPassantSq = gs.enPassantSq;
history.push_back(e);
Piece piece = gs.board[m.from.r][m.from.c];
Piece captured = gs.board[m.to.r][m.to.c];
if (m.enPassant)
captured = (piece == wP) ? bP : wP;
simulateMove(gs.board, gs.board, m);
gs.enPassantSq = std::nullopt;
if ((piece == wP || piece == bP) && std::abs(m.to.r - m.from.r) == 2)
gs.enPassantSq = Square{ (m.from.r + m.to.r) / 2, m.to.c };
if (piece == wK) { gs.castle.wK = false; gs.castle.wQ = false; }
if (piece == bK) { gs.castle.bK = false; gs.castle.bQ = false; }
if (piece == wR) { if (m.from.c == 0) gs.castle.wQ = false; if (m.from.c == 7) gs.castle.wK = false; }
if (piece == bR) { if (m.from.c == 0) gs.castle.bQ = false; if (m.from.c == 7) gs.castle.bK = false; }
if (captured == wR && m.to.r == 7 && m.to.c == 0) gs.castle.wQ = false;
if (captured == wR && m.to.r == 7 && m.to.c == 7) gs.castle.wK = false;
if (captured == bR && m.to.r == 0 && m.to.c == 0) gs.castle.bQ = false;
if (captured == bR && m.to.r == 0 && m.to.c == 7) gs.castle.bK = false;
gs.turn = opponent(gs.turn);
gs.whiteInCheck = isKingInCheck(gs.board, WHITE, gs.enPassantSq);
gs.blackInCheck = isKingInCheck(gs.board, BLACK, gs.enPassantSq);
}
void undoMove(GameState& gs, std::vector<HistoryEntry>& history) {
if (history.empty()) return;
auto& e = history.back();
copyBoard(e.board, gs.board);
gs.turn = e.turn; gs.castle = e.castle; gs.enPassantSq = e.enPassantSq;
gs.gameOver = false;
gs.whiteInCheck = isKingInCheck(gs.board, WHITE, gs.enPassantSq);
gs.blackInCheck = isKingInCheck(gs.board, BLACK, gs.enPassantSq);
history.pop_back();
}