Scholar's Mate example
Scholar’s Mate is the classic four-move checkmate: 1. e4 e5 2. Bc4 Nc6 3. Qh5 Nf6 4. Qxf7#. The example below plays out this game using only chess-core — parsing SAN, making moves, and detecting checkmate.
Example
Section titled “Example”import { fromFen, STARTING_FEN, makeMove, fromSan, toSan, isCheckmate,} from '@pech/chess-core';
const moves = ['e4', 'e5', 'Bc4', 'Nc6', 'Qh5', 'Nf6', 'Qxf7#'];
let pos = fromFen(STARTING_FEN);
for (const san of moves) { const move = fromSan(pos, san); if (!move) throw new Error(`Invalid move: ${san}`); console.log(toSan(pos, move)); pos = makeMove(pos, move);}
console.log('Checkmate:', isCheckmate(pos)); // trueOutput
Section titled “Output”e4e5Bc4Nc6Qh5Nf6Qxf7#Checkmate: trueAfter 4. Qxf7#, Black is in checkmate. The final position is produced entirely by repeated fromSan and makeMove; isCheckmate(pos) confirms the game is over.