Menu

Verifying Every Chess Puzzle on My Home Page with a Tiny Solver
πŸ“°

Verifying Every Chess Puzzle on My Home Page with a Tiny Solver

DEV Community: typescriptΒ·Amine AmineΒ·about 3 hours ago
#TzrPwztW
#devtotypescript#devto#dev#move#puzzle
Reading 0:00
15s threshold

My home page opens with a chess puzzle: White to play, mate in 2. It's the first thing visitors see, so it can't be wrong. A puzzle with two solutions, or one that's secretly a mate in 1, makes the whole site look sloppy.

I didn't want to check positions by hand, so I made the computer do it.

The definition, written as code

A mate-in-2 puzzle has exactly one first move for White such that, whatever Black replies, White can then deliver checkmate. Written down, that's three small functions on top of the chess engine that already powers Chess Master :

export function isCheckmate ( pos : ChessPosition ): boolean { return generateLegalMoves ( pos ). length === 0 && isInCheck ( pos , pos . turn ); }

/** After White's first move: does every Black reply allow mate in one? */ export function forcesMateInOne ( pos : ChessPosition ): boolean { if ( isCheckmate ( pos )) return true ; const replies = generateLegalMoves ( pos ); if ( replies . length === 0 ) return false ; // stalemate return replies .…

Continue reading β€” create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.