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 .β¦