-1

I am learning minimax and was wondering my logic for minimax is correct(rough pseudocode).:

fun minimax(tree, depth) { #assume the tree has already been built;
  if terminal node;   # if we reach a node with no children, return the score;
    return score;
  else;
    if max's turn then return min(minimax(tree,depth-1))  
    if min's turn then return max(minimax(tree,depth-1))   
}

I am assuming that the game tree is already built and just want to confirm that the this high level pseudocode is the correct logic.

Codor
  • 17,447
  • 9
  • 29
  • 56
lord12
  • 2,837
  • 9
  • 35
  • 47

2 Answers2

2
minimax(position,depth)
  ....
  if max's turn then return min(minimax(p,depth-1) for p in moves)  
  if min's turn then return max(minimax(p,depth-1) for p in moves) 

or

minimax(tree,depth)
  ....
  if max's turn then return min(minimax(node,depth-1) for node in nodes(tree))  
  if min's turn then return max(minimax(node,depth-1) for node in nodes(tree)) 

You need to call minimax with tree that is node (subtree) of current tree.

Luka Rahne
  • 10,336
  • 3
  • 34
  • 56
0

You'll have to call your function recursively for every possible move from that state and choose the move with max/ min score based on the player in current ply. In general, the best pseudo code for minimax implementation I've seen so far is in the "Artificial Intelligence: A modern approach" book by Peter Norvig. All the pseudocode in the book is on it's github page and here's the minimax code -

function MINIMAX-DECISION(state) returns an action
     return arg max a ∈ ACTIONS(s) MIN-VALUE(RESULT(state, a))

function MAX-VALUE(state) returns a utility value
    if TERMINAL-TEST(state) the return UTILITY(state)
    v ← −∞
    for each a in ACTIONS(state) do
        v ← MAX(v, MIN-VALUE(RESULT(state, a)))
    return v

function MIN-VALUE(state) returns a utility value
    if TERMINAL-TEST(state) the return UTILITY(state)
    v ← ∞
    for each a in ACTIONS(state) do
        v ← MIN(v, MAX-VALUE(RESULT(state, a)))
    return v

Few things to note for a depth-limited implementation:

  1. Make sure to decrement the depth and pass it to MAX-VALUE and MIN-VALUE functions.
  2. When you reach the depth = 0, replace the UTILITY function with your heuristic-score function.
midhunsezhi
  • 461
  • 5
  • 10