I am creating a chess game and found a public search function for the chess engine I was using. However, when I run it it freezes my studio for the time it takes to go to the specified depth (11491 nodes). This code is also running on the server. Does this mean that in an actual game whenver the search function runs on the server it will stop all other server code from running? If it will crash the server how can I prevent this from happening? Run it asynchronously?
Sunfish.search = function(pos, maxn)
-- Iterative deepening MTD-bi search
nodes = 0 -- the global value "nodes"
local score
-- We limit the depth to some constant, so we don't get a stack overflow in
-- the end game.
for depth = 1, 98 do
-- The inner loop is a binary search on the score of the position.
-- Inv: lower <= score <= upper
-- However this may be broken by values from the transposition table,
-- as they don't have the same concept of p(score). Hence we just use
-- 'lower < upper - margin' as the loop condition.
local lower, upper = -3 * MATE_VALUE, 3 * MATE_VALUE
while lower < upper - 3 do
local gamma = math.floor((lower + upper + 1) / 2)
score = bound(pos, gamma, depth)
-- print(nodes, gamma, score)
if score >= gamma then
lower = score
end
if score < gamma then
upper = score
end
end
-- print(string.format("Searched %d nodes. Depth %d. Score %d(%d/%d)", nodes, depth, score, lower, upper))
-- We stop deepening if the global N counter shows we have spent too
-- long, or if we have already won the game.
if nodes >= maxn or math.abs(score) >= MATE_VALUE then
break
end
end
-- If the game hasn't finished we can retrieve our move from the
-- transposition table.
local entry = tp_get(pos)
if entry ~= nil then
return entry.move, score
end
return nil, score
end