Explore Library
Code QuizAdvanced

Minimax Value Backup

Spot the error in how minimax backs up values at each node.

Codejavascript
function minimax(node, isMax) {
  if (node.isLeaf) return node.value;
  if (isMax) {
    let best = -Infinity;
    for (const child of node.children)
      best = Math.max(best, minimax(child, true));
    return best;
  } else {
    let best = Infinity;
    for (const child of node.children)
      best = Math.min(best, minimax(child, false));
    return best;
  }
}

What is the bug in this minimax implementation?

Watch the code walkthrough

Watch on YouTube →