From c2f641ab6268e0f8a94e055c59aa704215102b8b Mon Sep 17 00:00:00 2001 From: Wuzzy Date: Fri, 14 Jul 2023 18:36:12 +0200 Subject: [PATCH] Chess: Fix incomplete checkmate detection --- src/chess.lua | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/chess.lua b/src/chess.lua index 0ceb17f..859b650 100644 --- a/src/chess.lua +++ b/src/chess.lua @@ -599,7 +599,14 @@ local function get_theoretical_moves_from(meta, board, from_idx) -- KING 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 else 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 whiteMoves = get_theoretical_moves_for(meta, board_t, "white") - local b = 0 for k,v in pairs(blackMoves) do - b = b + 1 blackCanMove = true end - b = 0 for k,v in pairs(whiteMoves) do - b = b + 1 whiteCanMove = true end