Docs /Recipes /Grade a game's accuracy .md

Grade a game's accuracy

Score how well each side played, from 0 to 100, from one Engine call per position and the formula Lichess uses.

A score from 0 to 100 for each side of a finished game, the kind players compare after every game. It is a Recipe: copy it, then show the score however suits your app.

Calls: one Engine call per position, 81 for a 40-move game, about $0.033 a game on Stockfish, and no Generate call.

The grade

Accuracy turns Engine's numbers into one score a player understands: 100 for a game with no mistakes, falling quickly with each move that throws away winning chances.

  1. Engine once on every position, at depth 20, keeping the top Line of each: 2n + 1 calls for a game of n moves. A whole-game Engine call, which Review a game makes, gives every position's evaluation in one call at depth 14; if you review the game too, grade it from those results, and don't call Engine twice.
  2. Turn each evaluation into White's win percentage, from 0 to 100: 50 + 50 × (2 / (1 + exp(−0.00368208 × cp)) − 1), and 100 or 0 for a mate.
  3. Score each move by the win percentage it lost for the side that played it: 103.1668 × exp(−0.04354 × lost) − 3.1669, kept between 0 and 100. A move that loses nothing scores 100.
  4. A side's accuracy is the mean of its moves' scores. Lichess, whose formulas these are, also weighs each move by how sharp the position was, so its figure for the same game can differ by a few points.

The game below is the one from Review a game: Black's two blunders, 4...g6 and 5...Bxd1, pull Black's score down to about 73. The curl tab shows one Engine call; the Python and JavaScript tabs grade the whole game, 13 Engine calls for its 12 moves.

Reading the response: each Engine call's lines[0] gives cp, or mate when Engine sees a forced mate, both from White's point of view.

# Engine on one position, here the one after 4...g6 (ply 8). Repeat for every ply from 0 to the end.
curl https://api.chessdrive.io/v1/engine \
  -H "Authorization: Bearer $CHESSDRIVE_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "engine": "stockfish",
    "position": { "pgn": "1. e4 e5 2. Nf3 d6 3. Bc4 Bg4 4. Nc3 g6 5. Nxe5 Bxd1 6. Bxf7+ Ke7", "ply": 8 },
    "depth": 20
  }'