@echecs/game - v2.0.2
    Preparing search index...

    Class Game

    A mutable chess game with legal move generation, undo/redo, and game-state detection.

    const game = new Game();
    game.move({ from: 'e2', to: 'e4' });
    game.move({ from: 'e7', to: 'e5' });
    game.moves('d1'); // legal queen moves
    Index

    Constructors

    • Creates a new game. Defaults to the standard starting position.

      Parameters

      Returns Game

      const game = new Game();                     // starting position
      const game = new Game(customPosition); // from a Position

    Methods

    • Returns the board as an 8x8 array of Piece | undefined, indexed [rank][file] with board()[0] = rank 1 (a1-h1) and board()[7] = rank 8.

      Returns (Piece | undefined)[][]

      const game = new Game();
      game.board()[0]?.[4]; // { color: 'white', type: 'king' } (e1)
    • Returns the piece on the given square, or undefined if the square is empty.

      Parameters

      • square:
            | "a1"
            | "a2"
            | "a3"
            | "a4"
            | "a5"
            | "a6"
            | "a7"
            | "a8"
            | "b1"
            | "b2"
            | "b3"
            | "b4"
            | "b5"
            | "b6"
            | "b7"
            | "b8"
            | "c1"
            | "c2"
            | "c3"
            | "c4"
            | "c5"
            | "c6"
            | "c7"
            | "c8"
            | "d1"
            | "d2"
            | "d3"
            | "d4"
            | "d5"
            | "d6"
            | "d7"
            | "d8"
            | "e1"
            | "e2"
            | "e3"
            | "e4"
            | "e5"
            | "e6"
            | "e7"
            | "e8"
            | "f1"
            | "f2"
            | "f3"
            | "f4"
            | "f5"
            | "f6"
            | "f7"
            | "f8"
            | "g1"
            | "g2"
            | "g3"
            | "g4"
            | "g5"
            | "g6"
            | "g7"
            | "g8"
            | "h1"
            | "h2"
            | "h3"
            | "h4"
            | "h5"
            | "h6"
            | "h7"
            | "h8"

      Returns Piece | undefined

      const game = new Game();
      game.get('e1'); // { color: 'white', type: 'king' }
      game.get('e4'); // undefined
    • Returns the list of moves played so far. Undone moves are not included.

      Returns Move[]

    • Returns true if the active color's king is in check.

      Returns boolean

    • Returns true if the active color is in checkmate.

      Returns boolean

    • Returns true if the position is a draw by any of: 50-move rule, insufficient material, stalemate, or threefold repetition.

      Returns boolean

    • Returns true if the game is over by checkmate or draw.

      Returns boolean

    • Returns true if the active color has no legal moves and is not in check.

      Returns boolean

    • Applies a move and returns this for chaining. Clears the redo stack.

      Parameters

      Returns this

      Error if the move is illegal, with a descriptive message explaining why.

      const game = new Game();
      game.move({ from: 'e2', to: 'e4' }).move({ from: 'e7', to: 'e5' });
      // promotion
      game.move({ from: 'e7', to: 'e8', promotion: 'queen' });
    • Returns all legal moves for the active color. If a square is given, returns only the legal moves for the piece on that square.

      Parameters

      • Optionalsquare:
            | "a1"
            | "a2"
            | "a3"
            | "a4"
            | "a5"
            | "a6"
            | "a7"
            | "a8"
            | "b1"
            | "b2"
            | "b3"
            | "b4"
            | "b5"
            | "b6"
            | "b7"
            | "b8"
            | "c1"
            | "c2"
            | "c3"
            | "c4"
            | "c5"
            | "c6"
            | "c7"
            | "c8"
            | "d1"
            | "d2"
            | "d3"
            | "d4"
            | "d5"
            | "d6"
            | "d7"
            | "d8"
            | "e1"
            | "e2"
            | "e3"
            | "e4"
            | "e5"
            | "e6"
            | "e7"
            | "e8"
            | "f1"
            | "f2"
            | "f3"
            | "f4"
            | "f5"
            | "f6"
            | "f7"
            | "f8"
            | "g1"
            | "g2"
            | "g3"
            | "g4"
            | "g5"
            | "g6"
            | "g7"
            | "g8"
            | "h1"
            | "h2"
            | "h3"
            | "h4"
            | "h5"
            | "h6"
            | "h7"
            | "h8"

      Returns Move[]

      const game = new Game();
      game.moves(); // all 20 legal opening moves
      game.moves('e2'); // [{ from: 'e2', to: 'e3' }, { from: 'e2', to: 'e4' }]
    • Steps forward one move after an undo. No-op if there is nothing to redo.

      Returns void

      The redo stack is cleared whenever a new move is made.

    • Steps back one move. No-op at the start of the game.

      Returns void