Chess: Fix crash if AI is in check
This commit is contained in:
parent
e316e77846
commit
cede0534eb
@ -723,6 +723,7 @@ end
|
|||||||
-- Given a table of theoretical moves and the king of the player is attacked,
|
-- 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,
|
-- returns true if the player still has at least one move left,
|
||||||
-- return false otherwise.
|
-- return false otherwise.
|
||||||
|
-- 2nd return value ist table of save moves
|
||||||
-- * theoretical_moves: moves table returned by get_theoretical_moves_for()
|
-- * theoretical_moves: moves table returned by get_theoretical_moves_for()
|
||||||
-- * board: board table
|
-- * board: board table
|
||||||
-- * player: player color ("white" or "black")
|
-- * player: player color ("white" or "black")
|
||||||
@ -752,7 +753,7 @@ local function has_king_safe_move(theoretical_moves, board, player)
|
|||||||
end
|
end
|
||||||
|
|
||||||
if next(save_moves) then
|
if next(save_moves) then
|
||||||
return true
|
return true, save_moves
|
||||||
else
|
else
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
@ -1841,9 +1842,9 @@ local function ai_move(inv, meta)
|
|||||||
if aiAttacked then
|
if aiAttacked then
|
||||||
kingSafe = false
|
kingSafe = false
|
||||||
meta:set_string(aiColor.."Attacked", "true")
|
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
|
if is_safe then
|
||||||
bestMoveSaveFrom, bestMoveSaveTo = best_move(save_moves)
|
bestMoveSaveFrom, bestMoveSaveTo = best_move(safe_moves)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user