## minimax

### Data

in lua

#### Tags

graphs, minimax, search

### Source Code

-- Minimax search implementation
-- See: http://en.wikipedia.org/wiki/Minimax

-- Internal recursive Minimax search
local function minimax(tree, node, depth, maximize, bestScore)
if depth == 0 or tree:isLeaf(node) then
return tree:heuristic(node)
end
local children = tree:children(node)
if maximize then
bestScore = -math.huge
for i, child in ipairs(children) do
bestScore = math.max(bestScore, minimax(tree, child, depth - 1, false))
end
return bestScore
else
bestScore = math.huge
for i, child in ipairs(children) do
bestScore = math.min(bestScore, minimax(tree, child, depth - 1, true))
end
return bestScore
end
end

-- Performs Minimax search
-- node : the node from where to start the search, usually the head node
-- tree : the search tree
-- depth : the maximum depth of search
return function(node, tree, depth)
local bestScore
return minimax(tree, node, depth, true, bestScore)
end

-- Tests for minimax.lua
local minimax = require 'minimax'

local total, pass = 0, 0

local function dec(str, len)
return #str < len
and str .. (('.'):rep(len-#str))
or str:sub(1,len)
end

local function run(message, f)
total = total + 1
local ok, err = pcall(f)
if ok then pass = pass + 1 end
local status = ok and 'PASSED' or 'FAILED'
print(('%02d. %68s: %s'):format(total, dec(message,68), status))
end

run('Testing Minimax', function()
local tree = require 'handlers.tree_handler'

local t = tree()