← back to the sticker book
C++ · game AI

Search Algorithm Explorer

A* and Minimax written from scratch in C++, then pointed at a pile of puzzles and board games. I wanted to see for myself how much a good heuristic actually saves, and what it costs.

C++A*Minimaxalpha-beta pruninggame AI

try it

A* on the 8-puzzle

Get the tiles back in order, 1 to 8. Click a tile next to the gap to slide it, or shuffle and let A* find the shortest fix. Then switch the heuristic and watch how many positions it has to look at.

heuristic

Minimax on tic-tac-toe

You're X. The computer searches every possible game from here to the end, so the best you can do is a draw. Alpha-beta pruning lets it skip branches that can't change its answer.

your move

Minimax on Othello

You're black and you move first. Place a disc so that it traps a line of white discs between it and another black disc, and they all flip to black. The computer looks 5 moves ahead and scores positions by where the discs sit: corners are worth a lot, the squares right next to them are a trap.

your move

Minimax on chess

You're white. Click a piece to see where it can go, then click a square to move it. The computer looks 4 moves ahead and counts material plus piece-square tables, a bonus or penalty for each piece on each square (knights like the centre, kings like to stay home). Castling, en passant and promotion all work.

your move

All four demos are JavaScript ports of the same algorithms, made so they run right here. Othello and chess use the same rules, scoring and search depth as the C++ app. The C++ originals are in the repos.

how it works

One A*, five puzzles

The solver doesn't know what a puzzle is. Each puzzle is a node class that can list its neighbours, say whether it's the goal, and estimate how far it is from the goal. The same astarSolve works on the 8-puzzle, the 15-puzzle, Klotski (Huarong Pass), Missionaries & Cannibals and 8-Queens.

The heuristic is the whole game

A* always expands the position with the lowest moves so far + estimated moves left. For sliding puzzles the estimate is Manhattan distance: how many rows and columns each tile is away from home.

It never overestimates, so the answer is guaranteed to be the shortest. A weaker estimate like "misplaced tiles" is still correct but searches far more.

Accuracy vs speed

Multiply the estimate by 3 and A* turns greedy. It rushes toward whatever looks closest and finishes after looking at a tiny fraction of the positions, but the path it finds is often a few moves longer. Try "compare all four" above to see it.

Minimax with alpha-beta

Minimax assumes both players play perfectly: I pick the move with the best score, knowing you'll pick the reply with the worst score for me. Alpha-beta keeps track of the best score each side is already guaranteed and stops exploring a branch the moment it can't beat that.

Move ordering

Pruning works best when the strongest moves are tried first. Before searching, every move gets a quick one-step evaluation and the list is sorted best-first. That order decides how much of the tree gets skipped.

Four games, one engine

The minimax engine only sees an abstract GameState: its legal moves, what a move leads to, and a score. Tic-tac-toe, Gomoku, Othello and Chess each plug into it, with a small Windows GUI to play against it. Three of them are playable above.

Tic-tac-toe is small enough to search to the very end. Othello and chess aren't, so the search stops after a few moves and a heuristic guesses who's ahead.