# 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](/docs/recipes): 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: 2*n* + 1 calls for a game of *n* moves. A [whole-game Engine call](/docs/engine#review-a-whole-game), which [Review a game](/docs/recipes/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](/docs/recipes/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. ```bash curl # 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 }' ``` ```python Python import math import os import re import requests PGN = "1. e4 e5 2. Nf3 d6 3. Bc4 Bg4 4. Nc3 g6 5. Nxe5 Bxd1 6. Bxf7+ Ke7" def best_line(ply): r = requests.post( "https://api.chessdrive.io/v1/engine", headers={"Authorization": f"Bearer {os.environ['CHESSDRIVE_API_KEY']}"}, json={"engine": "stockfish", "position": {"pgn": PGN, "ply": ply}, "depth": 20}, ) r.raise_for_status() return r.json()["lines"][0] def win_percent(line): """White's win percentage from 0 to 100, as Lichess computes it.""" if line["mate"] is not None: return 100 if line["mate"] > 0 else 0 return 50 + 50 * (2 / (1 + math.exp(-0.00368208 * line["cp"])) - 1) def move_accuracy(lost): if lost <= 0: return 100 return max(0, min(100, 103.1668 * math.exp(-0.04354 * lost) - 3.1669)) sans = [t for t in PGN.split() if not re.match(r"\d+\.", t)] wins = [win_percent(best_line(ply)) for ply in range(len(sans) + 1)] scores = {"White": [], "Black": []} for ply, san in enumerate(sans): white = ply % 2 == 0 lost = wins[ply] - wins[ply + 1] if white else wins[ply + 1] - wins[ply] score = move_accuracy(lost) scores["White" if white else "Black"].append(score) print(f"{ply // 2 + 1}{'.' if white else '...'}{san} {score:.0f}") for side, s in scores.items(): print(side, f"{sum(s) / len(s):.1f}") ``` ```js JavaScript const PGN = "1. e4 e5 2. Nf3 d6 3. Bc4 Bg4 4. Nc3 g6 5. Nxe5 Bxd1 6. Bxf7+ Ke7"; async function bestLine(ply) { const res = await fetch("https://api.chessdrive.io/v1/engine", { method: "POST", headers: { Authorization: `Bearer ${process.env.CHESSDRIVE_API_KEY}`, "Content-Type": "application/json", }, body: JSON.stringify({ engine: "stockfish", position: { pgn: PGN, ply }, depth: 20 }), }); if (!res.ok) throw new Error(await res.text()); return (await res.json()).lines[0]; } // White's win percentage from 0 to 100, as Lichess computes it. const winPercent = (line) => line.mate !== null ? (line.mate > 0 ? 100 : 0) : 50 + 50 * (2 / (1 + Math.exp(-0.00368208 * line.cp)) - 1); const moveAccuracy = (lost) => lost <= 0 ? 100 : Math.max(0, Math.min(100, 103.1668 * Math.exp(-0.04354 * lost) - 3.1669)); const sans = PGN.split(" ").filter((t) => !/^\d+\./.test(t)); const wins = []; for (let ply = 0; ply <= sans.length; ply++) wins.push(winPercent(await bestLine(ply))); const scores = { White: [], Black: [] }; for (const [ply, san] of sans.entries()) { const white = ply % 2 === 0; const lost = white ? wins[ply] - wins[ply + 1] : wins[ply + 1] - wins[ply]; const score = moveAccuracy(lost); scores[white ? "White" : "Black"].push(score); console.log(`${Math.floor(ply / 2) + 1}${white ? "." : "..."}${san} ${score.toFixed(0)}`); } for (const [side, s] of Object.entries(scores)) console.log(side, (s.reduce((a, b) => a + b, 0) / s.length).toFixed(1)); ``` ```text Output 1.e4 96 1...e5 100 2.Nf3 98 2...d6 83 3.Bc4 87 3...Bg4 81 4.Nc3 89 4...g6 45 5.Nxe5 99 5...Bxd1 27 6.Bxf7+ 100 6...Ke7 100 White 94.9 Black 72.6 ```