Visualizations
219 / 500

219 · Games

Mancala

The ancient sowing game on a carved wooden board, played by minimax with alpha-beta.

Two AIs play Mancala by the Kalah rules: scoop a pit, sow the seeds counterclockwise, move again if the last one lands in your store, capture when it lands in an empty pit of your own. Each move is chosen by minimax with alpha-beta pruning and iterative deepening, written as plain minimax rather than negamax because extra turns let the same side move twice, and free moves do not use up depth. The bars beside the pits show what the search currently thinks of each move, and the beam below shows who it thinks is ahead. The board is a height field (a bevelled plank with bowls carved in) lit per pixel over procedural wood grain and built a few rows per frame, and the seeds are glass and stone sprites that ride a hopping hand from pit to pit, drop, bounce and settle under position-based physics.

Try it. Click one of your pits on the near row (or press 1 to 6) to take over the bottom side; on your turn the bars are the AI's opinion of your options. Choose a search depth from 2 to 12 (or + and -), and use the Play or Watch button (W) to switch modes. N starts a new game.

  • Minimax with alpha-beta pruning
  • Iterative deepening
  • Height-field lighting
  • Procedural wood grain
  • Position-based seed physics

View the source · one module, plus a small shared runtime for sizing, the animation loop and input

Build your own

Paste this into Claude Code, Codex or any coding agent to get a simple version running, then take it wherever you like.

Build a Mancala game (Kalah rules) where I play against a minimax AI, with JavaScript and the HTML canvas element. Put everything in a single index.html file with no libraries or build step, so I can open it directly in a browser.

Start simple:
- Model the board as an array of 14 numbers: six pits for each player and one store each, four seeds in every pit to start. A move picks up all seeds in one of your pits and sows them one by one counterclockwise, skipping the opponent's store.
- Implement the rules: if the last seed lands in your store you move again; if it lands in an empty pit on your side, capture it and the seeds opposite; when one side is empty, the other side's seeds go to their owner's store and the bigger store wins.
- Draw circles for pits, dots for seeds and a count beside each pit. I click my pits on the bottom row to play.
- Write minimax with alpha-beta pruning for the top player, scored by the store difference, searching 6 plies. Because of extra turns, track whose turn it is explicitly instead of flipping sides every ply.

Once that works, make it beautiful:
- Paint a wooden board: generate a wood grain texture with a small noise function (stretched rings bent by noise), and shade the pits as carved bowls with a highlight on the lit rim.
- Draw the seeds as glossy glass beads in several colors with a radial gradient and a white highlight, and animate sowing: the seeds hop from pit to pit and settle with a little bounce.
- Show bars beside each pit with the search's score for that move, and let me pick the search depth.

Explain the key ideas in short code comments. When you're done, tell me how to open it and suggest three directions I could take it next, such as iterative deepening that runs a slice per frame, soft physics so seeds jostle inside each pit, or an AI versus AI mode at different depths.
PreviousPhased ArraySteer and focus a beam of sound with per-speaker delays in a live 2D wave simulation. NextChiptune TrackerAn 8-bit sound chip emulated from scratch, playing a procedurally composed song.

Related visualizations

  • Alpha-Beta OthelloGames Othello against alpha-beta search, with its pruned tree unfurling beside the board.
  • Marble LabyrinthGames A wooden tilt maze where a careful AI steers a steel ball between the holes.
  • Sliding Puzzle SolverGames IDA* with pattern databases solves the 15-puzzle in the fewest possible moves.
  • MCTS BloomAlgorithms Four-in-a-row against Monte Carlo Tree Search, its search tree blooming as a flower.
  • Minesweeper OracleAlgorithms Every hidden cell shows its exact mine probability while an AI plays the safest move.
  • Procedural PinballRetro Generated pinball tables in perspective, played by an AI that simulates its flips.
  • Sokoban ForgeAlgorithms Crate puzzles forged by playing backward from the goals, then solved by A*.
  • Dancing LinksAlgorithms Knuth's Algorithm X tiles boards with wooden pentominoes while its links visibly dance.
  • Lights Out AlgebraMath The toggle-tile puzzle solved instantly at any size by linear algebra over GF(2).

Use ← and → to move between demos. While the canvas has focus, keys go to the demo instead.

← More from Emergent Mind Labs