Chess: Fix incomplete checkmate detection

This commit is contained in:
Wuzzy 2023-07-14 18:36:12 +02:00
parent 769f867ab1
commit c2f641ab62

View File

@ -599,7 +599,14 @@ local function get_theoretical_moves_from(meta, board, from_idx)
-- KING -- KING
elseif piece == "king" then elseif piece == "king" then
if attacked(color, xy_to_index(to_x, to_y), board) then local inv = meta:get_inventory()
-- King can't move to any attacked square
-- king_board simulates the board with the king moved already.
-- Required for the attacked() check to work
local king_board = board_to_table(inv)
king_board[to_idx] = king_board[from_idx]
king_board[from_idx] = ""
if attacked(color, to_idx, king_board) then
moves[to_idx] = nil moves[to_idx] = nil
else else
local dx = from_x - to_x local dx = from_x - to_x
@ -1069,14 +1076,10 @@ local function update_game_result(meta)
local blackMoves = get_theoretical_moves_for(meta, board_t, "black") local blackMoves = get_theoretical_moves_for(meta, board_t, "black")
local whiteMoves = get_theoretical_moves_for(meta, board_t, "white") local whiteMoves = get_theoretical_moves_for(meta, board_t, "white")
local b = 0
for k,v in pairs(blackMoves) do for k,v in pairs(blackMoves) do
b = b + 1
blackCanMove = true blackCanMove = true
end end
b = 0
for k,v in pairs(whiteMoves) do for k,v in pairs(whiteMoves) do
b = b + 1
whiteCanMove = true whiteCanMove = true
end end