Chess: Fix en passant may put own king at risk

This commit is contained in:
Wuzzy 2023-07-16 08:48:45 +02:00
parent e28f2a1844
commit 5e97bd81b7

View File

@ -1500,6 +1500,7 @@ function realchess.move(pos, from_list, from_index, to_list, to_index, player)
local promotion = false local promotion = false
local doublePawnStep = nil local doublePawnStep = nil
local en_passant_target = nil
-- PAWN -- PAWN
if pieceFrom:sub(11,14) == "pawn" then if pieceFrom:sub(11,14) == "pawn" then
@ -1563,7 +1564,7 @@ function realchess.move(pos, from_list, from_index, to_list, to_index, player)
-- en passant -- en passant
if can_capture_en_passant(meta, "black", xy_to_index(to_x, from_y)) then if can_capture_en_passant(meta, "black", xy_to_index(to_x, from_y)) then
can_capture = true can_capture = true
inv:set_stack(to_list, xy_to_index(to_x, from_y), "") en_passant_target = xy_to_index(to_x, from_y)
end end
end end
if not can_capture then if not can_capture then
@ -1633,7 +1634,7 @@ function realchess.move(pos, from_list, from_index, to_list, to_index, player)
-- en passant -- en passant
if can_capture_en_passant(meta, "white", xy_to_index(to_x, from_y)) then if can_capture_en_passant(meta, "white", xy_to_index(to_x, from_y)) then
can_capture = true can_capture = true
inv:set_stack(to_list, xy_to_index(to_x, from_y), "") en_passant_target = xy_to_index(to_x, from_y)
end end
end end
if not can_capture then if not can_capture then
@ -1920,6 +1921,8 @@ function realchess.move(pos, from_list, from_index, to_list, to_index, player)
local blackAttacked = attacked("black", black_king_idx, board) local blackAttacked = attacked("black", black_king_idx, board)
local whiteAttacked = attacked("white", white_king_idx, board) local whiteAttacked = attacked("white", white_king_idx, board)
-- Refuse to move if it would put or leave the own king
-- under attack
if blackAttacked and thisMove == "black" then if blackAttacked and thisMove == "black" then
return return
end end
@ -1927,6 +1930,10 @@ function realchess.move(pos, from_list, from_index, to_list, to_index, player)
return return
end end
if en_passant_target then
inv:set_stack(to_list, en_passant_target, "")
end
if kingMoved and thisMove == "white" then if kingMoved and thisMove == "white" then
meta:set_int("castlingWhiteL", 0) meta:set_int("castlingWhiteL", 0)
meta:set_int("castlingWhiteR", 0) meta:set_int("castlingWhiteR", 0)