mirror of
https://gitlab.com/manzerbredes/chessarbiter.git
synced 2025-04-05 17:46:26 +02:00
Support for promotiongit add -A
This commit is contained in:
parent
dd4574691a
commit
3ba7bd82d2
3 changed files with 49 additions and 2 deletions
|
@ -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
|
|
@ -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
|
||||
|
|
|
@ -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');
|
||||
}
|
Loading…
Add table
Reference in a new issue