## negamax

### Data

in lua

#### Tags

graphs, minimax, search

### Source Code

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

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

-- Performs Negamax search
-- node  : the node from where to start the search, usually the head node
-- tree  : the search tree
-- depth : the maximum depth of search
-- flag  : the player (+1) or the opponent (-1)
return function(node, tree, depth, flag)
flag = (flag / math.abs(flag)) -- Gets the sign (either +1 or -1)
return negamax(tree, node, depth, flag)
end

``````
``````-- Tests for alpha_beta_pruning.lua
local negamax = require 'negamax'

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 Negamax', function()
local tree = require 'handlers.tree_handler'

local t = tree()