Docs /Recipes /Turn a blunder into a puzzle .md

Turn a blunder into a puzzle

Make a puzzle from the position right after a blunder. Engine finds the one winning reply and checks it is the only one, and Knight writes a hint.

A puzzle from a real game: the position right after a blunder, where the player to move has exactly one move that wins. It is a Recipe: copy it, then change the checks and the hint to suit your app.

Calls: one Engine call to find and check the solution, and one Generate call on Knight for the hint, about $0.0028 a puzzle.

The puzzle

Start from a blunder you already know about, such as one that Review a game or Grade a game's accuracy found. The puzzle is the position right after it, sent as the game's PGN and the ply after the blunder.

  1. Engine once, with lines 2. One call answers both questions: the top Line is the solution, and the second Line says whether anything else works too. depth 16 is enough to check a tactic, for a fraction of depth 20's Engine time.
  2. Turn both Lines into win chances for the player to move, from −1 to +1, the way Review a game does: 2 / (1 + exp(−0.00368208 × cp)) − 1, ±1 for a mate, and the sign flipped when Black is to move.
  3. Keep it only if one move wins: the top Line at 0.5 or more, and the second Line at least 0.3 behind it. A position with two winning moves, or with only one legal move, makes a poor puzzle.
  4. Generate on Knight for a hint, given the solution and told not to name it, so the solver gets a nudge rather than the answer.

This checks the first move only. For a puzzle of several moves, run the same check again at each of the solver's later moves, one more Engine call each.

The game below falls into the Elephant trap: White's 6.Nxd5 looks like it wins a pawn, but 6...Nxd5 7.Bxd8 Bb4+ 8.Qd2 Kxd8 wins Black a piece. The curl tab shows the two calls; the Python and JavaScript tabs check the puzzle and print it.

Reading the response: the answer is Engine's lines[0].moves[0]; once the solver finds it, play out the rest of that Line to show why it works. Knight's lines should be empty, since the hint names no move. If it isn't, the hint gives the answer away: ask again, or show the puzzle without one.

# Engine on the position after 6.Nxd5 (ply 11), with the two best Lines.
curl https://api.chessdrive.io/v1/engine \
  -H "Authorization: Bearer $CHESSDRIVE_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "engine": "stockfish",
    "position": { "pgn": "1. d4 d5 2. c4 e6 3. Nc3 Nf6 4. Bg5 Nbd7 5. cxd5 exd5 6. Nxd5", "ply": 11 },
    "depth": 16,
    "lines": 2
  }'

# Only 6...Nxd5 wins, so Knight writes the hint.
curl https://api.chessdrive.io/v1/generate \
  -H "Authorization: Bearer $CHESSDRIVE_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "knight-1",
    "system": "You write hints for chess puzzles. You are given the puzzle'\''s solution.\nIn one or two sentences, point the solver at the idea without naming the winning move, the piece that plays it or the square it goes to.\nNever show any moves.",
    "position": { "pgn": "1. d4 d5 2. c4 e6 3. Nc3 Nf6 4. Bg5 Nbd7 5. cxd5 exd5 6. Nxd5", "ply": 11 },
    "messages": [{ "role": "user", "content": "The solution is Nxd5 Bxd8 Bb4+ Qd2 Kxd8. Write the hint." }]
  }'