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 🙏

© 2024 – Pkg Stats / Ryan Hefner

logichess

v0.1.7

Published

A chess programming library written in TypeScript

Downloads

13

Readme

logichess

logichess is a TypeScript library that provides a set of classes to handle chess game logic. It can be used to create custom chess games, validate moves, get legal moves, and other useful functionalities related to chess.

Installation

To install logichess, you need to have npm installed. Then run the following command:

npm i logichess

Usage

To use logichess in your TypeScript or JavaScript project, import the module using either CommonJS or ES6 import syntax.

// Common JS
const { Chess } = require("logichess");
// ES Module
import { Chess } from "logichess";

Creating a new game

You can create a new game by simply initializing a new instance of the Chess class:

const game = new Chess();

This will create a new game with the default starting position.

Alternatively, you can create a custom game by passing a FEN string to the constructor:

const fenString =
  "r1bqkb1r/pppp1ppp/2n2n2/4p1N1/2B1P3/8/PPPP1PPP/RNBQK2R b KQkq - 5 4";
const customGame = new Chess(fenString);

Retrieving game information

Once you have a Chess instance, you can retrieve information about the game state by accessing its properties:

console.log(game.data);

Which will log the following data:

{
  status: 'normal',
  difference: 0,
  turn: 'w',
  fen: 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1',
  board: [
    ["r", "n", "b", "q", "k", "b", "n", "r"],
    ["p", "p", "p", "p", "p", "p", "p", "p"],
    [".", ".", ".", ".", ".", ".", ".", "."],
    [".", ".", ".", ".", ".", ".", ".", "."],
    [".", ".", ".", ".", ".", ".", ".", "."],
    [".", ".", ".", ".", ".", ".", ".", "."],
    ["P", "P", "P", "P", "P", "P", "P", "P"],
    ["R", "N", "B", "Q", "K", "B", "N", "R"],
  ],
  moves: [
    {
      from: { rank: 6, file: 0 },
      to: { rank: 4, file: 0 },
      piece: 'P',
      faction: 'w',
      san: 'a4',
      lan: 'a2a4',
      uci: 'a2a4',
      fenResult: 'rnbqkbnr/pppppppp/8/8/P7/8/1PPPPPPP/RNBQKBNR b KQkq a3 0 1'
    },
    {
      from: { rank: 6, file: 0 },
      to: { rank: 5, file: 0 },
      piece: 'P',
      faction: 'w',
      san: 'a3',
      lan: 'a2a3',
      uci: 'a2a3',
      fenResult: 'rnbqkbnr/pppppppp/8/8/8/P7/1PPPPPPP/RNBQKBNR b KQkq - 0 1'
    },
    // ... more moves below
  ]
}

This will log an object containing the following properties:

  • status - the status of the game ("normal", "check", "end")
  • difference - the difference in material value between the two sides
  • turn - the side to move ("w" for white, "b" for black)
  • fen - the FEN string representing the current position
  • board - a two-dimensional array representing the current position on the board
  • moves - an array containing all legal moves in the current position

Making a move

To make a move, call the play method on the Chess instance, passing the move in either SAN, LAN, or UCI format as a string:

// using SAN
game.play("e4");
// using LAN
game.play("e2e4");
// using UCI
game.play("e2-e4");

You can also pass a Move object as the parameter:

game.play({
  from: { rank: 6, file: 4 },
  to: { rank: 4, file: 4 },
  piece: "P",
  faction: "w",
  san: "e4",
  lan: "e2e4",
  uci: "e2e4",
  fenResult: "rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1",
});

To learn more about the Move object, read the source file.

The play method will update the game state with the new position resulting from the move. If the move is not legal, an exception will be thrown.

Contributing

Thank you for considering contributing to logichess! If you would like to contribute, please follow these steps:

  • Fork the repository and create your branch from main.
  • Clone the repository and install dependencies: npm install
  • Commit your changes and push your branch.
  • Open a pull request.

You can also suggest new features to be added by opening new issues about it. Here's a small list of to be implemented features for starters:

  • validateMove - check if a move is legal in the current position
  • getMoveHistory - get an array of all moves played in the game so far
  • undo - undo the last move played

License

logichess is released under the MIT License. See LICENSE for details.