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

yd-chess-lib

v1.4.64

Published

Library to emulate a chess game.

Downloads

29

Readme

Chess Lib

npm

Chess Lib is a library that allows us emulate a game of chess.

Installation

npm install yd-chess-lib

Getting started with Chess Lib

Here is an example of a basic app using Chess Lib:

import { Chess } from "yd-chess-lib";

let chess = Chess.getInstance();

Public Methods

To access the library you have to call "getInstance()" in every call. We implement a Singleton, to avoid duplicating data.

getInstance

getInstance();
import { Chess } from "yd-chess-lib";

let chess = Chess.getInstance();

printChessboardToConsole

Print the chessboard in the console.

printChessboardToConsole = () => void;
import { Chess } from "yd-chess-lib";

let chess = Chess.getInstance();

//print chessboard
chess.printChessboardToConsole();

move

Moves the piece located in square from to square to. Only if it is a valid and allowed move. Return true if the move was made. Also can move and promote a pawn in one movement example: "7cx8bQ" || "2bx1cR" ("Q","R","N","B")

move = (from: String, to: string) => boolean; || move = (movement: string) => boolean;
import { Chess } from "yd-chess-lib";

let chess = Chess.getInstance();

// moving 2d => 4d
chess.move("2d", "4d"); //return true
chess.printChessboardToConsole();

// moving 2b => 3b
chess.move("2bx3b"); //return true
chess.printChessboardToConsole();

// moving 2c => 5c
chess.move("2cx5c"); //return false (invalid or not allowed movement)
chess.printChessboardToConsole();

// moving 3b => 8bQ
chess.move("3b", "4b"); //return true
chess.move("4bx5b"); //return true
chess.move("5bx6b"); //return true
chess.move("6bx7a"); //return true
chess.move("7ax8bQ"); //return true
chess.printChessboardToConsole();

Result

//moved 2d => 4d         //moved 2b => 3b         //invalid move           //Default 3b => 8bQ
RB|NB|BB|QB|KB|BB|NB|RB  RB|NB|BB|QB|KB|BB|NB|RB  RB|NB|BB|QB|KB|BB|NB|RB  RB|QW|BB|QB|KB|BB|NB|RB
PB|PB|PB|PB|PB|PB|PB|PB  PB|PB|PB|PB|PB|PB|PB|PB  PB|PB|PB|PB|PB|PB|PB|PB  60|PB|PB|PB|PB|PB|PB|PB
50|51|52|53|54|55|56|57  50|51|52|53|54|55|56|57  50|51|52|53|54|55|56|57  50|51|52|53|54|55|56|57
40|41|42|43|44|45|46|47  40|41|42|43|44|45|46|47  40|41|42|43|44|45|46|47  40|41|42|43|44|45|46|47
30|31|32|PW|34|35|36|37  30|31|32|PW|34|35|36|37  30|31|32|PW|34|35|36|37  30|31|32|PW|34|35|36|37
20|21|22|23|24|25|26|27  20|PW|22|23|24|25|26|27  20|PW|22|23|24|25|26|27  20|21|22|23|24|25|26|27
PW|PW|PW|13|PW|PW|PW|PW  PW|11|PW|13|PW|PW|PW|PW  PW|11|PW|13|PW|PW|PW|PW  PW|11|PW|13|PW|PW|PW|PW
RW|NW|BW|QW|KW|BW|NW|RW  RW|NW|BW|QW|KW|BW|NW|RW  RW|NW|BW|QW|KW|BW|NW|RW  RW|NW|BW|QW|KW|BW|NW|RW

getSquare

We get the piece by passing the position.

getSquare = (position: string) => boolean;
let chess = Chess.getInstance();
//pawn
let square_2_d = chess.getSquare("2d");
console.log(square_2_d);

Result

{ key: "2d", color: "W", type: "P", movementsAllowed: ["3d", "4d"], neverMoved: true }
let chess = Chess.getInstance();
//empty square
let square_5_c = chess.getSquare("fc");
console.log(square_5_c);

Result

null

getChessboard

We get the all chessboard.

getChessboard = () :
  Array<Array<{ key: string; color: string; type: string; movementsAllowed: Array<string>; neverMoved: boolean } | null>>;
let chess = Chess.getInstance();
let chessboard = chess.getChessboard();
console.log(chessboard);

Result

[
  [
    {
      key: '1a',
      color: 'W',
      type: 'R',
      movementsAllowed: [],
      neverMoved: true
    },
    {
      key: '1b',
      color: 'W',
      type: 'N',
      movementsAllowed: ["3c", "3a"],
      neverMoved: true
    },
    ...

getHistory

We get an arrays with all the movements made in order that were made.

getHistory = (): Array<string>;
let history = chess.getHistory();
console.log(history);

Result

["2dx4d", "2bx3b"]

isInCheckMate

This function return a boolean with value true is the color is in checkmate. Parameter color "W" (White) "B" (Black)

isInCheckMate = (color: Color): boolean;
chess.isInCheckMate("W"); // check if white is in checkmate
chess.isInCheckMate("B"); // check if black is in checkmate

replacePawn

This function allow us to replace the pawn if is in the end of the rows for a Queen(Q), Rook(R), Bishop(B) or Knight(N).

pawnPromotion = (pawn_key: string, type_of_piece: TypeOfPiece): void
chess.replacePawn("1b", "Q");
chess.replacePawn("8a", "N");

isDraw

This function evaluates if it’s a draw returns a string with the type of draw. if isn’t a draw return null. p_color is the color that it is turn to play

isDraw = (p_color: Color): string | null
chess.isDraw("W"); //   "Slatemate"
chess.isDraw("W"); //   "Dead Position"
chess.isDraw("B"); //   "Repetition"
chess.isDraw("W"); //   null

isCasteling

This function evaluates the board and return a boolean with the value true if it can castling.

isCasteling = (kingPosition: string, rookPosition: string): boolean
chess.isCasteling("1e", "1h"); //   true
chess.isCasteling("1e", "1h"); //   false (may does not meet all requirements)
chess.isCasteling("1e", "1b"); //   false

hasToPromoteAPawn

hasToPromoteAPawn return true if you have a replace a pawn.

hasToPromoteAPawn = (): boolean
chess.hasToPromoteAPawn(); //   true