Chess Engine STABLE

Play

A chess engine that runs entirely in your browser. You play White; drag a piece to move. Nothing is sent anywhere, and the search runs on your own machine.

Even Your turn.
WhiteBlack

Captured

Nothing captured yet.

Settings

Move history

No moves yet.

How it works

Everything happens in this page. There is no server, no call to an outside service and no database of games: when the engine plays a move, your own computer worked it out a moment ago, using only the code in this file.

The engine is deliberately simple, built the way the first chess programs were. It has no intuition and no memory of famous games. It imagines every move it could make, every reply you could make, and so on for a fixed number of moves, then judges the positions it ends up in with a short list of rules of thumb. The whole engine fits in about a hundred lines. The rules of chess themselves (how pieces move, what counts as check, mate or a draw) come from chess.js, a small, widely used library that is included in the page.

Looking a fixed distance ahead

The engine plays Black. When it is its turn, it looks ahead a fixed number of half-moves, set by the "Search depth" option: 2, 3 or 4. At depth 3, the default, it considers each of its moves, each of your possible replies, and each of its possible answers to those, and only then stops to judge the position. It never looks further than that, however sharp the position, and never stops earlier, however quiet.

score score score score score score score score stops here half-move 1 half-move 2 half-move 3

Each extra half-move multiplies the work, because each position has around thirty legal moves. The figures below come from one middlegame test position; open positions with many legal moves take longer, simple endgames far less.

depth 1 33 positions · 0.005 s depth 2 1,154 positions · 0.2 s depth 3 9,778 positions · 1.4 s depth 4 168,654 positions · 25 s bar length on a log scale: each step is roughly ten times the last

That is why depth 4 is labelled "slow": it can take half a minute to choose a single move.

Assuming you play well

Scores are always given from White's side: positive is good for White, and so for you, negative is good for the engine. When the engine works back from the positions at the end of its lookahead, it assumes that you always choose the reply with the highest score and that it will choose the move with the lowest. This rule is called minimax. It stops the engine from falling for moves that only work if you blunder: a move that wins a pawn but lets you win a queen is judged by the queen.

+1 +3 +3 0 +4 +4 −1 +2 +2 +2: the engine plays the third move engine you

Skipping moves that are already refuted

Examining every branch would be far too slow, so the engine uses alpha-beta pruning. Once it has found a move that holds White to +3, any other move can be dropped as soon as you have one reply that scores more than that, because the engine would never choose it. The remaining replies to that move are never looked at. This changes nothing about the move finally chosen; it only avoids pointless work.

+1 +3 +3 +4 ? skipped +4 or more −1 +2 +2 +2: the engine plays the third move engine you

Alpha-beta saves the most when the strongest moves happen to be looked at first. This engine takes moves in whatever order the rules library lists them, so the savings depend on luck. At the top level it also starts every candidate move from scratch, without telling the search what the best move so far already guarantees. Both are the main reasons it can't look deeper in the time it takes.

What happens at the end of each line

Each position reached at the end of a line is first checked against the rules, then scored. The checks come first because the end of the game outranks everything: a checkmate is worth a million points, so the engine always takes a mate it can see and always avoids one it can see coming. A stalemate, a threefold repetition, the fifty-move rule, or too little material to mate all score exactly zero.

Checkmate? Draw? Stalemate? Repetition? Count up asked of every position at the end of a line, in this order The first four questions each make the rules library list every legal move, which is where most of the thinking time goes.

These checks are thorough but expensive: the library answers each question by working out every legal move in the position. The engine therefore examines only a few thousand positions a second, a small number for a chess program, and the counter under the board reports how many it looked at for each move.

Judging a position

If the game isn't over, the engine adds up a score in centipawns, hundredths of a pawn. Everything it knows about chess is in these few rules.

Material. Each piece is counted at a fixed value, and Black's total is subtracted from White's. This is by far the largest part of the score, so the engine plays above all for material.

♙︎ pawn 100 ♘︎ knight 300 ♗︎ bishop 320 ♖︎ rook 500 ♕︎ queen 900

The centre. A piece standing on one of the four central squares earns 12 centipawns, on the squares beside them 5, and on the ring just outside 3. The bonus goes to any piece standing there, even a king, and it rewards standing on the square, not controlling it from a distance.

3 3 3 3 5 12 12 5 5 12 12 5 3 3 3 3 Any piece standing here earns the bonus shown, in centipawns. It counts where pieces stand, not which squares they attack.

Advancing pawns. Every pawn earns 4 centipawns for each rank it has moved forward, so a pawn about to promote is worth 20 more than one still at home. This nudges the engine to push its pawns and to take yours seriously when they advance.

0 rank 2 +4 rank 3 +8 rank 4 +12 rank 5 +16 rank 6 +20 rank 7 White's view; Black's pawns earn the same going the other way

Shelter for the king. The engine looks at the three squares directly in front of each king. A friendly pawn straight ahead is worth 18, and one on either diagonal 12. A king still on the d- or e-file costs 10, which encourages castling. These rules apply the same way throughout the game, including the endgame, where a king would normally want to come forward.

♔︎ ♙︎ ♙︎ ♙︎ ♚︎ ♟︎ ♟︎ White: pawn in front of the king +18, each diagonal neighbour +12 Black: the king on the d-file costs 10; the two diagonal pawns earn 12 each

What it doesn't see

These limits are what make the engine easy to beat once you know them.

The horizon. Because the lookahead always stops at the same depth, the engine can end a line in the middle of an exchange. If the last move it examines is its queen taking a pawn, it counts the pawn and never sees that the pawn was defended. This horizon effect is behind most of its blunders, and setting such traps is the surest way to beat it.

depth 3 ends here what the engine sees engine you queen takes pawn “a pawn up!” what actually happens engine you queen takes pawn pawn takes queen queen lost

No sense of time. All mates score the same million points, so the engine doesn't prefer a mate in one to a mate in three. Once it sees a win, it may take the long way round.

No strategy. It knows nothing about piece activity, weak squares, pawn structure, open files, passed pawns or endgame technique beyond pushing pawns. It plays sound material chess, grabbing what is safe to grab, but it has no plans.

While it thinks

The search runs on the same thread as the page, so the page freezes until the move is chosen. At depth 3 that usually takes a second or two; at depth 4 it can take much longer. If the browser offers to stop the script, wait instead: the engine is still working.

Opening book

For the first few moves, the engine can follow a short, hand-written book of 39 standard opening lines, each up to ten half-moves long, instead of searching. When the moves played so far match the start of a line, it plays the next move of that line, choosing at random when several match. The name of the opening appears in the move history. Switching the book off makes the engine think from the very first move, which is slower and often stranger.

e4 d4 e5 c5 d5 Nf3 Nc6 Bb5 Bc4 d4 c4 Ruy Lopez Italian Game Scotch Game Sicilian Defence Queen's Gambit

House rules

Colours
You play White and move first. The engine plays Black.
Special moves
Castling and en passant are allowed. A pawn reaching the last rank always promotes to a queen.
Draws
Threefold repetition, the fifty-move rule, stalemate and insufficient material all end the game.
No undo
Once a move is released it is final. Start again with New game.
Waiting
The search blocks the page while it runs. If your browser offers to stop the script, wait instead.