A Chess Engine in a Browser Tab: Node Budgets, Not Clocks
The Play5s chess opponent runs in the player's own browser, and each of its six levels is limited by a number of search nodes rather than a number of seconds. A level then plays at the same strength on a phone and on a desktop: a slow device takes longer instead of playing weaker. It uses a 0x88 board, alpha-beta search with quiescence and a transposition table, runs in a Web Worker so the board keeps moving, and plays its weaker levels weak on purpose.
This note covers only the computer player that runs in the page.
Why count nodes instead of seconds?
The usual way to stop a chess search is a clock. The time management page of the Chess Programming Wiki describes the clock-driven designs engines use against each other. They are right for a match with a time control, and the wrong tool for a game that runs on a tablet in one household and a desktop in the next.
With a clock, the depth a search reaches depends on the device, on other tabs, and on whether the browser throttled the page a moment ago. The same level would be strong on a fast desktop and weak on an old phone, and a player who switched devices would meet a different opponent under the same name. With a node count, the amount of search is fixed by the level. A phone takes longer to reach the answer than a desktop, but it searches just as much, so level 4 means the same thing everywhere.
A level therefore has two limits: a deepest iteration and a node budget. Every call to the search or the capture-only search counts as a node, and once the count passes the budget the search sets a flag and unwinds.
How is the board represented?
The board is a 0x88 array. The 0x88 representation gives each square a number of rank times 16 plus file, so a1 is 0 and h8 is 119. The second nibble of every index is a gap that doubles as a guard: a move that steps off the board lands on a number with a bit set in 0x88, and one bitwise test rejects it.
Moves are plain integers, with the origin square in the low bits, the target above it, then promotion and flag bits:
// move = from | to << 7 | promo << 14 | flags << 17
const F_CAP = 1, F_EP = 2, F_CASTLE = 4, F_DOUBLE = 8;
Nothing is allocated in the hot loop. Making a move pushes one record of 13 integers onto a preallocated Int32Array undo stack, and unmaking pops it. A Zobrist hash is kept in two 32-bit halves, and the positional part of the evaluation is added and subtracted during make and unmake, so evaluating a position is a constant-time read instead of a scan of 64 squares. The generator produces pseudo-legal moves and a quick test removes those that leave the king attacked, a check that looks only along rays from the king through the square just vacated.
What does the search do?
It is a textbook design, assembled from pages of the Chess Programming Wiki:
- Negamax with alpha-beta pruning, searched with a null window after the first root move and re-searched only when a move beats the best score so far. See alpha-beta.
- Iterative deepening. The search runs depth 1, then 2, and so on, and the best move of the last finished depth is always available. See iterative deepening.
- Quiescence. At the horizon the search keeps trying captures until the position is quiet, which avoids judging a position right after a queen takes a pawn that is defended. See quiescence search.
- A transposition table of 2 to the power 17 entries in typed arrays, so about 2.3 MB. Instead of clearing it before every search, each entry carries a generation stamp and only counts for the search that wrote it. See transposition table.
- Move ordering by the table move, then captures ranked by most valuable victim, least valuable attacker, then two killer moves per ply.
- Evaluation with Tomasz Michniewski's Simplified Evaluation Function: material and piece-square tables, with the king's table blended from middlegame to endgame by game phase. A bishop-pair bonus and a "mop-up" term that herds a lone king toward the edge help the engine actually finish a won ending.
The budget check sits at the top of both the main search and quiescence:
nodes++;
if (nodes > budget) { aborted = true; return 0; }
// at the root, after each iteration:
if (aborted) break; // the unfinished iteration is thrown away
bestMove = roots[bm]; // otherwise keep the deepest finished result
Because iterations are nested, a search that runs out of budget during depth 6 simply plays the move from depth 5. A forced mate found early ends the loop too, since a deeper search cannot change it.
How do the six levels differ?
A level is a row of five numbers.
| Level | Deepest iteration | Node budget | Random bonus per move | Chance of a slip |
|---|---|---|---|---|
| 1 | 1 | 1,500 | up to 1.5 pawns | 30% |
| 2 | 2 | 4,000 | up to 0.7 pawns | 15% |
| 3 | 3 | 8,000 | up to 0.35 pawns | 7% |
| 4 | 4 | 40,000 | up to 0.18 pawns | 2% |
| 5 | 5 | 120,000 | up to 0.08 pawns | none |
| 6 | 7 | 300,000 | up to 0.03 pawns | none |
Strength is only part of it. The low levels are weak on purpose, and in a human way. Each root move gets a random bonus before the best is chosen, so the computer does not repeat itself and sometimes prefers an inferior move. A slip is a deliberate mistake: a clearly worse move picked after a quick look at the captures that follow, such as leaving a piece hanging. The slip code refuses any move that allows mate in one, because a beginner who hangs a bishop is believable and one who walks into a one-move mate is not.
All levels open the way a person would. A table of 40 common opening lines answers the first moves before any search happens. The Hint button uses its own level, depth 6 and 120,000 nodes with no random bonus, so a hint is the same careful search every time.
How does it stay off the main thread?
Thinking happens in a Web Worker, so the board keeps animating while the search runs. MDN's guide to Web Workers covers the mechanics. The page hands the current game to the worker. The worker thinks and answers with one move in UCI notation such as e2e4.
The page is written to survive losing the worker. If the worker cannot be created, or its onerror fires, the pending replies are answered with nothing and the page thinks for itself on the main thread after a 30 ms pause, long enough for the "Thinking" label to paint. Chess is slower that way, but never stuck.
How do we know the move generator is right?
With perft, the standard test for a move generator. Perft counts the leaf positions reachable in a given number of moves, and the Perft Results page lists the accepted numbers. From the starting position the count is 20 at depth 1, 8,902 at depth 3 and 197,281 at depth 4. Our tests run six standard positions, including the awkward ones full of castling, en passant and promotion, and compare against those published counts. One wrong move generated, or one right move missed, shows up as a different total.
Perft proves the rules, not the playing strength. For the engine itself we run self-play as a sanity check: level 3 beat level 1 in six of six games, and level 5 beat level 3 in six of six. That is a check that the dial turns the right way, not a rating, and we do not publish a rating.
What are the limits?
On a busy development machine the search runs at roughly two million nodes per second in Node, so the 300,000-node budget of level 6 is a fraction of a second there. Phones are slower, and that is the point of counting nodes: they take longer and still search just as deep. The table memory is fixed at about 2.3 MB, which is why the engine does not grow with the length of a game.
It is not Stockfish and does not try to be. The target is an opponent whose six levels feel different and that keeps the page responsive while it thinks. If you want to see the levels, pick a different one in the board games on Play5s and play ten moves.