-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameController.cpp
More file actions
540 lines (446 loc) · 21.9 KB
/
Copy pathGameController.cpp
File metadata and controls
540 lines (446 loc) · 21.9 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
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
#include "GameController.h"
void GameController::setup() noexcept {
const auto& BLACK = Piece::Colour::BLACK;
const auto& WHITE = Piece::Colour::WHITE;
auto& board = game.board;
for (const auto& colour : {WHITE, BLACK}){
const std::string pawnRow = (colour == WHITE ? "2" : "7"); // NB: didn't use gsl::index as don't want to risk confusing with ACTUAL indices (1, 6 respectively)
const std::string pieceRow = (colour == WHITE ? "1" : "8");
for (Location location{"A" + pawnRow}; location <= Location{"H" + pawnRow}; ++location) {
board.insert(location, std::make_unique<Pawn>(colour));
}
for (Location location: {Location{"A" + pieceRow}, Location{"H" + pieceRow}}) {
board.insert(location, std::make_unique<Rook>(colour));
}
for (Location location: {Location{"B" + pieceRow}, Location{"G" + pieceRow}}) {
board.insert(location, std::make_unique<Knight>(colour));
}
for (Location location: {Location{"C" + pieceRow}, Location{"F" + pieceRow}}) {
board.insert(location, std::make_unique<Bishop>(colour));
}
board.insert(Location{"D" + pieceRow}, std::make_unique<Queen>(colour));
board.insert(Location{"E" + pieceRow}, std::make_unique<King>(colour));
}
}
void GameController::setupSimple() noexcept {
/// For debugging
const auto& BLACK = Piece::Colour::BLACK;
const auto& WHITE = Piece::Colour::WHITE;
auto& board = game.board;
board.insert(Location{"H7"}, std::make_unique<Pawn>(WHITE));
board.insert(Location{"H2"}, std::make_unique<Pawn>(BLACK));
board.insert(Location{"A3"}, std::make_unique<King>(WHITE));
board.insert(Location{"D2"}, std::make_unique<Knight>(WHITE));
board.insert(Location{"A1"}, std::make_unique<King>(BLACK));
}
void GameController::makeMove(const Location &source, const Location &destination, const Piece* promotionPiece = nullptr) noexcept {
Board& board = game.board;
board.erase(destination); // in case piece already there
board.insert(destination, std::move(board[source]));
board.erase(source);
// logistics for special moves
// 1. en passant
if (isEnPassant(source, destination)) {
board.erase(game.enPassantTargetSquare);
return;
}
// 2. castling
if (King::isValidCastlingPath(source, destination)) {
handleRookCastlingMove(destination);
return;
}
// 3. pawn promotion
if (promotionPiece == nullptr) return;
board.erase(destination); // erase pawn at back row
board.insert(destination, promotionPiece->clone());
}
void GameController::submitMove(const Location &source, const Location &destination, const Piece* const promotionPiece = nullptr) noexcept {
// pre-move validation <- TODO: Low priority, put calcMoveValidityStatus and moveLeavesMoverInCheck in one function
if (auto result = calcMoveValidityStatus(game.activePlayer, source, destination, promotionPiece); !result.isValid) {
gameView->displayException(std::runtime_error("ERROR: " + result.reason));
return;
}
if (moveLeavesMoverInCheck(source, destination)) {
gameView->displayException(std::runtime_error("ERROR: Move leaves mover in check"));
return;
}
const std::unique_ptr<Piece> pieceMoved = game.board.pieceAt(source)->clone(); // now we know it's valid we're safe to assign pieceMoved
// move
makeMove(source, destination, promotionPiece);
// post-move things that need sorting
updateCastingAvailability(*pieceMoved, source);
setEnPassantTargetSquare(source, destination);
swapActivePlayer();
}
void GameController::swapActivePlayer() noexcept {
game.activePlayer = ((game.activePlayer == game.whitePlayer) ? game.blackPlayer : game.whitePlayer);
}
GameController::MoveValidityStatus GameController::calcMoveValidityStatus(const Player& player, const Location &source, const Location &destination, const Piece* promotionPiece = nullptr) const noexcept {
const auto& board = game.board;
const auto& moversColour = player.getColour();
const bool isDirectCapture = board.thereExistsPieceAt(destination); // i.e. capture that's not an en passant
// universal conditions
if (!board.thereExistsPieceAt(source)) {
return {.isValid = false, .reason = "No piece at source square"};
}
if (board.pieceAt(source)->getColour() != moversColour) {
return {.isValid = false, .reason = "Moving wrong colour piece"};
}
if (board.thereExistsPieceAt(destination) && board[destination]->getColour() == moversColour) {
return {.isValid = false, .reason = "Can't take your own piece"};
}
if (!board[source]->isValidMovePath(source, destination, game.enPassantTargetSquare, isDirectCapture)) {
return {.isValid = false, .reason = "Piece can't move that way"};
}
if (board.isPathBlocked(source, destination)) {
return {.isValid = false, .reason = "Path blocked"};
}
// piece-dependant conditions
// castling
if (King::isValidCastlingPath(source, destination) && !isValidCastling(source, destination)) {
return {.isValid = false, .reason = "Invalid castling attempt"};
}
// pawn promotion
if (isType<Pawn>(*board.pieceAt(source)) && isBackRow(destination, player)) { // if (pawn moves to back row) ...
const bool validPromotionPiece = isValidPromotionPiece(promotionPiece, player);
if (!validPromotionPiece) {
return {.isValid = false, .reason = "Invalid promotion piece"};
}
return {.isValid = true};
}
if (promotionPiece) {
return {.isValid = false, .reason = "Move includes promotion piece but can't promote"};
}
return {.isValid = true};
}
bool GameController::moveLeavesMoverInCheck(const Location &source, const Location &destination) const noexcept {
GameController copy {*this};
copy.makeMove(source, destination);
bool returnValue = copy.inCheck(copy.game.activePlayer);
return returnValue;
}
void GameController::setEnPassantTargetSquare(const Location &source, const Location &destination) noexcept {
const Location::RowColumnDifferences locationDifferences = Location::calculateRowColumnDifferences(source, destination);
game.enPassantTargetSquare = [&](){
if (isType<Pawn>(*game.board.pieceAt(destination)) && abs(locationDifferences.rowDifference) == 2) {
return destination;
}
return Location{};
}();
}
bool GameController::isValidCastling(const Location &source, const Location &destination) const noexcept {
if (!King::isValidCastlingPath(source, destination)) return false;
if (destination == Location{"C1"}) {
return game.whiteCastlingAvailability.queenSide && !game.board.thereExistsPieceAt(Location{"B1"}); // !game.board.thereExistsPieceAt(Location{"B1"}) as not handled by isPathBlocked()
} else if (destination == Location{"C8"}) {
return game.blackCastlingAvailability.queenSide && !game.board.thereExistsPieceAt(Location{"B8"}); // !game.board.thereExistsPieceAt(Location{"B1"}) as not handled by isPathBlocked()
} else if (destination == Location{"G1"}) {
return game.whiteCastlingAvailability.kingSide;
} else if (destination == Location{"G8"}) {
return game.blackCastlingAvailability.kingSide;
}
return false;
}
void GameController::updateCastingAvailability(const Piece& pieceMoved, const Location &source) noexcept {
if (isType<King>(pieceMoved)) {
if (game.activePlayer.getColour() == Piece::Colour::WHITE) {
game.whiteCastlingAvailability = { .kingSide = false, .queenSide = false };
} else {
game.blackCastlingAvailability = { .kingSide = false, .queenSide = false };
}
} else if (isType<Rook>(pieceMoved)) {
if (game.activePlayer.getColour() == Piece::Colour::WHITE) {
if (source == Location("A1")) {
game.whiteCastlingAvailability.queenSide = false;
} else if (source == Location("H1")) {
game.whiteCastlingAvailability.kingSide = false;
}
} else {
if (source == Location("A8")) {
game.blackCastlingAvailability.queenSide = false;
} else if (source == Location("H8")) {
game.blackCastlingAvailability.kingSide = false;
}
}
}
}
bool GameController::isEnPassant(const Location &source, const Location &destination) const noexcept{
const auto& [sourceRow, sourceColumn] = source;
const auto& [destinationRow, destinationColumn] = destination;
return (isType<Pawn>(*game.board.pieceAt(destination))
&& game.enPassantTargetSquare == Location{sourceRow.value(), destinationColumn.value()});
}
void GameController::handleRookCastlingMove(const Location &destination) noexcept{
Board& board = game.board;
if (destination == Location{"C1"}) { //whiteCastingAvailability.queenSide;
board.insert(Location{"D1"}, board.pieceAt(Location{"A1"})->clone());
board.erase(Location{"A1"});
}
else if (destination == Location{"G1"}) { //whiteCastingAvailability.kingSide;
board.insert(Location{"F1"}, board.pieceAt(Location{"H1"})->clone());
board.erase(Location{"H1"});
}
else if (destination == Location{"C8"}) { //blackCastingAvailability.queenSide;
board.insert(Location{"D8"}, board.pieceAt(Location{"A8"})->clone());
board.erase(Location{"A8"});
}
else { // (destination == Location("G8")) //blackCastingAvailability.kingSide;
board.insert(Location{"F8"}, board.pieceAt(Location{"H8"})->clone());
board.erase(Location{"H8"});
}
}
Game::GameState GameController::calculateGameState() const noexcept{
if (thereExistsValidMove(game.activePlayer)) {
// todo: implement additional draw conditions
if (isDrawByInsufficientMaterial() /*|| isThreeFoldRepetition() || isFiftyMoveRule()*/) {
return Game::GameState::DRAW;
}
return Game::GameState::IN_PROGRESS;
}
else {
if (inCheck(game.activePlayer)) {
return game.activePlayer.getColour() == Piece::Colour::WHITE
? Game::GameState::BLACK_WIN
: Game::GameState::WHITE_WIN;
}
else {
return Game::GameState::STALEMATE;
}
}
}
bool GameController::thereExistsValidMove(const Player& activePlayer) const noexcept{
GameController copy {*this};
for (const auto& [source, piece] : copy.game.board) {
if (piece->getColour() != activePlayer.getColour()) continue;
for (Location destination = Location{"A1"}; destination <= Location{"H8"}; ++destination) {
if (copy.calcMoveValidityStatus(activePlayer, source, destination).isValid
&& !copy.moveLeavesMoverInCheck(source, destination)) {
return true;
}
}
}
return false;
}
void GameController::initGameLoop() noexcept {
while (game.gameState == Game::GameState::IN_PROGRESS) {
gameView->viewBoard(game.board);
try {
gameView->displayTurn(game.activePlayer);
const auto& [source, destination, promotionPiece] = getMoveInfoFromUser();
const Player preMoveActivePlayer = game.activePlayer;
submitMove(source, destination, promotionPiece.get());
// TODO: Low priority, submitMove -> bool submitMoveAndReturnSuccessStatus()??
if (preMoveActivePlayer != game.activePlayer) {
game.gameState = calculateGameState();
}
}
catch (const std::exception& e) {
gameView->displayException(e);
}
}
gameView->viewBoard(game.board);
gameView->displayEndOfGameMessage(game.gameState);
}
bool GameController::inCheck(const Player& player) const noexcept {
const Player& opponent = (player.getColour() == Piece::Colour::WHITE ? game.blackPlayer : game.whitePlayer);
return isUnderAttackBy(getLocationOfKing(player),opponent);
}
void GameController::manualSetup() noexcept {
/// NB: Implementing has the potential to be a huge rabbit-hole so *mostly* assumes a valid position
/// (e.g. doesn't cover both players in check/checkmate, pawns on back ranks, person in check when opponent's turn)
/// Checks players have exactly one king as, out of all the ways to be invalid, this one would be most problematic
gameView->viewBoard(game.board);
std::cout << '\n';
while (toupper(gameView->readInput("Add another piece? (Any key for Yes, 'N' for no): ")[0], std::locale()) != 'N') {
std::unique_ptr<Piece> selectedPiece = getPieceFromUser("Enter piece char (eg. 'K', 'k'): ");
const Location selectedLocation = getLocationFromUser("Place at location: ");
game.board.insert(selectedLocation, std::move(selectedPiece));
gameView->viewBoard(game.board);
}
const bool eachHaveExactlyOneKing = std::invoke([&](){
const auto& board = game.board.board;
size_t whiteKingCount = 0, blackKingCount = 0;
std::for_each(cbegin(board), cend(board), [&](const auto& it){
const auto& piece = it.second;
if (isType<King>(*piece)) {
(piece->getColour() == Piece::Colour::WHITE) ? ++whiteKingCount : ++blackKingCount;
}
});
return whiteKingCount == 1 && blackKingCount == 1;
});
if (!eachHaveExactlyOneKing) {
gameView->displayException(std::runtime_error("Invalid Position: Each player must have exactly one king. Clearing board..."));
game.board.board.clear();
return;
}
game.activePlayer = getStartingPlayer();
}
GameController::GameController(const GameController &rhs)
: game{rhs.game}, gameView{rhs.gameView->clone()} { }
GameController &GameController::operator=(const GameController &rhs) {
gameView = rhs.gameView->clone();
game.board.board.clear(); // bug fix for moveLeavesMoverInCheck() where `*this = copy` didn't remove the moved piece
game = rhs.game;
return *this;
}
std::map<char, PieceFactory> GameController::createPieceFactories() noexcept {
std::map<char, PieceFactory> temp;
temp['P'] = [](Piece::Colour color) { return std::make_unique<Pawn>(color); };
temp['B'] = [](Piece::Colour color) { return std::make_unique<Bishop>(color); };
temp['N'] = [](Piece::Colour color) { return std::make_unique<Knight>(color); };
temp['R'] = [](Piece::Colour color) { return std::make_unique<Rook>(color); };
temp['Q'] = [](Piece::Colour color) { return std::make_unique<Queen>(color); };
temp['K'] = [](Piece::Colour color) { return std::make_unique<King>(color); };
return temp;
}
const std::map<char, PieceFactory> GameController::pieceFactories = createPieceFactories();
bool GameController::isBackRow(const Location &square, const Player &player) const noexcept {
if (player == game.whitePlayer) {
return square.getBoardRowIndex() == Location::getMaxRowIndex();
}
return square.getBoardRowIndex() == 0;
}
Game::MoveInfo GameController::getMoveInfoFromUser() const noexcept {
const auto source = getLocationFromUser("Type source square: ");
const auto destination = getLocationFromUser("Type destination square: ");
auto promotionPiece = std::invoke([&]() -> std::unique_ptr<Piece> {
if (!isType<Pawn>(*game.board.pieceAt(source)) || !isBackRow(destination, game.activePlayer)) {
return nullptr;
}
while (true) {
auto piece = getPieceFromUser("Enter promotion piece char (eg. 'Q', 'R'): ");
if (!isType<King>(*piece)
&& !isType<Pawn>(*piece)
&& piece->getColour() == game.activePlayer.getColour()){
return piece;
} else {
gameView->displayException(std::runtime_error("Invalid Promotion Piece (wrong piece type and/or colour)"));
}
}
});
return {source, destination, std::move(promotionPiece)};
}
Location GameController::getLocationFromUser(std::string_view message) const noexcept {
while (true) {
try {
return Location{gameView->readInput(message)};
}
catch (const std::exception& e) {
gameView->displayException(e);
}
}
}
std::unique_ptr<Piece> GameController::getPieceFromUser(std::string_view message) const noexcept {
while (true) {
const char pieceChar = gameView->readInput(message)[0];
const char pieceCode = toupper(pieceChar, std::locale());
if (pieceFactories.find(pieceCode) != pieceFactories.end()) {
const auto colour = (isupper(pieceChar) ? Piece::Colour::WHITE : Piece::Colour::BLACK);
return pieceFactories.at(pieceCode)(colour);
} else {
gameView->displayException(std::runtime_error("Entered invalid char"));
}
}
}
Location GameController::getLocationOfKing(const Player &player) const noexcept {
const auto& board = game.board.board;
const auto& kingColour = player.getColour();
auto it = std::find_if(board.begin(), board.end(), [&](const auto& entry) {
const auto& piece = entry.second;
return (piece->getColour() == kingColour) && (isType<King>(*piece));
});
return (it != board.end() ? it->first : Location{});
}
bool GameController::isUnderAttackBy(Location target, const Player &opponent) const noexcept {
// NB: a square isn't marked as under attack if the attacker has a piece there.
// En passant target squares are also not accounted for, but as isUnderAttack is a used in inCheck() and validMove()
// and you cant castle through an en passant target square, this is a moot issue
const auto& board = game.board.board;
auto isOpponentPieceAttackingTarget = [&](const auto& it) -> bool {
const auto& [source, sourcePiece] = it;
if (sourcePiece.get()->getColour() != opponent.getColour()) return false;
return calcMoveValidityStatus(opponent, source, target).isValid;
};
return std::any_of(board.cbegin(), board.cend(), isOpponentPieceAttackingTarget);
}
Player GameController::getStartingPlayer() const noexcept {
while (true) {
const char startColour = toupper(gameView->readInput("Enter starting colour ('W' or 'B'): ")[0], std::locale());
if (startColour == 'W') { return game.whitePlayer; }
else if (startColour == 'B') { return game.blackPlayer; }
else { gameView->displayException(std::runtime_error("Starting colour char not recognised"));}
}
}
bool GameController::isDrawByInsufficientMaterial() const noexcept {
/** NB: This innocent-seeming function is more complex to implement in accordance with FIDE rules than it
* may seem, due to the following also being classed as "Insufficient Material"
*
* FIDE rules (article 6.9)
* "However, the game is drawn if the position is such that the opponent cannot checkmate the player’s king
* by any possible series of legal moves."
*
* This means the position 4k3/8/8/1p2p2p/1P2P2P/8/8/4K3 (FEN notation) is also classed as insufficient material,
* despite many pieces being on the board.
*
* It's worth noting that this is so low-priority that, at the time of writing this (28 Oct 2023),
* neither of the two largest chess sites chess.com and lichess.com have implemented this rule
* TODO: Low priority, Integrate a 'helpmate analyser' to fix this.
*/
const bool thereExistsAPawnOrMajorPiece = [&](){
auto& board = game.board.board;
return std::any_of(begin(board), end(board), [](const auto& it) {
const auto& piece = it.second;
return isType<Rook>(*piece) || isType<Queen>(*piece) || isType<Pawn>(*piece);
});
}();
if (thereExistsAPawnOrMajorPiece) return false;
struct MinorPieceCount {
size_t whiteBishopCount, whiteKnightCount, blackBishopCount, blackKnightCount;
};
const MinorPieceCount minorPieceCount = [&](){
MinorPieceCount count {0,0,0,0};
auto& board = game.board.board;
std::for_each(cbegin(board), cend(board), [&](const auto& it){
const auto& piece = it.second;
if (isType<Bishop>(*piece)) {
(piece->getColour() == Piece::Colour::WHITE) ? ++count.whiteBishopCount : ++count.blackBishopCount;
}
else if (isType<Knight>(*piece)) {
(piece->getColour() == Piece::Colour::WHITE) ? ++count.whiteKnightCount : ++count.blackKnightCount;
}
});
return count;
}();
const auto totalWhiteMinorPieces = minorPieceCount.whiteBishopCount + minorPieceCount.whiteKnightCount;
const auto totalBlackMinorPieces = minorPieceCount.blackBishopCount + minorPieceCount.blackKnightCount;
// trivial draw conditions
if (totalWhiteMinorPieces == 1 && totalBlackMinorPieces == 1) return true;
if (totalWhiteMinorPieces > 2 || totalBlackMinorPieces > 2) return true;
// if either side has a lone king
if (totalWhiteMinorPieces == 0 || totalBlackMinorPieces == 0) {
if ((totalWhiteMinorPieces == totalBlackMinorPieces)
|| (totalWhiteMinorPieces == 1 || totalBlackMinorPieces == 1)
|| (minorPieceCount.whiteKnightCount == 2 || minorPieceCount.blackKnightCount == 2)) {
return true;
}
}
// if (1 minor VS 2 minor)
// 2 minor pieces against one results in a draw, except when the stronger side has a bishop Pair
if (totalWhiteMinorPieces == 1 && totalBlackMinorPieces == 2
|| totalWhiteMinorPieces == 2
&& totalBlackMinorPieces == 1)
{
return minorPieceCount.whiteBishopCount != 2 && minorPieceCount.blackBishopCount != 2;
}
return false;
}
void GameController::displayAllUnderAttackBy(const Player &player) noexcept {
Game copy {game};
copy.board.board.clear();
for (Location i = Location{"A1"}; i <= Location{"H8"}; ++i) {
if (isUnderAttackBy(i, player)) {
copy.board.insert(i, std::make_unique<Pawn>(Piece::Colour::WHITE));
}
}
gameView->viewBoard(copy.board);
}