Chess vs the thinking-ahead AI
Real chess! I'm an AI that plays by imagining moves and replies. Tap a piece, then a dot. You're White.
What I think you'll play next
The moves I considered
How it works
Imagine my moves
I list every legal move I could play. In chess there are about 30 in a typical position.
Imagine your replies
For each of my moves I picture every reply you could make, and my answers to those, as deep as my thinking depth allows.
Count the pieces
Chess is too big to see to the end, so I score the last position by counting points: pawn 1, knight and bishop 3, rook 5, queen 9. Good squares earn a little extra.
Assume you play well
For each of my moves I assume you find your best reply. That's my "worst case".
Pick the best worst-case
I play the move whose worst case is best. That's minimax, and skipping useless branches is alpha-beta pruning.
What just happened?
- The AI didn't learn from games. It searched: it imagined the future and picked the safest move.
- At depth 1 it only sees its own next move, so it can't tell a protected pawn from a free one. At depth 2 it stops giving pieces away. Deeper, it sees traps and trades.
- To predict your next move, the AI plays your side in its head. It scores each of your moves, then gives better moves a bigger chance. Strong players are predictable: the good move is usually the one they play!
- Tic-tac-toe can be searched to the very end. Chess can't, so the AI must guess how good a position is. A better guess means better chess.
- Real chess engines add a very good guesser. Modern ones, like AlphaZero and Leela, use a neural network for that, so learning and searching work together.
For grown-ups: what's really going on
This is minimax search with alpha-beta pruning, written as negamax, over the full legal-move tree of chess. It is classical (symbolic) AI: no data, no training. The move generator implements all the rules except underpromotion by the player (pawns auto-queen), threefold repetition and the draw by agreement: castling with its check rules, en passant, promotion, check, checkmate, stalemate, the 50-move rule and insufficient material. It matches the published perft counts (20, 400, 8,902, 197,281 positions at depths 1 to 4 from the start).
- Evaluation: material (100/320/330/500/900 centipawns), plus small bonuses for central knights and bishops, advanced pawns (more for passed-pawn territory), rooks on the seventh and a king tucked into a corner. This is the evaluation function: the AI's estimate when it runs out of depth.
- Quiescence search: at the depth limit the search keeps playing out captures (up to 4 more plies) so it doesn't stop in the middle of a trade and mistake it for a free piece. This reduces the "horizon effect".
- Move ordering: captures are searched first, biggest victim by smallest attacker. Alpha-beta prunes far more when the best moves come first.
- Mate scores: a checkmate scores ±100000 adjusted by distance, so the AI prefers faster mates and slower losses.
- Ties: equal-valued root moves are picked at random so games differ. The root window is widened by one so tied moves keep exact scores, the same trick as in the tic-tac-toe demo.
- Strength: depth 4 with quiescence is a modest club-beginner level. Real engines search 20+ plies using transposition tables, iterative deepening, null-move pruning, late-move reductions and bitboards. Stockfish searches millions of positions per second; there are roughly 1044 chess positions in all.