Skip to content

Commit

Permalink
Fix have_en_passant function to prevent returning a possible en passa…
Browse files Browse the repository at this point in the history
…nt in a position unreachable by the pawn
  • Loading branch information
pioz committed Aug 29, 2024
1 parent fdbcba8 commit 152ee77
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
chess (0.3.5)
chess (0.3.6)

GEM
remote: http://rubygems.org/
Expand Down
12 changes: 6 additions & 6 deletions ext/chess/special.c
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,12 @@ update_en_passant (Board *board, int from, int to)
int
have_en_passant (Board *board, int from, int to)
{
if (board->placement[from] == 'P' && to == board->en_passant
&& (from == to - 7 || from == to - 9))
return to - 8;
else if (board->placement[from] == 'p' && to == board->en_passant
&& (from == to + 7 || from == to + 9))
return to + 8;
if (board->placement[from] == 'P' && to == board->en_passant) {
return xray_attack_white_pawn(from) & (1ULL << to) ? to - 8 : 0;
}
if (board->placement[from] == 'p' && to == board->en_passant) {
return xray_attack_black_pawn(from) & (1ULL << to) ? to + 8 : 0;
}
return 0;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/chess/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# The Chess library module.
module Chess
# The library version.
VERSION = '0.3.5'.freeze
VERSION = '0.3.6'.freeze
end
8 changes: 8 additions & 0 deletions test/test_move_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,12 @@ class ChessTest < Minitest::Test
end
end
end

def test_github_issue32
game = Chess::Game.new
game.moves = %w[f2f4 d7d6 d2d3 h7h5 b1d2 e7e5 f4f5 a7a5 c2c3 d8f6 d2c4 b7b6 c4d2 a8a6 d2f3 a6a7 f3e5 b6b5 h2h4]
generated_moves = game.board.generate_moves('a5')

assert_equal %w[a4], generated_moves
end
end

0 comments on commit 152ee77

Please sign in to comment.