-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.cpp
397 lines (275 loc) · 9.63 KB
/
Game.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
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
#include <bit>
#include <optional>
#include <array>
#include <algorithm>
#include <sstream>
#include <cmath>
#include "Game.hpp"
#include "Pieces/PieceMoveTables.hpp"
//Should this return illegal moves too?
Bitboard Game::genMoves(Piece const& piece) {
Bitboard moveSpace = 0x00;
//Will need to be modified for moves where the king is in check
if (pieceDatCache[&piece].boardIntersect ==
piece.getMoveRange() & gameBoard.getWholeBoard() &&
piece.getType() != PieceType::King) {
return pieceDatCache[&piece].moveSpace &
~gameBoard.getColorBoard(piece.getColor());
}
switch (piece.getType()) {
case (PieceType::King) : {
moveSpace = genKingMoves(piece);
break;
}
case (PieceType::Queen) : {
moveSpace = genStraightMoves(piece) | genDiagonalMoves(piece);
break;
}
case (PieceType::Rook) : {
moveSpace = genStraightMoves(piece);
break;
}
case (PieceType::Bishop) : {
moveSpace = genDiagonalMoves(piece);
break;
}
case (PieceType::Knight) : {
moveSpace = genKnightMoves(piece);
break;
}
case (PieceType::Pawn) : {
moveSpace = genPawnMoves(piece);
break;
}
}
pieceDatCache[&piece].boardIntersect = piece.getMoveRange() & gameBoard.getWholeBoard();
pieceDatCache[&piece].moveSpace = moveSpace;
moveSpace &= ~gameBoard.getColorBoard(piece.getColor());
return moveSpace;
}
Bitboard Game::genStraightMoves(Piece const& piece) const {
Bitboard moveSpace = 0x00;
moveSpace |= genMoveSpacePart(Pieces::MoveTables::upStraight
[piece.getPos().column][piece.getPos().row], true);
moveSpace |= genMoveSpacePart(Pieces::MoveTables::downStraight
[piece.getPos().column][piece.getPos().row], false);
moveSpace |= genMoveSpacePart(Pieces::MoveTables::leftStraight
[piece.getPos().column][piece.getPos().row], true);
moveSpace |= genMoveSpacePart(Pieces::MoveTables::rightStraight
[piece.getPos().column][piece.getPos().row], false);
return moveSpace;
}
Bitboard Game::genDiagonalMoves(Piece const& piece) const {
Bitboard moveSpace = 0x00;
moveSpace |= genMoveSpacePart(Pieces::MoveTables::upRight
[piece.getPos().column][piece.getPos().row], true);
moveSpace |= genMoveSpacePart(Pieces::MoveTables::upLeft
[piece.getPos().column][piece.getPos().row], true);
moveSpace |= genMoveSpacePart(Pieces::MoveTables::downRight
[piece.getPos().column][piece.getPos().row], false);
moveSpace |= genMoveSpacePart(Pieces::MoveTables::downLeft
[piece.getPos().column][piece.getPos().row], false);
return moveSpace;
}
/*Wether to use MSB or use LSB for finding
the nearest intersected piece*/
Bitboard Game::genMoveSpacePart(Bitboard rangePart, bool spansUp) const {
Bitboard intersect = rangePart & gameBoard.getWholeBoard();
if (!intersect)
return rangePart;
/*Select the closest piece to the piece
we are generating move data for...*/
uint8_t targetBitIdx = spansUp ?
std::countr_zero(intersect) :
63 - std::countl_zero(intersect);
Bitboard targetPiece = 0b01;
targetPiece <<= targetBitIdx;
Bitboard rangePartIdx = spansUp ?
std::countr_zero(rangePart) :
63 - std::countl_zero(rangePart);
Bitboard clipMask = rangePart;
//Make branchless later
if (spansUp) {
clipMask <<= targetBitIdx - rangePartIdx;
} else {
clipMask >>= rangePartIdx - targetBitIdx;
}
//Preserve the target piece bit
Bitboard moveSpacePart = rangePart & ~clipMask /*& ~tailClipMask*/ | targetPiece;
return moveSpacePart;
}
Bitboard Game::genPawnMoves(Piece const& piece) const {
Bitboard moveSpace = 0x00;
//Prevent pawns on one end column of the board
//from bleeding into the opposite column
Bitboard constexpr rightClip = 0xFEFEFEFEFEFEFEFE;
Bitboard constexpr leftClip = 0X7F7F7F7F7F7F7F7F;
bool movesUp = piece.getColor() == Color::White;
/*No need to check if the pawn is at the end of the board,
since it will always be promoted to a different piece*/
Bitboard moveTile;
moveTile = movesUp ?
piece.getBBoard() << 8 : piece.getBBoard() >> 8;
moveSpace |= (moveTile & ~gameBoard.getWholeBoard());
moveTile = movesUp ?
piece.getBBoard() << 16 : piece.getBBoard() >> 16;
if (!piece.hasMoved && !(moveTile & gameBoard.getWholeBoard()))
moveSpace |= moveTile;
moveSpace |= genPawnThreat(piece);
return moveSpace;
}
Bitboard Game::genPawnThreat(Piece const& piece) const {
Bitboard constexpr rightClip = 0xFEFEFEFEFEFEFEFE;
Bitboard constexpr leftClip = 0X7F7F7F7F7F7F7F7F;
bool movesUp = piece.getColor() == Color::White;
Bitboard threatBB = 0x00;
Bitboard moveTile;
moveTile = movesUp ?
(piece.getBBoard() << 9)&rightClip : (piece.getBBoard() >> 9)&leftClip;
threatBB |= moveTile & gameBoard.getColorBoard(!piece.getColor());
moveTile = movesUp ?
piece.getBBoard() << 7 : piece.getBBoard() >> 7;
threatBB |= moveTile & gameBoard.getColorBoard(!piece.getColor());
if (numMoves == 0)
return threatBB;
//en passe
//Sometimes causes seg fault and doesn't work yet... disabled for now
Pos adjRight({uint8_t (piece.getPos().column + 1), piece.getPos().row});
Pos adjLeft({uint8_t (piece.getPos().column - 1), piece.getPos().row});
if (piece.getPos().column < 7 && gameBoard.squareOccupied(adjRight)) {
Piece const& adjPiece = gameBoard.getPiece(adjRight);
//this is dirty as fuck
const_cast<Game*>(this)->gameBoard.undoMove();
if (adjPiece.getType() == PieceType::Pawn &&
adjPiece.getColor() != piece.getColor() &&
std::abs(adjPiece.getPos().row - adjRight.row) == 2) {
threatBB |= movesUp ? piece.getBBoard() << 7 : piece.getBBoard() >> 9;
}
//redo the move we undid while processing
const_cast<Game*>(this)->gameBoard.redoMove();
}
if (piece.getPos().column > 0 && gameBoard.squareOccupied(adjLeft)) {
Piece const& adjPiece = gameBoard.getPiece(adjLeft);
const_cast<Game*>(this)->gameBoard.undoMove();
if (adjPiece.getType() == PieceType::Pawn &&
adjPiece.getColor() != piece.getColor() &&
std::abs(adjPiece.getPos().row - adjLeft.row) == 2) {
threatBB |= movesUp ? piece.getBBoard() << 9 : piece.getBBoard() >> 7;
}
const_cast<Game*>(this)->gameBoard.redoMove();
}
return threatBB;
}
//Should I ditch these?!?
Bitboard Game::genKnightMoves(Piece const& piece) const {
return piece.getMoveRange();
}
Bitboard Game::genKingMoves(Piece const& piece) const {
return piece.getMoveRange() | genCastleMoves(piece);
}
void Game::movePiece(Pos start, Pos end) {
if (!gameBoard.squareOccupied(start)) {
std::cerr << "Error: illegal move" << std::endl;
return;
}
Piece const& piece = gameBoard.getPiece(start);
if (!(genMoves(gameBoard.getPiece(start)) & end.asBitBoard())) {
std::cerr << "Error: illegal move" << std::endl;
return;
}
if (piece.getColor() != (status == GameStatus::BlackTurn ? Color::Black : Color::White)) {
std::cerr << "Error: illegal move" << std::endl;
return;
}
gameBoard.movePiece(start, end);
numMoves++;
if (isCheck(piece.getColor())) {
std::cerr << "Error: illegal move" << std::endl;
undoMove();
return;
}
status = (status == GameStatus::BlackTurn ?
GameStatus::WhiteTurn : GameStatus::BlackTurn);
}
//Note: this is ONLY FOR USE BY THE KING
Bitboard Game::genCastleMoves(Piece const& piece) const {
Bitboard castleMask = 0;
uint8_t const row = piece.getColor() == Color::Black ? 7 : 0;
//Mask of space between the king and the rooks
Bitboard const lSpaceMask = (uint64_t) 0x70 << row * 8;
Bitboard const rSpaceMask = (uint64_t) 0x06 << row * 8;
Bitboard const lCastleMask = (uint64_t) 0x20 << row * 8;
Bitboard const rCastleMask = (uint64_t) 0x02 << row * 8;
auto checkConds = [&](Piece const* piece, PieceType type, Bitboard mask = 0x00) -> bool {
return piece && piece->getType() == type &&
!piece->hasMoved && !(gameBoard.getWholeBoard() & mask);
};
Piece const* king = &gameBoard.getPiece({4, row});
if (!checkConds(king, PieceType::King))
return 0;
std::pair<Piece const*, Piece const*> rooks = std::make_pair(
&gameBoard.getPiece({0, row}),
&gameBoard.getPiece({7, row})
);
if (checkConds(rooks.first, PieceType::Rook, lSpaceMask)) {
castleMask |= lCastleMask;
}
if (checkConds(rooks.second, PieceType::Rook, rSpaceMask)) {
castleMask |= rCastleMask;
}
return castleMask;
}
Bitboard Game::getMovesFromPos(Pos pos) {
return genMoves(gameBoard.getPiece(pos));
}
bool Game::isCheck(Color color, std::optional<Pos> kingPosHint) {
Bitboard threatBB = 0;
Bitboard kingBB = kingPosHint.has_value() ? kingPosHint.value().asBitBoard() : 0;
for (uint8_t col = 0; col < 8; col++) {
for (uint8_t row = 0; row < 8; row++) {
if (gameBoard.squareOccupied({col, row})) {
Piece const& piece = gameBoard.getPiece({col, row});
if (!kingBB && piece.getType() == PieceType::King &&
piece.getColor() == color) { kingBB = piece.getBBoard(); }
if (piece.getColor() == !color) {
//Pawns have distinct attack and normal moves
threatBB |= piece.getType() != PieceType::Pawn ?
genMoves(piece) : genPawnThreat(piece);
if (threatBB & kingBB)
return true;
}
}
}
}
return false;
}
std::string Game::gameOutput() {
std::ostringstream stream;
stream << "Will Smith vs Chris Rock:\n\n";
switch (status) {
case (GameStatus::WhiteTurn) : {
stream << "White's turn";
break;
}
case (GameStatus::BlackTurn) : {
stream << "Black's turn";
break;
}
}
stream << std::endl;
stream << gameBoard;
if (isCheck(Color::Black))
stream << "Black is in check!" << std::endl;
if (isCheck(Color::White))
stream << "White is in check!" << std::endl;
return stream.str();
}
Game::Game() {
for (uint8_t col = 0; col < 8; col++) {
for (uint8_t row = 0; row < 8; row++) {
if (gameBoard.squareOccupied({col, row})) {
pieceDatCache.insert({&gameBoard.getPiece({col, row}), {0, 0}});
}
}
}
}