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

chess-easy

v1.0.3

Published

Chess engine that makes writing chessgame easier than writing a calculator

Downloads

5

Readme

chess-easy

Description

This is a Typescript library for validating chess moves, generating possible moves, detection of check, checkmate and draw (Insufficient Material or Stalemate) and keeping state of the game using FEN notation.

Installation

npm i chess-easy

How to use

You can initialize game without any arguments, and that object will be initialized with starting game fen: "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1". You can also pass different fen string and if it will be valid, game will be initialized with given Fen state.

import { Game } from "chess-easy

const chessGame = new Game();
const gameFromFen = new Game(`rnbqkbnr/pp1ppppp/8/2p5/4P3/5N2/PPPP1PPP/RNBQKB1R b KQkq - 1 2`);

Game object methods

  • getGameStateObject(): GameStateObject: Getting current state of game as object ex. {'e4': {piece: ChessPieces.KNHIGHT, color: COLORS.WHITE}, ...}
  • isPromotionMove(from: string, to: string): boolean: Getting information if given move will cause promoting a pawn, useful for fronted, when you want to show some modal with choice of the promortion piece
  • isDraw(): {isDraw: boolean, reason?: string}: Getting information if current game state is draw. Reason field exists if isDraw is true and can be one of 2 values: 'Stalemate', or 'Insufficient Material'
  • move(from: string, to: string, promotion: PromotionPossibility = PromotionPossibility.QUEEN): boolean - This method validates if given move is correct and makes the move if it is valid. promotion argument is used only if move ends with promotion of a pawn and can have one of four values:
    • PromotionPossibility.ROOK
    • PromotionPossibility.BISHOP
    • PromotionPossibility.KNIGHT
    • PromotionPossibility.QUEEN
  • generateFen(): string: Returns Fen string from current game state
  • getNextColor(): "black" | "white": Returns string with next moving color.

Game object fields

  fen: string;
  gameState: GameState;
  movesNext: Colors;
  castlingAvailability: string;
  enPassantPossibility: string;
  halfMoveClock: number;
  fullMoveNumber: number;
  possibleMoves: PossibleMoves;
  isCheck: boolean;
  isCheckmate: boolean;
  isStalemate: boolean;
  isInsufficientMaterial: boolean;

Types

  • enum ChessPieces {
      PAWN,
      BISHOP,
      KNIGHT,
      ROOK,
      QUEEN,
      KING,
    }
  • enum Colors {
        WHITE,
        BLACK,
      }
  • interface GameStateField {
      color: Colors;
      piece: ChessPieces;
    }
  • type GameStateRow = (GameStateField | null)[];
  • type GameState = GameStateRow[];
  • type GameStateObject = {
      [key: string]: GameStateField;
    };
  • type PossibleMoves = {
      [key: string]: string[];
    };

    Example

    If you want to see an example how this package can be used, you can see this chess service, than uses this package on both: frontend, and backend: https://github.com/mwisniewski00/Chess