Chess: Refactor king safe moves function
This commit is contained in:
parent
90feb42d52
commit
2828e5644a
@ -877,15 +877,16 @@ function realchess.locate_kings(board)
|
|||||||
return Bidx, Widx
|
return Bidx, Widx
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Given a table of theoretical moves and the king of the player is attacked,
|
-- Given a table of theoretical moves, returns a table
|
||||||
-- returns true if the player still has at least one move left,
|
-- of moves that are safe for the king, i.e. moves
|
||||||
-- return false otherwise.
|
-- that neither put or leave the king at risk.
|
||||||
-- 2nd return value is table of save moves
|
-- 2nd return value is the number of said safe moves.
|
||||||
-- * theoretical_moves: moves table returned by realchess.get_theoretical_moves_for()
|
-- * theoretical_moves: moves table returned by realchess.get_theoretical_moves_for()
|
||||||
-- * board: board table
|
-- * board: board table
|
||||||
-- * player: player color ("white" or "black")
|
-- * player: player color ("white" or "black")
|
||||||
function realchess.has_king_safe_move(theoretical_moves, board, player)
|
function realchess.get_king_safe_move(theoretical_moves, board, player)
|
||||||
local safe_moves = {}
|
local safe_moves = {}
|
||||||
|
local safe_moves_count = 0
|
||||||
-- create a virtual board
|
-- create a virtual board
|
||||||
local v_board = table.copy(board)
|
local v_board = table.copy(board)
|
||||||
|
|
||||||
@ -903,7 +904,7 @@ function realchess.has_king_safe_move(theoretical_moves, board, player)
|
|||||||
local black_king_idx, white_king_idx = realchess.locate_kings(v_board)
|
local black_king_idx, white_king_idx = realchess.locate_kings(v_board)
|
||||||
if not black_king_idx or not white_king_idx then
|
if not black_king_idx or not white_king_idx then
|
||||||
minetest.log("error", "[xdecor] Chess: Insufficient kings on chessboard!")
|
minetest.log("error", "[xdecor] Chess: Insufficient kings on chessboard!")
|
||||||
return false
|
return {}, 0
|
||||||
end
|
end
|
||||||
local king_idx
|
local king_idx
|
||||||
if player == "black" then
|
if player == "black" then
|
||||||
@ -915,6 +916,7 @@ function realchess.has_king_safe_move(theoretical_moves, board, player)
|
|||||||
if not playerAttacked then
|
if not playerAttacked then
|
||||||
safe_moves[from_idx] = safe_moves[from_idx] or {}
|
safe_moves[from_idx] = safe_moves[from_idx] or {}
|
||||||
safe_moves[from_idx][to_idx] = value
|
safe_moves[from_idx][to_idx] = value
|
||||||
|
safe_moves_count = safe_moves_count + 1
|
||||||
end
|
end
|
||||||
|
|
||||||
-- restore the old state of the virtual board
|
-- restore the old state of the virtual board
|
||||||
@ -923,11 +925,7 @@ function realchess.has_king_safe_move(theoretical_moves, board, player)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if next(safe_moves) then
|
return safe_moves, safe_moves_count
|
||||||
return true, safe_moves
|
|
||||||
else
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Given a chessboard, checks whether it is in a "dead position",
|
-- Given a chessboard, checks whether it is in a "dead position",
|
||||||
@ -1924,9 +1922,9 @@ local function update_game_result(meta)
|
|||||||
local isKingAttacked = realchess.attacked(checkPlayer, king_idx, board_t)
|
local isKingAttacked = realchess.attacked(checkPlayer, king_idx, board_t)
|
||||||
if isKingAttacked then
|
if isKingAttacked then
|
||||||
meta:set_string(checkPlayer.."Attacked", "true")
|
meta:set_string(checkPlayer.."Attacked", "true")
|
||||||
local is_safe = realchess.has_king_safe_move(checkMoves, board_t, checkPlayer)
|
local _, save_moves = realchess.get_king_safe_move(checkMoves, board_t, checkPlayer)
|
||||||
-- If not safe moves left, player can't move
|
-- If not safe moves left, player can't move
|
||||||
if not is_safe then
|
if save_moves == 0 then
|
||||||
if checkPlayer == "black" then
|
if checkPlayer == "black" then
|
||||||
blackCanMove = false
|
blackCanMove = false
|
||||||
else
|
else
|
||||||
|
@ -91,8 +91,8 @@ function chessbot.move(inv, meta)
|
|||||||
if botAttacked then
|
if botAttacked then
|
||||||
kingSafe = false
|
kingSafe = false
|
||||||
meta:set_string(currentBotColor.."Attacked", "true")
|
meta:set_string(currentBotColor.."Attacked", "true")
|
||||||
local is_safe, safe_moves = realchess.has_king_safe_move(moves, board_t, currentBotColor)
|
local safe_moves, save_moves_count = realchess.get_king_safe_move(moves, board_t, currentBotColor)
|
||||||
if is_safe then
|
if save_moves_count >= 1 then
|
||||||
bestMoveSaveFrom, bestMoveSaveTo = best_move(safe_moves)
|
bestMoveSaveFrom, bestMoveSaveTo = best_move(safe_moves)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user