mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-01-08 12:19:37 +03:00
Chess: Fix generation of promotion moves
This commit is contained in:
parent
a6b68451dc
commit
6c2d0dea91
Notes:
sideshowbarker
2024-07-19 03:22:35 +09:00
Author: https://github.com/petelliott Commit: https://github.com/SerenityOS/serenity/commit/6c2d0dea91d Pull-request: https://github.com/SerenityOS/serenity/pull/3214 Issue: https://github.com/SerenityOS/serenity/issues/3171 Issue: https://github.com/SerenityOS/serenity/issues/3187 Reviewed-by: https://github.com/Dexesttp Reviewed-by: https://github.com/alimpfard Reviewed-by: https://github.com/awesomekling
@ -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)))
|
||||
|
@ -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}}) &&
|
||||
|
Loading…
Reference in New Issue
Block a user