How the Mancala AI Works: Minimax and the Free-Turn Rule
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; }
Three levels, one search
The difficulty levels are just search depth: a shallow look for Beginner, a deeper one for Hard. Because Kalah is deterministic, more depth translates cleanly into stronger play — there is no luck to wash the advantage out, so a deeper search that spots a free-turn chain or a capture two moves early will take the point almost every time.
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?
Neither — the Mancala AI uses no randomness and no neural network. Because Kalah has no chance element and no hidden information, there is nothing to sample and no model to learn — a plain deterministic minimax search is the right tool, and it plays a strong, honest game. Because the search is deterministic, the same position always produces the same move.
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 →