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
Notes: sideshowbarker 2024-07-19 03:22:35 +09:00
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)
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;
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 other_start_rank = (colour == Colour::White) ? 6 : 1;
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) {
// Regular pawn move.
@ -457,7 +468,9 @@ bool Chess::is_promotion_move(const Move& move, Colour colour) const
if (colour == Colour::None)
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;
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;
Type promote_to;
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)
, to(to)
, promote_to(promote_to)
@ -200,15 +200,17 @@ void Chess::generate_moves(Callback callback, Colour colour) const
bool keep_going = true;
if (piece.type == Type::Pawn) {
keep_going =
try_move({sq, {sq.rank+1, sq.file}}) &&
try_move({sq, {sq.rank+2, sq.file}}) &&
try_move({sq, {sq.rank-1, sq.file}}) &&
try_move({sq, {sq.rank-2, sq.file}}) &&
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}});
for (auto& piece : Vector({Type::None, Type::Knight, Type::Bishop, Type::Rook, Type::Queen})) {
keep_going =
try_move({sq, {sq.rank+1, sq.file}, piece}) &&
try_move({sq, {sq.rank+2, sq.file}, piece}) &&
try_move({sq, {sq.rank-1, sq.file}, piece}) &&
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}});
}
} else if (piece.type == Type::Knight) {
keep_going =
try_move({sq, {sq.rank+2, sq.file+1}}) &&