From cede0534ebca9e06c5ed17daa9f00ddb360a9747 Mon Sep 17 00:00:00 2001 From: Wuzzy Date: Fri, 14 Jul 2023 22:42:26 +0200 Subject: [PATCH] Chess: Fix crash if AI is in check --- src/chess.lua | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/chess.lua b/src/chess.lua index 8eeeb57..492fed5 100644 --- a/src/chess.lua +++ b/src/chess.lua @@ -723,6 +723,7 @@ end -- Given a table of theoretical moves and the king of the player is attacked, -- returns true if the player still has at least one move left, -- return false otherwise. +-- 2nd return value ist table of save moves -- * theoretical_moves: moves table returned by get_theoretical_moves_for() -- * board: board table -- * player: player color ("white" or "black") @@ -752,7 +753,7 @@ local function has_king_safe_move(theoretical_moves, board, player) end if next(save_moves) then - return true + return true, save_moves else return false end @@ -1841,9 +1842,9 @@ local function ai_move(inv, meta) if aiAttacked then kingSafe = false meta:set_string(aiColor.."Attacked", "true") - local is_safe = has_king_safe_move(moves, board, aiColor) + local is_safe, safe_moves = has_king_safe_move(moves, board, aiColor) if is_safe then - bestMoveSaveFrom, bestMoveSaveTo = best_move(save_moves) + bestMoveSaveFrom, bestMoveSaveTo = best_move(safe_moves) end end