-
Notifications
You must be signed in to change notification settings - Fork 1
/
chessboard.cpp
322 lines (267 loc) · 8.7 KB
/
chessboard.cpp
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
#include "chessboard.h"
Chessboard::Chessboard() {
for (char i = 0; i < SQUARE_ON_CHESSBOARD; i++) {
frame_[i] = OPEN_SQUARE;
}
frame_[0] = WHITE_ROOK_QUEEN;
frame_[1] = WHITE_KNIGHT_QUEEN;
frame_[2] = WHITE_BISHOP_QUEEN;
frame_[3] = WHITE_QUEEN;
frame_[4] = WHITE_KING;
frame_[5] = WHITE_BISHOP_KING;
frame_[6] = WHITE_KNIGHT_KING;
frame_[7] = WHITE_ROOK_KING;
frame_[8] = WHITE_PAWN1;
frame_[9] = WHITE_PAWN2;
frame_[10] = WHITE_PAWN3;
frame_[11] = WHITE_PAWN4;
frame_[12] = WHITE_PAWN5;
frame_[13] = WHITE_PAWN6;
frame_[14] = WHITE_PAWN7;
frame_[15] = WHITE_PAWN8;
frame_[48] = BLACK_PAWN8;
frame_[49] = BLACK_PAWN7;
frame_[50] = BLACK_PAWN6;
frame_[51] = BLACK_PAWN5;
frame_[52] = BLACK_PAWN4;
frame_[53] = BLACK_PAWN3;
frame_[54] = BLACK_PAWN2;
frame_[55] = BLACK_PAWN1;
frame_[56] = BLACK_ROOK_QUEEN;
frame_[57] = BLACK_KNIGHT_QUEEN;
frame_[58] = BLACK_BISHOP_QUEEN;
frame_[59] = BLACK_QUEEN;
frame_[60] = BLACK_KING;
frame_[61] = BLACK_BISHOP_KING;
frame_[62] = BLACK_KNIGHT_KING;
frame_[63] = BLACK_ROOK_KING;
inCheck_ = 0;
whiteKingCastle_ = 1;
whiteQueenCastle_ = 1;
blackKingCastle_ = 1;
blackQueenCastle_ = 1;
enPassantCol_ = -1;
}
Chessboard::Chessboard(const Chessboard& chessboard) {
for (char i = 0; i < SQUARE_ON_CHESSBOARD; i++) {
frame_[i] = chessboard.frame_[i];
}
inCheck_ = chessboard.inCheck_;
enPassantCol_ = chessboard.enPassantCol_;
whiteKingCastle_ = chessboard.whiteKingCastle_;
whiteQueenCastle_ = chessboard.whiteQueenCastle_;
blackKingCastle_ = chessboard.blackKingCastle_;
blackQueenCastle_ = chessboard.blackQueenCastle_;
}
char Chessboard::isKingInCheck(char king) {
char kingInCheck = 0;
for (int row = 0; row < 8; row++) {
for (int col = 0; col < 8; col++) {
if (get(row, col) == king) {
kingInCheck = getCheck(row, col);
}
}
}
return kingInCheck;
}
std::vector<Move> Chessboard::getMoves(char color) {
std::vector<Move> moves;
char kingInCheck = 0;
for (int row = 0; row < 8; row++) {
for (int col = 0; col < 8; col++) {
if (getColor(row, col) == color) {
std::vector<Move> m;
char piece = get(row, col);
if (isPawn(piece)) {
m = getPawnMoves(*this, row, col);
} else if (isKnight(piece)) {
m = getKnightMoves(*this, row, col);
} else if (isBishop(piece)) {
m = getBishopMoves(*this, row, col);
} else if (isRook(piece)) {
m = getRookMoves(*this, row, col);
} else if (isQueen(piece)) {
m = getQueenMoves(*this, row, col);
} else if (isKing(piece)) {
m = getKingMoves(*this, row, col);
}
moves.insert(moves.end(), m.begin(), m.end());
}
}
}
buildCheckBoard(moves);
return moves;
}
void Chessboard::buildCheckBoard(const std::vector<Move>& moves) {
inCheck_ = 0;
for (int i = 0; i < moves.size(); i++) {
if (moves[i].getCheck()) {
char row = moves[i].getRowEnd();
char col = moves[i].getColEnd();
inCheck_ |= 1 << (row * 8 + col);
}
}
}
Chessboard Chessboard::applyMove(const Move& move) const {
Chessboard next(*this);
// In every circumstance, we move a piece from one location to another.
char rowStart = move.getRowStart();
char colStart = move.getColStart();
char rowEnd = move.getRowEnd();
char colEnd = move.getColEnd();
char pieceStart = next.get(rowStart, colStart);
char pieceColor = next.getColor(rowStart, colStart);
if (pieceStart == OPEN_SQUARE) {
throw std::invalid_argument("Can't move from open square.");
}
next.set(rowStart, colStart, OPEN_SQUARE);
next.set(rowEnd, colEnd, pieceStart);
// We handle the en passant.
if (move.getEnPassantTaken()) {
if (pieceColor == WHITE_PIECE) {
next.set(4, colEnd, OPEN_SQUARE);
}
if (pieceColor == BLACK_PIECE) {
next.set(3, colEnd, OPEN_SQUARE);
}
}
// We always update enPassantCol_ to move.getEnPassant()
next.enPassantCol_ = move.getEnPassant();
// We handle castling.
if (move.getCastling()) {
if (rowEnd == 0 && colEnd == 2) {
next.set(rowEnd, 0, OPEN_SQUARE);
next.set(rowEnd, 3, WHITE_ROOK_QUEEN);
}
else if (rowEnd == 0 && colEnd == 6) {
next.set(rowEnd, 0, OPEN_SQUARE);
next.set(rowEnd, 5, WHITE_ROOK_KING);
}
else if (rowEnd == 7 && colEnd == 2) {
next.set(rowEnd, 0, OPEN_SQUARE);
next.set(rowEnd, 3, BLACK_ROOK_QUEEN);
}
else if (rowEnd == 7 && colEnd == 6) {
next.set(rowEnd, 0, OPEN_SQUARE);
next.set(rowEnd, 5, BLACK_ROOK_KING);
}
else {
throw std::invalid_argument("Castling was not properly set.");
}
}
next.getMoves(pieceColor);
return next;
}
void Chessboard::checkForValidRow(char row) const {
if (row < 0 || row >= 8) {
int rowInt = row;
throw std::invalid_argument("Invalid row.");
}
}
void Chessboard::checkForValidCol(char col) const {
if (col < 0 || col >= 8) {
throw std::invalid_argument("Invalid col.");
}
}
char Chessboard::isBlack(char row, char col) const {
checkForValidRow(row);
checkForValidCol(col);
char piece = get(row, col);
return piece == BLACK_ROOK_QUEEN ||
piece == BLACK_KNIGHT_QUEEN ||
piece == BLACK_BISHOP_QUEEN ||
piece == BLACK_QUEEN ||
piece == BLACK_KING ||
piece == BLACK_BISHOP_KING ||
piece == BLACK_KNIGHT_KING ||
piece == BLACK_ROOK_KING ||
piece == BLACK_PAWN1 ||
piece == BLACK_PAWN2 ||
piece == BLACK_PAWN3 ||
piece == BLACK_PAWN4 ||
piece == BLACK_PAWN5 ||
piece == BLACK_PAWN6 ||
piece == BLACK_PAWN7 ||
piece == BLACK_PAWN8
;
}
char Chessboard::isWhite(char row, char col) const {
checkForValidRow(row);
checkForValidCol(col);
char piece = get(row, col);
return piece == WHITE_ROOK_QUEEN ||
piece == WHITE_KNIGHT_QUEEN ||
piece == WHITE_BISHOP_QUEEN ||
piece == WHITE_QUEEN ||
piece == WHITE_KING ||
piece == WHITE_BISHOP_KING ||
piece == WHITE_KNIGHT_KING ||
piece == WHITE_ROOK_KING ||
piece == WHITE_PAWN1 ||
piece == WHITE_PAWN2 ||
piece == WHITE_PAWN3 ||
piece == WHITE_PAWN4 ||
piece == WHITE_PAWN5 ||
piece == WHITE_PAWN6 ||
piece == WHITE_PAWN7 ||
piece == WHITE_PAWN8
;
}
char Chessboard::getColor(char row, char col) const {
checkForValidRow(row);
checkForValidCol(col);
if (isWhite(row, col)) {
return WHITE_PIECE;
}
if (isBlack(row, col)) {
return BLACK_PIECE;
}
return OPEN_SQUARE;
}
void Chessboard::set(char row, char col, char piece) {
frame_[row * 8 + col] = piece;
}
char Chessboard::get(char row, char col) const {
checkForValidRow(row);
checkForValidCol(col);
return frame_[row * 8 + col];
}
char Chessboard::getCheck(char row, char col) const {
checkForValidRow(row);
checkForValidCol(col);
return inCheck_ & (1 << (row * 8 + col));
}
std::vector<Move> Chessboard::getMovesInDirection(char row, char col, char rowDirection, char colDirection) const {
checkForValidRow(row);
checkForValidCol(col);
char color = getColor(row, col);
std::vector<Move> moves;
char i = row + rowDirection;
char j = col + colDirection;
for (; i >= 0 && i <= 7 && j >= 0 && j <= 7 && get(i, j) == OPEN_SQUARE; i += rowDirection, j += colDirection) {
Move m(row, col, i, j);
m.setCheck();
moves.push_back(m);
}
if (i >= 0 && i <= 7 && j >= 0 && j <= 7 && getColor(i, j) != color) {
Move m(row, col, i, j);
m.setCheck();
moves.push_back(m);
}
return moves;
}
char Chessboard::getEnPassantCol() const {
return enPassantCol_;
}
char Chessboard::isWhiteKingCastlingAllowed() const {
return whiteKingCastle_;
}
char Chessboard::isWhiteQueenCastlingAllowed() const {
return whiteQueenCastle_;
}
char Chessboard::isBlackKingCastlingAllowed() const {
return blackKingCastle_;
}
char Chessboard::isBlackQueenCastlingAllowed() const {
return blackQueenCastle_;
}