From 1e5a34d8d2befa5786adc30e99d85372510f017f Mon Sep 17 00:00:00 2001 From: Loic Guegan Date: Thu, 17 Feb 2022 18:01:06 +0100 Subject: [PATCH] Debug castling --- src/ChessArbiter.cpp | 41 +++++++++++++++++++++++++------------ tests/chessarbiter.cpp | 46 +++++++++++++++++++++++++++++++++--------- 2 files changed, 65 insertions(+), 22 deletions(-) diff --git a/src/ChessArbiter.cpp b/src/ChessArbiter.cpp index 1c04bed..21d0bfa 100644 --- a/src/ChessArbiter.cpp +++ b/src/ChessArbiter.cpp @@ -3,7 +3,7 @@ namespace chessarbiter { ChessArbiter::ChessArbiter() : wPawn(1), wRook(5), wKnight(3), wBishop(3), wQueen(9), wKing(0), SAN(""), - capture(' '){} + capture(' ') {} void ChessArbiter::Setup(std::string fen) { positions.clear(); @@ -47,28 +47,32 @@ bool ChessArbiter::Play(std::string move) { FEN newFen = fen; INIT_BACKUP(); SAN = ""; - capture=' '; + capture = ' '; if (IsCapture) { capture = board.GetPieceAt(dst).piece; } // Perform the move - if (move == "O-O" || move == "O-O-O") { - if (fen.player && move == "O-O") { + if ((moved.piece == 'k' || moved.piece == 'K') && move == "e8g8" || + move == "e8c8" || move == "e1g1" || move == "e1c1") { + if (move == "e8g8") { board.Move("e8g8"); - board.Move("h8e8"); - } else if (fen.player && move == "O-O") { + board.Move("h8f8"); + SAN = "O-O"; + } else if (move == "e8c8") { board.Move("e8c8"); board.Move("a8d8"); - } else if (!fen.player && move == "O-O") { + SAN = "O-O-O"; + } else if (move == "e1g1") { board.Move("e1g1"); - board.Move("h1e1"); + board.Move("h1f1"); + SAN = "O-O"; } else { board.Move("e1c1"); board.Move("a1d1"); + SAN = "O-O-O"; } - SAN = move; } else { // Update SAN move if (moved.piece == 'p' || moved.piece == 'P') { @@ -312,10 +316,21 @@ std::vector ChessArbiter::ListLegalMoves(bool isBlack) { } // Casling - if (IsCastlePossible(isBlack, false)) - moves.push_back("O-O"); - if (IsCastlePossible(isBlack, true)) - moves.push_back("O-O-O"); + if (isBlack) { + if (IsCastlePossible(isBlack, false)) { + moves.push_back("e8g8"); + } + if (IsCastlePossible(isBlack, true)) { + moves.push_back("e8c8"); + } + } else { + if (IsCastlePossible(isBlack, false)) { + moves.push_back("e1g1"); + } + if (IsCastlePossible(isBlack, true)) { + moves.push_back("e1c1"); + } + } return (moves); } diff --git a/tests/chessarbiter.cpp b/tests/chessarbiter.cpp index 48e58fa..c5206d7 100644 --- a/tests/chessarbiter.cpp +++ b/tests/chessarbiter.cpp @@ -180,43 +180,43 @@ TEST_CASE("ListLegalMoves", "[chessarbiter/ListLegalMoves]") { a.Setup("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQK11R w KQkq - 0 1"); moves = a.ListLegalMoves(false); REQUIRE(moves.size() == 22); - CHECK(std::find(moves.begin(), moves.end(), "O-O") != moves.end()); + CHECK(std::find(moves.begin(), moves.end(), "e1g1") != moves.end()); // White Short Castle impossible a.Setup("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQK11R w Qkq - 0 1"); moves = a.ListLegalMoves(false); - CHECK_FALSE(std::find(moves.begin(), moves.end(), "O-O") != moves.end()); + CHECK_FALSE(std::find(moves.begin(), moves.end(), "e1g1") != moves.end()); // White Short Castle impossible 2 a.Setup("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQK1NR w KQkq - 0 1"); moves = a.ListLegalMoves(false); - CHECK_FALSE(std::find(moves.begin(), moves.end(), "O-O") != moves.end()); + CHECK_FALSE(std::find(moves.begin(), moves.end(), "e1g1") != moves.end()); // White Short Castle impossible 3 (queen attacks by black) a.Setup("rnbqkbnr/pppppqpp/8/8/8/8/PPPPP1PP/RNBQK11R w KQkq - 0 1"); moves = a.ListLegalMoves(false); - CHECK_FALSE(std::find(moves.begin(), moves.end(), "O-O") != moves.end()); + CHECK_FALSE(std::find(moves.begin(), moves.end(), "e1g1") != moves.end()); // White Long Castle possible a.Setup("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/R3KNBR w KQkq - 0 1"); moves = a.ListLegalMoves(false); REQUIRE(moves.size() == 23); - CHECK(find(moves.begin(), moves.end(), "O-O-O") != moves.end()); + CHECK(find(moves.begin(), moves.end(), "e1c1") != moves.end()); // White Long Castle impossible a.Setup("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/R3KBNR w Kkq - 0 1"); moves = a.ListLegalMoves(false); - CHECK_FALSE(std::find(moves.begin(), moves.end(), "O-O-O") != moves.end()); + CHECK_FALSE(std::find(moves.begin(), moves.end(), "e1c1") != moves.end()); // White Long Castle impossible 2 a.Setup("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RN2KBNR w KQkq - 0 1"); moves = a.ListLegalMoves(false); - CHECK_FALSE(std::find(moves.begin(), moves.end(), "O-O-O") != moves.end()); + CHECK_FALSE(std::find(moves.begin(), moves.end(), "e1c1") != moves.end()); // White Long Castle impossible 3 (rook attacks by black) a.Setup("rnbqkbnr/pprppppp/8/8/8/8/PP1PPPPP/R3KBNR w KQkq - 0 1"); moves = a.ListLegalMoves(false); - CHECK_FALSE(std::find(moves.begin(), moves.end(), "O-O-O") != moves.end()); + CHECK_FALSE(std::find(moves.begin(), moves.end(), "e1c1") != moves.end()); } TEST_CASE("IsPlayable", "[chessarbiter/IsPlayable]") { @@ -367,5 +367,33 @@ TEST_CASE("SimpleCapture", "[SimplePieceCapture]") { a.Play("e4d5"); CHECK(a.GetFEN() == "rnbqkbnr/ppp1pppp/8/3P4/8/8/PPPP1PPP/RNBQKBNR b KQkq - 0 2"); - CHECK(a.GetCapture()=='p'); + CHECK(a.GetCapture() == 'p'); +} + +TEST_CASE("SimpleCastle", "[SimpleCastle]") { + ChessArbiter a; + a.Setup("rnbqkbnr/ppp1p1pp/8/3p1p2/4P3/3B1N2/PPPP1PPP/RNBQK2R w KQkq - 0 2"); + + // White + CHECK(a.Play("e1g1")); + CHECK(a.GetFEN() == + "rnbqkbnr/ppp1p1pp/8/3p1p2/4P3/3B1N2/PPPP1PPP/RNBQ1RK1 b kq - 1 2"); + + a.Setup("rnbqkbnr/ppp1p1pp/8/3p1p2/4P3/1BNB1N2/PPPPQPPP/R3K2R w KQkq - 0 2"); + CHECK(a.Play("e1c1")); + CHECK(a.GetFEN() == + "rnbqkbnr/ppp1p1pp/8/3p1p2/4P3/1BNB1N2/PPPPQPPP/2KR3R b kq - 1 2"); + + // Black + a.Setup( + "r3k2r/pppnp1pp/1bq1bn2/3p1p2/4P3/1BNB1N2/PPPPQPPP/R3K2R b KQkq - 0 2"); + CHECK(a.Play("e8g8")); + CHECK(a.GetFEN() == + "r4rk1/pppnp1pp/1bq1bn2/3p1p2/4P3/1BNB1N2/PPPPQPPP/R3K2R w KQ - 1 3"); + + a.Setup( + "r3k2r/pppnp1pp/1bq1bn2/3p1p2/4P3/1BNB1N2/PPPPQPPP/R3K2R b KQkq - 0 2"); + CHECK(a.Play("e8c8")); + CHECK(a.GetFEN() == + "2kr3r/pppnp1pp/1bq1bn2/3p1p2/4P3/1BNB1N2/PPPPQPPP/R3K2R w KQ - 1 3"); }