diff options
| author | Loic Guegan <manzerbredes@mailbox.org> | 2022-12-26 22:01:22 +0100 |
|---|---|---|
| committer | Loic Guegan <manzerbredes@mailbox.org> | 2022-12-26 22:01:22 +0100 |
| commit | 3ba7bd82d255c555aec3882f897be471fdb69a8d (patch) | |
| tree | a76cae0e8b8c1c40c4f66a8a5bbb580a98ee7fcc | |
| parent | dd4574691a862548a7b7341a529bded347718068 (diff) | |
Support for promotiongit add -A
| -rw-r--r-- | src/ChessArbiter.cpp | 28 | ||||
| -rw-r--r-- | src/ChessArbiter.hpp | 3 | ||||
| -rw-r--r-- | tests/chessarbiter.cpp | 20 |
3 files changed, 49 insertions, 2 deletions
diff --git a/src/ChessArbiter.cpp b/src/ChessArbiter.cpp index d64ac83..96c7d49 100644 --- a/src/ChessArbiter.cpp +++ b/src/ChessArbiter.cpp @@ -39,7 +39,7 @@ bool ChessArbiter::IsCheck(bool isBlack) { return (IsAttacked(kingloc, !isBlack)); } -bool ChessArbiter::Play(std::string move) { +bool ChessArbiter::Play(std::string move, char promote) { std::vector<std::string> moves = ListLegalMoves(fen.player); if (find(moves.begin(), moves.end(), move) != moves.end()) { Piece moved = board.GetPieceAt(move.substr(0, 2)); // This call never fail @@ -125,6 +125,18 @@ bool ChessArbiter::Play(std::string move) { } newFen.halfmove = 0; // Pawn moves reset half moves } + // Check pawn promotion + if(moved.piece == 'p' || moved.piece == 'P'){ + if(moved.piece == 'p' && dst[1]=='1'){ + board.RemovePiece(dst); + board.AddPiece(tolower(promote),dst); + SAN+="="+promote; + } else if(dst[1]=='8'){ + board.RemovePiece(dst); + board.AddPiece(toupper(promote),dst); + SAN+="="+toupper(promote); + } + } // Captures reset half moves if (IsCapture) { newFen.halfmove = 0; @@ -518,4 +530,18 @@ std::string ChessArbiter::ParseSAN(std::string SANMove) { return (src + dst); } +char ChessArbiter::ParseSANPromotion(std::string SANMove){ + for(short i=0;i<SANMove.length();i++){ + if(SANMove[i]=='='){ + if((i+1)<SANMove.length()){ + char p=SANMove[i+1]; // Must be upper + if(p=='Q' || p=='R' || p=='B' || p=='N'){ + return SANMove[i+1]; + } + } + } + } + return 'Q'; +} + } // namespace chessarbiter
\ No newline at end of file diff --git a/src/ChessArbiter.hpp b/src/ChessArbiter.hpp index 19a4ba1..d63ab06 100644 --- a/src/ChessArbiter.hpp +++ b/src/ChessArbiter.hpp @@ -44,7 +44,7 @@ public: /// @brief Check if a side is in check bool IsCheck(bool); /// @brief Play a move (return false if it's illegal) - bool Play(std::string); + bool Play(std::string, char promote='Q'); /// @brief Check if a square is attacked by a particular player bool IsAttacked(std::string, bool); /// @brief Get the serialized board @@ -70,5 +70,6 @@ public: bool IsDraw(); bool WasEnPassant(); std::string ParseSAN(std::string SANMove); + char ParseSANPromotion(std::string SANMove); }; } // namespace chessarbiter diff --git a/tests/chessarbiter.cpp b/tests/chessarbiter.cpp index 2e65ae5..96b5f3b 100644 --- a/tests/chessarbiter.cpp +++ b/tests/chessarbiter.cpp @@ -478,4 +478,24 @@ TEST_CASE("Specific bugs found on a game", "[BugFixes]") { a.Setup("1k3r1r/npqbbp2/4p1p1/p2pPnNp/1P3B1P/P1PB4/5PPQ/RN2R1K1 w - - 1 19"); a.Play("e1c1"); CHECK(a.GetFEN() == "1k3r1r/npqbbp2/4p1p1/p2pPnNp/1P3B1P/P1PB4/5PPQ/RNR3K1 b - - 2 19"); + + // BUG 2 (Promotion) + a.Setup("8/k2P4/2p1ppp1/5qBp/5P1P/8/6PK/8 w - - 0 45"); + a.Play("d7d8"); + CHECK(a.GetFEN() == "3Q4/k7/2p1ppp1/5qBp/5P1P/8/6PK/8 b - - 0 45"); + + // BUG 3 (Promotion) + a.Setup("8/k2P4/2p1ppp1/5qBp/5P1P/8/6PK/8 w - - 0 45"); + a.Play("d7d8",'n'); + CHECK(a.GetFEN() == "3N4/k7/2p1ppp1/5qBp/5P1P/8/6PK/8 b - - 0 45"); + + // BUG 4 (Promotion) + char p=a.ParseSANPromotion("d8=Q"); + CHECK(p == 'Q'); + p=a.ParseSANPromotion("d8=N+"); + CHECK(p == 'N'); + p=a.ParseSANPromotion("d8=B+"); + CHECK(p == 'B'); + p=a.ParseSANPromotion("d8=R"); + CHECK(p == 'R'); }
\ No newline at end of file |
