Fix castling queenside for black

This commit is contained in:
Alex Clink 2021-09-17 23:19:47 -04:00
parent ceaaf16f80
commit 7614001e45
2 changed files with 25 additions and 3 deletions

View file

@ -23,10 +23,32 @@ describe "Castling" do
fen = Fen.parse("8/8/8/8/8/8/8/R3K2R w KQkq - 0 1")
game = Game.new(board: fen.board, players: [Player.new, Player.new])
game.make_move(from: "e1", to: "c11")
game.make_move(from: "e1", to: "c1")
debug_board(game, ["e1", "c1"])
fen.update(game)
fen.to_s.should eq("8/8/8/8/8/8/8/2KR3R b kq - 1 1")
end
end
describe "black" do
it "moves the king and the rook when castling kingside" do
fen = Fen.parse("r3k2r/8/8/8/8/8/8/8 w KQkq - 0 1")
game = Game.new(board: fen.board, players: [Player.new, Player.new])
game.make_move(from: "e8", to: "g8")
debug_board(game, ["e8", "g8"])
fen.update(game)
fen.to_s.should eq("r4rk1/8/8/8/8/8/8/8 b kq - 1 1")
end
it "moves the king and the rook when castling queenside" do
fen = Fen.parse("r3k2r/8/8/8/8/8/8/8 w KQkq - 0 1")
game = Game.new(board: fen.board, players: [Player.new, Player.new])
game.make_move(from: "e8", to: "c8")
debug_board(game, ["e8", "c8"])
fen.update(game)
fen.to_s.should eq("2kr3r/8/8/8/8/8/8/8 b kq - 1 1")
end
end
end

View file

@ -206,7 +206,7 @@ module LxChess
player = piece.white? ? @players[0] : @players[1]
return false unless player.castle_king
return false unless index = piece.index
return false unless @board.border_right(index) >= 2
return false unless (index - @board.border_left(index)).abs >= 2
# TODO: figure out if castling crosses checks
@board[index + 1].nil? && @board[index + 2].nil?
end
@ -216,7 +216,7 @@ module LxChess
player = piece.white? ? @players[0] : @players[1]
return false unless player.castle_queen
return false unless index = piece.index
return false unless @board.border_left(index) <= 2
return false unless (index - @board.border_left(index)).abs >= 2
# TODO: figure out if castling crosses checks
@board[index - 1].nil? && @board[index - 2].nil?
end