npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@echecs/game

v1.1.0

Published

GAME is part of the ECHECS project, providing a chess game engine with legal move generation, undo/redo, and game-state detection.

Downloads

167

Readme

GAME

npm Test Coverage License: MIT

GAME is a TypeScript chess game engine — part of the ECHECS project.

It provides a single mutable Game class that manages board state, generates legal moves, and detects game-ending conditions. Zero runtime dependencies.

Installation

npm install @echecs/game

Quick Start

import { Game } from '@echecs/game';

const game = new Game();

game.move({ from: 'e2', to: 'e4' });
game.move({ from: 'e7', to: 'e5' });

console.log(game.turn()); // 'w'
console.log(game.moves()); // all legal moves for white
console.log(game.fen()); // current position as FEN string

API

Construction

new Game()

Creates a new game from the standard starting position.

const game = new Game();

Game.fromFen(fen)

Creates a game from an arbitrary FEN string. Throws Error if the FEN is invalid.

const game = Game.fromFen(
  'rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1',
);

Board queries

game.turn()

Returns the color whose turn it is to move: 'w' or 'b'.

game.get(square)

Returns the piece on the given square, or undefined if the square is empty.

game.get('e1'); // { color: 'w', type: 'k' }
game.get('e4'); // undefined

game.board()

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

game.board()[0]?.[4]; // { color: 'w', type: 'k' }  (e1)

game.fen()

Returns the current position as a FEN string.

Move generation

game.moves(square?)

Returns all legal moves for the active color. If square is given, returns only the legal moves for the piece on that square.

game.moves(); // all 20 legal opening moves
game.moves('e2'); // [{ from: 'e2', to: 'e3' }, { from: 'e2', to: 'e4' }]

Each Move object has the shape:

interface Move {
  from: Square;
  to: Square;
  promotion?: 'n' | 'b' | 'r' | 'q';
}

Move execution

game.move(move)

Applies a move. Returns this for chaining. Throws Error if the move is illegal.

game.move({ from: 'e2', to: 'e4' });
game.move({ from: 'e7', to: 'e8', promotion: 'q' }); // promotion

game.undo()

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

game.redo()

Steps forward one move (after an undo). Returns this. No-op at the end of history. Cleared whenever a new move() is made.

game.move({ from: 'e2', to: 'e4' });
game.undo(); // back to start
game.redo(); // e4 again

History

game.history()

Returns the list of moves played so far. Undone moves are not included.

game.move({ from: 'e2', to: 'e4' });
game.history(); // [{ from: 'e2', to: 'e4' }]

State detection

game.isCheck()

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

game.isAttacked(square, color)

Returns true if any piece of color attacks square. The square does not need to be empty — it may contain a piece of either color. A piece does not attack its own square.

const game = new Game();
game.isAttacked('f3', 'w'); // true — white pawn on g2 attacks f3
game.isAttacked('f6', 'b'); // true — black pawn on g7 attacks f6

Pinned pieces still count as attacking. There is no X-ray — a piece blocked by another piece does not attack through it.

game.isCheckmate()

Returns true if the active color is in checkmate.

game.isStalemate()

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

game.isDraw()

Returns true if the position is a draw by any of:

  • 50-move rule (100 half-moves without a pawn move or capture)
  • Insufficient material (K vs K, K+B vs K, K+N vs K, all bishops on the same square colour)
  • Stalemate
  • Threefold repetition

game.isGameOver()

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

Interop

@echecs/game has no dependency on @echecs/pgn or @echecs/uci. The caller bridges them:

import { parse } from '@echecs/pgn';
import { Game } from '@echecs/game';

// Replay a parsed PGN into a Game
const [pgn] = parse(pgnString);
const game = new Game();
for (const [, white, black] of pgn.moves) {
  if (white) {
    game.move({ from: white.from, to: white.to, promotion: white.promotion });
  }
  if (black) {
    game.move({ from: black.from, to: black.to, promotion: black.promotion });
  }
}

Contributing

Contributions are welcome. Please read CONTRIBUTING.md for guidelines on how to submit issues and pull requests.