-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathChessboard.cpp
More file actions
370 lines (324 loc) · 11.3 KB
/
Chessboard.cpp
File metadata and controls
370 lines (324 loc) · 11.3 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
#include "Chessboard.h"
#include "Constants.h"
using namespace std;
void setDefault(Chessboard& board) {
board = Chessboard(DEFAULT_BOARD);
}
uint8_t getPosition(Chessboard board, uint8_t x, uint8_t y) {
size_t index = POSITION_BIT_SIZE * (y * DEFAULT_SIZE + x);
uint8_t position = 0;
for (int i = POSITION_BIT_SIZE - 1; i >= 0; i--)
position = (position << 1) + board[index + i];
return position;
}
void setPosition(Chessboard& board, uint8_t position, uint8_t x, uint8_t y) {
size_t index = POSITION_BIT_SIZE * (y * DEFAULT_SIZE + x);
for (int i = 0; i < POSITION_BIT_SIZE; i++) {
board[index + i] = position & 0b0001;
position >>= 1;
}
}
bool getColor(uint8_t position) {
return !((position & BLACK_MASK) == BLACK_MASK);
}
void displayBinary(Chessboard board) {
for (int y = DEFAULT_SIZE - 1; y >= 0; y--) {
for (int x = 0; x < DEFAULT_SIZE; x++) {
int position = POSITION_BIT_SIZE * (y * DEFAULT_SIZE + x);
for (int i = 0; i < POSITION_BIT_SIZE; i++) {
cout << board[position + (POSITION_BIT_SIZE - i - 1)];
}
cout << " ";
}
cout << endl;
}
}
void displayBoard(Chessboard board) {
const char CHAR_DISPLAY[] = {' ', 'K', 'Q', 'R', 'N', 'B', 'P', 'X'};
cout << " +---+---+---+---+---+---+---+---+" << endl;
for (int y = DEFAULT_SIZE - 1; y >= 0; y--) {
cout << (y + 1) << " |";
for (int x = 0; x < DEFAULT_SIZE; x++) {
uint8_t position = getPosition(board, x, y);
char display = CHAR_DISPLAY[position % DEFAULT_SIZE];
if (getColor(position) == BLACK)
display = tolower(display);
cout << " " << display << " |";
}
cout << endl << " +---+---+---+---+---+---+---+---+" << endl;
}
cout << " ";
for (int x = 0; x < DEFAULT_SIZE; x++)
cout << " " << (char)('a' + x);
cout << endl;
}
bool validateMove(Chessboard board, Move p, bool color) {
cout << endl << "Movement (" << (int)p.fromX << "," << (int)p.fromY
<< ") to (" << (int)p.toX << "," << (int)p.toY << ")" << endl;
if (p.fromX == p.toX && p.fromY == p.toY) {
cout << "-- Can't move to same tile" << endl;
return false;
}
if (p.fromX >= DEFAULT_SIZE || p.fromY >= DEFAULT_SIZE ||
p.toX >= DEFAULT_SIZE || p.toY >= DEFAULT_SIZE) {
cout << "-- Position out of bounds" << endl;
return false;
}
uint8_t position = getPosition(board, p.fromX, p.fromY);
cout << "Moving piece " << bitset<4>(position) << endl;
if (position == EMPTY) {
cout << "-- Can't move empty tile" << endl;
return false;
}
if (getColor(position) != color) {
cout << "-- Can't move opponent's pieces" << endl;
return false;
}
uint8_t destination = getPosition(board, p.toX, p.toY);
cout << "Destination piece " << bitset<4>(destination) << endl;
if ((destination != EMPTY) && ((position ^ destination) < 0b1000)) {
cout << "-- Destination is occupied" << endl;
return false;
}
switch (position & PIECE_MASK) {
case KING:
if (!validateKing(board, destination, p))
return false;
break;
case QUEEN:
if (!validateQueen(board, destination, p))
return false;
break;
case ROOK:
if (!validateRook(board, destination, p))
return false;
break;
case KNIGHT:
if (!validateKnight(board, destination, p))
return false;
break;
case BISHOP:
if (!validateBishop(board, destination, p))
return false;
break;
case PAWN:
if (!validatePawn(board, destination, p, getColor(position)))
return false;
break;
default:
cout << "Unknown piece " << bitset<4>(position) << endl;
return false;
}
Chessboard output(board);
setPosition(output, position, p.toX, p.toY);
setPosition(output, EMPTY, p.fromX, p.fromY);
if (hasCheck(output, getColor(position))) {
cout << "-- King in check" << endl;
return false;
}
cout << "-- Movement accepted" << endl;
return true;
}
void move(Chessboard& board, Move m) {
uint8_t position = getPosition(board, m.fromX, m.fromY);
setPosition(board, position, m.toX, m.toY);
setPosition(board, EMPTY, m.fromX, m.fromY);
}
bool validateKing(Chessboard board, uint8_t dest, Move p) {
if (abs(p.fromX - p.toX) <= 1 && abs(p.fromY - p.toY) <= 1)
return true;
cout << "-- Invalid king movement" << endl;
return false;
}
bool validateQueen(Chessboard board, uint8_t dest, Move p) {
return validateBishop(board, dest, p) || validateRook(board, dest, p);
}
bool validateRook(Chessboard board, uint8_t dest, Move p) {
int i, increment;
if (p.fromX == p.toX) {
increment = p.toY > p.fromY ? 1 : -1;
i = p.fromY + increment;
while (i != p.toY) {
if (getPosition(board, p.fromX, i) != EMPTY) {
cout << "-- Path obstructed" << endl;
return false;
}
i += increment;
}
return true;
}
if (p.fromY == p.toY) {
increment = p.toX > p.fromX ? 1 : -1;
i = p.fromX + increment;
while (i != p.toX) {
if (getPosition(board, i, p.fromY) != EMPTY) {
cout << "-- Path obstructed" << endl;
return false;
}
i += increment;
}
return true;
}
cout << "-- Invalid rook movement" << endl;
return false;
}
bool validateKnight(Chessboard board, uint8_t dest, Move p) {
if (abs(p.toX - p.fromX) == 2 && abs(p.toY - p.fromY) == 1)
return true;
if (abs(p.toX - p.fromX) == 1 && abs(p.toY - p.fromY) == 2)
return true;
cout << "-- Invalid knight movement" << endl;
return false;
}
bool validateBishop(Chessboard board, uint8_t dest, Move p) {
int x, y, incrementX, incrementY;
if (abs(p.toX - p.fromX) == abs(p.toY - p.fromY)) {
incrementX = p.toX > p.fromX ? 1 : -1;
incrementY = p.toY > p.fromY ? 1 : -1;
x = p.fromX + incrementX;
y = p.fromY + incrementY;
while (x != p.toX) {
if (getPosition(board, x, y) != EMPTY) {
cout << "-- Path obstructed" << endl;
return false;
}
x += incrementX;
y += incrementY;
}
return true;
}
cout << "-- Invalid bishop movement" << endl;
return false;
}
bool validatePawn(Chessboard board, uint8_t dest, Move p, bool color) {
int offset = color == WHITE ? 1 : -1;
if (dest == EMPTY && p.fromX == p.toX) {
if (p.fromY + offset == p.toY)
return true;
uint8_t pawnStartY = color == WHITE ? 1 : 6;
if (p.fromY + offset * 2 == p.toY && p.fromY == pawnStartY)
return true;
}
if (dest != EMPTY && abs(p.fromX - p.toX) == 1 && p.fromY + offset == p.toY) {
return true;
}
cout << "-- Invalid pawn movement" << endl;
return false;
}
bool hasCheck(Chessboard board, bool color) {
uint8_t maskUser, maskOpponent;
if (color == WHITE) {
maskUser = WHITE_MASK;
maskOpponent = BLACK_MASK;
}
else {
maskUser = BLACK_MASK;
maskOpponent = WHITE_MASK;
}
uint8_t kingX, kingY;
uint8_t kingPiece = KING | maskUser;
uint8_t targetPiece;
for (kingY = 0; kingY < DEFAULT_SIZE; kingY++) {
for (kingX = 0; kingX < DEFAULT_SIZE; kingX++) {
if (getPosition(board, kingX, kingY) == kingPiece) {
goto found;
}
}
}
found:
// Check if knight is attacking
targetPiece = KNIGHT | maskOpponent;
for (uint8_t x = kingX - 2; x <= kingX + 2; x+=4) {
if (x >= DEFAULT_SIZE) continue;
for (uint8_t y = kingY - 1; y <= kingY + 1; y+=2) {
if (y >= DEFAULT_SIZE) continue;
if (getPosition(board, x, y) == targetPiece)
return true;
}
}
for (uint8_t x = kingX - 1; x <= kingX + 1; x+=2) {
if (x >= DEFAULT_SIZE) continue;
for (uint8_t y = kingY - 2; y <= kingY + 2; y+=4) {
if (y >= DEFAULT_SIZE) continue;
if (getPosition(board, x, y) == targetPiece)
return true;
}
}
// Check if king is attacking
targetPiece = KING | maskOpponent;
for (uint8_t x = kingX - 1; x <= kingX + 1; x++) {
if (x >= DEFAULT_SIZE) continue;
for (uint8_t y = kingY - 1; y <= kingY + 1; y++) {
if (y >= DEFAULT_SIZE) continue;
if (x == kingX && y == kingY) continue;
if (getPosition(board, x, y) == targetPiece)
return true;
}
}
// Check if pawn is attacking
targetPiece = PAWN | maskOpponent;
uint8_t offset = color == WHITE ? 1 : -1;
uint8_t kingPawnOffset = kingY + offset;
if (kingPawnOffset < DEFAULT_SIZE) {
for (uint8_t x = kingX - 1; x <= kingX + 1; x+=2) {
if (x >= DEFAULT_SIZE) continue;
if (getPosition(board, x, kingPawnOffset) == targetPiece)
return true;
}
}
uint8_t x, y, currentTile;
uint8_t rookOpponent = ROOK | maskOpponent;
uint8_t bishopOpponent = BISHOP | maskOpponent;
uint8_t queenOpponent = QUEEN | maskOpponent;
// Check if diagonal piece is attacking
for (int incrementY = -1; incrementY <= 1; incrementY+=2) {
for (int incrementX = -1; incrementX <= 1; incrementX+=2) {
x = kingX + incrementX;
y = kingY + incrementY;
while (x <= DEFAULT_SIZE && y <= DEFAULT_SIZE) {
currentTile = getPosition(board, x, y);
if (currentTile == EMPTY){
x += incrementX;
y += incrementY;
continue;
}
if (currentTile == bishopOpponent || currentTile == queenOpponent) {
return true;
}
break;
}
}
}
// Check if vertical piece is attacking
for (int incrementX = -1; incrementX <= 1; incrementX+=2) {
x = kingX + incrementX;
y = kingY;
while (x <= DEFAULT_SIZE) {
currentTile = getPosition(board, x, y);
if (currentTile == EMPTY){
x += incrementX;
continue;
}
if (currentTile == rookOpponent || currentTile == queenOpponent) {
return true;
}
break;
}
}
for (int incrementY = -1; incrementY <= 1; incrementY+=2) {
x = kingX;
y = kingY + incrementY;
while (y <= DEFAULT_SIZE) {
currentTile = getPosition(board, x, y);
if (currentTile == EMPTY){
y += incrementY;
continue;
}
if (currentTile == rookOpponent || currentTile == queenOpponent) {
return true;
}
break;
}
}
return false;
}