Under the Hood

How the Mancala AI Works: Minimax and the Free-Turn Rule

Published September 12, 2026 · Updated October 21, 2026 with a self-play benchmark · LK Forge

48
stones in play across the board
2
stores to fill — yours vs theirs
α-β
minimax with alpha-beta pruning
0
servers — runs in your browser

Unlike the card games and backgammon, LK Forge Mancala (Kalah) hides nothing and rolls no dice. Both players see the whole board at all times, and every move is a choice, not a gamble. That makes it a perfect-information game — exactly the setting classic minimax was built for.

Minimax with alpha-beta pruning

To pick a move the engine plays futures out for both sides and scores each end position by the thing that wins Kalah: stones safe in your store minus stones in the opponent's, plus setups that lead to captures. It keeps the move with the best guaranteed result and uses alpha-beta pruning to skip branches that provably cannot change the decision, so it can look several moves deeper for the same effort.

The free-turn rule breaks a hidden assumption

Standard minimax assumes the players alternate — my move, your move, my move. Kalah quietly violates that: if your last stone lands in your own store, you move again. A naive search that flips sides after every move would credit those extra turns to the opponent and misjudge the whole line. So the engine only flips sides when the turn actually passes — after a free-turn move it keeps the same player to move. That one conditional is what lets it plan the long chains of free turns that decide sharp Mancala games.

// negamax, but a free-turn move keeps the same side to move
function search(state, depth, alpha, beta) {
  if (depth === 0 || isOver(state)) return evaluate(state);
  let best = -Infinity;
  for (const mv of legalMoves(state)) {
    const next = applyMove(state, mv);
    const score = next.freeTurn
      ? search(next, depth - 1, alpha, beta)            // same side — do NOT negate
      : -search(next, depth - 1, -beta, -alpha);         // turn passes — flip as usual
    if (score > best) best = score;
    if (best > alpha) alpha = best;
    if (alpha >= beta) break;                          // prune
  }
  return best;
}
Captures, too. Landing your last stone in an empty pit on your side captures the stones in the pit directly opposite. The engine doesn't need a special rule for it — it plays the position out, and the captured stones simply show up in the store-difference score that minimax is maximising.

Three levels, one search

The levels share the same search and differ in how far it looks. Beginner plays a random legal move about 45% of the time and a 2-ply search otherwise, so it makes the kind of mistakes a new player makes. Medium searches 6 plies deep on every move. Hard deepens iteratively up to 16 plies within a 1.1-second budget per move; in practice that reaches about 12 plies. Because Kalah has no luck, extra depth turns into wins fairly directly. The benchmark below measures how directly.

The benchmark

The numbers come from scripts/benchmark-mancala.mjs, which loads the exact engine the game ships (the inline engine block of the game page) and self-plays it in Node. Each matchup starts from 100 seeded random two-move openings, and every opening is played twice with the colours swapped, so the first-move advantage cancels out: 200 games per row. A tie (24–24) counts as a draw. Margin is the average store difference at the end of the game, from the first player's side. Measured October 21, 2026 on a laptop, one matchup per CPU core; timings will differ on your machine.

The three levels

MatchupWinDrawLossMargin
Beginner vs a random mover89%3%8%+15.0
Medium vs a random mover100%0%0%+25.5
Hard vs a random mover100%0%0%+24.9
Medium vs Beginner96.5%1%2.5%+18.9
Hard vs Beginner99.5%0.5%0%+19.6
Hard vs Medium79%5%16%+9.3

Hard averaged 12.4 plies against Medium and spent about 0.55 seconds per move. Medium never lost to the random mover in 200 games, and Hard lost none of its 200 games to Beginner.

What each extra ply is worth

The second study pits fixed search depths against Medium (6 plies). Depth 6 against itself is the control and lands where it should, at an even score.

Depth vs MediumWinDrawLossMarginms / move
2 plies16%1%83%−9.4<0.1
3 plies27%5.5%67.5%−5.6<0.1
4 plies32.5%4.5%63%−3.30.1
5 plies42%8%50%−2.20.3
6 plies (control)48.5%3%48.5%0.00.7
7 plies56%12.5%31.5%+3.61.7
8 plies54%0%46%+3.64.2
10 plies71.5%6.5%22%+6.724
12 plies80.5%1%18.5%+8.8123
14 plies80.5%0.5%19%+8.1445

Two things stand out. The curve is not perfectly smooth: 8 plies scored a little below 7. With 200 games per row, differences of a few points are within the noise, so read the overall trend rather than any single step. And the gain flattens after 12 plies: 14 plies took almost four times as long per move and won exactly as often.

Re-run it yourself. node scripts/benchmark-mancala.mjs reproduces both tables from the shipped engine. The openings are seeded, so the depth study gives the same result every run; the Hard rows depend on how deep your machine gets in 1.1 seconds.

That is the whole engine: minimax over a fully visible board, alpha-beta to search deeper, and one careful exception so the free-turn rule is scored correctly. Want to see game-tree search in motion? Read minimax and alpha-beta, step by step, or why our games are search and not chatbots in Real Game AI, Not a Chatbot.

FAQ

How does the Mancala AI choose a move?

Mancala (Kalah) is a perfect-information game with no dice and nothing hidden, so the engine runs a straight minimax game-tree search with alpha-beta pruning. It plays futures out for both sides, scores each by the difference in captured stones, and picks the move with the best guaranteed result. Everything runs in your browser.

How does the AI handle the free-turn rule?

The free-turn rule works like this: in Kalah, if your last stone lands in your own store you move again. That breaks the usual take-turns assumption of minimax, so the search does not flip sides after a free-turn move — it keeps the same player to move. This lets the engine plan whole chains of free turns in a single visit, which is where a lot of Mancala's tactics live.

Does the Mancala AI use randomness or a neural network?

No neural network, and randomness only on Beginner. Because Kalah has no chance element and no hidden information, there is nothing to sample and no model to learn, so a plain minimax search is the right tool. Medium and Hard are deterministic: the same position always gets the same move. Beginner deliberately plays a random legal move about 45% of the time so it makes beginner-like mistakes.

What does the AI value in a position?

Chiefly the stones already safe in its own store versus the opponent's, plus setups that lead to captures (landing your last stone in an empty pit on your side grabs the stones opposite) and to more free turns. Deeper search turns those small edges into a decisive lead. Because Kalah has no luck, that edge tends to hold up once the search plays it out.

Chain your free turns against the search.

Play Mancala →