1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 11:07:45 +00:00

Chess: Fix generation of promotion moves

This commit is contained in:
Peter Elliott 2020-08-17 15:40:55 -06:00 committed by Andreas Kling
parent a6b68451dc
commit 6c2d0dea91
2 changed files with 27 additions and 12 deletions

View file

@ -153,7 +153,10 @@ bool Chess::is_legal_no_check(const Move& move, Colour colour) const
if (move.to.rank > 7 || move.to.file > 7) if (move.to.rank > 7 || move.to.file > 7)
return false; return false;
if (move.promote_to == Type::Pawn || move.promote_to == Type::King || move.promote_to == Type::None) if (piece.type != Type::Pawn && move.promote_to != Type::None)
return false;
if (move.promote_to == Type::Pawn || move.promote_to == Type::King)
return false; return false;
if (piece.type == Type::Pawn) { if (piece.type == Type::Pawn) {
@ -161,6 +164,14 @@ bool Chess::is_legal_no_check(const Move& move, Colour colour) const
unsigned start_rank = (colour == Colour::White) ? 1 : 6; unsigned start_rank = (colour == Colour::White) ? 1 : 6;
unsigned other_start_rank = (colour == Colour::White) ? 6 : 1; unsigned other_start_rank = (colour == Colour::White) ? 6 : 1;
unsigned en_passant_rank = (colour == Colour::White) ? 4 : 3; unsigned en_passant_rank = (colour == Colour::White) ? 4 : 3;
unsigned promotion_rank = (colour == Colour::White) ? 7 : 0;
if (move.to.rank == promotion_rank) {
if (move.promote_to == Type::Pawn || move.promote_to == Type::King || move.promote_to == Type::None)
return false;
} else if (move.promote_to != Type::None) {
return false;
}
if (move.to.rank == move.from.rank + dir && move.to.file == move.from.file && get_piece(move.to).type == Type::None) { if (move.to.rank == move.from.rank + dir && move.to.file == move.from.file && get_piece(move.to).type == Type::None) {
// Regular pawn move. // Regular pawn move.
@ -457,7 +468,9 @@ bool Chess::is_promotion_move(const Move& move, Colour colour) const
if (colour == Colour::None) if (colour == Colour::None)
colour = turn(); colour = turn();
if (!is_legal(move, colour)) Move queen_move = move;
queen_move.promote_to = Type::Queen;
if (!is_legal(queen_move, colour))
return false; return false;
if (get_piece(move.from).type == Type::Pawn && ((colour == Colour::Black && move.to.rank == 0) || (colour == Colour::White && move.to.rank == 7))) if (get_piece(move.from).type == Type::Pawn && ((colour == Colour::Black && move.to.rank == 0) || (colour == Colour::White && move.to.rank == 7)))

View file

@ -91,7 +91,7 @@ public:
Square to; Square to;
Type promote_to; Type promote_to;
Move(const StringView& algebraic); Move(const StringView& algebraic);
Move(const Square& from, const Square& to, const Type& promote_to = Type::Queen) Move(const Square& from, const Square& to, const Type& promote_to = Type::None)
: from(from) : from(from)
, to(to) , to(to)
, promote_to(promote_to) , promote_to(promote_to)
@ -200,15 +200,17 @@ void Chess::generate_moves(Callback callback, Colour colour) const
bool keep_going = true; bool keep_going = true;
if (piece.type == Type::Pawn) { if (piece.type == Type::Pawn) {
keep_going = for (auto& piece : Vector({Type::None, Type::Knight, Type::Bishop, Type::Rook, Type::Queen})) {
try_move({sq, {sq.rank+1, sq.file}}) && keep_going =
try_move({sq, {sq.rank+2, sq.file}}) && try_move({sq, {sq.rank+1, sq.file}, piece}) &&
try_move({sq, {sq.rank-1, sq.file}}) && try_move({sq, {sq.rank+2, sq.file}, piece}) &&
try_move({sq, {sq.rank-2, sq.file}}) && try_move({sq, {sq.rank-1, sq.file}, piece}) &&
try_move({sq, {sq.rank+1, sq.file+1}}) && try_move({sq, {sq.rank-2, sq.file}, piece}) &&
try_move({sq, {sq.rank+1, sq.file-1}}) && try_move({sq, {sq.rank+1, sq.file+1}}) &&
try_move({sq, {sq.rank-1, sq.file+1}}) && try_move({sq, {sq.rank+1, sq.file-1}}) &&
try_move({sq, {sq.rank-1, sq.file-1}}); try_move({sq, {sq.rank-1, sq.file+1}}) &&
try_move({sq, {sq.rank-1, sq.file-1}});
}
} else if (piece.type == Type::Knight) { } else if (piece.type == Type::Knight) {
keep_going = keep_going =
try_move({sq, {sq.rank+2, sq.file+1}}) && try_move({sq, {sq.rank+2, sq.file+1}}) &&