New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@chesslib/core

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@chesslib/core

A fast & universal chesslibrary with Basic AI support. This is the core part of the @chesslib bundle.

  • 1.1.9
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

@chesslib/core

A fast & universal chesslibrary with Basic AI support. This is the core part of the @chesslib bundle.

This library is required for

  • @chesslib/ai
  • @chesslib/game
  • @chesslib/plugin

Install:

npm install @chesslib/core

https://www.npmjs.com/package/@chesslib/core

Intro

Get all available moves

import { Chessboard, Positions, COLORS } from "@chesslib/core";

var board = Chessboard.fromPosition(Positions.default);

// Get all possible moves
var moves = board.getFENMoves();
if(board.tryMoveFEN(moves[0].from, moves[0].to))
{
    console.log("Move was valid");
}

Get all available moves for BLACK

// Get all possible moves for BLACK
var moves = board.getFENMoves(COLORS.BLACK);

Get all available moves for A2 Pawn?

// Get all possible moves for BLACK
var moves = board.getFENMovesatFEN("a2");

Validate if move is possible?

// Get all possible moves for BLACK
if(board.isValidMoveFEN("a2", "a4"))
{
	console.log("Move is valid");
}

Check if king was captured

// Get all possible moves
if(board.findPieces("K").length == 1)
{
	console.log("GAME OVER!");
}

For more please see examples folder

Chess AI

The AI is a simple implementation of a basic board evaluation. The AI is trying every moves possible confirming the board value is better for a given move. The search depth can be configured, and due to it's high CPU intensivity it is recommended to use it with webworkers in browsers, and run it as a seperate process or thread in nodejs if required.

AI REPO: https://github.com/azarus/chesslib-ai The example use of the AI is available in the repo under examples/ folder.

API

Chessboard

Thirdparty:
  • Add Move History

addHistory(piece: Piece, data: any);

  • Property Board Size

boardSize: {x: number, y: number}

  • Returns the character representation of a square by it's index

charIndex(index: number): string;

  • clones the board

clone(): Chessboard

Squares
  • clears a square

clearSquare(square: Square);

  • Spawns a piece on a square

createPiece(type: string, color: number | COLORS, square: Square);

  • Makes a move using a move object

doMove(move: MoveObject);

  • Makes a move using strings

doMoveFEN(move: { from: string, to: string});

  • Converts a FEN string to position

FENtoPosition(fen: string): {x: number, y: number};

  • Returns the square for a FEN string

FENToSquare(fen: string): SVGPathSegCurvetoQuadraticRel

  • Finds all pieces on the board

findPieces(pieceType: string, color: number | COLORS): Piece[]

  • Returns a board object (Usefull for chessboard visualization)

getBoard(): { BoardObject }

  • Set's the board from an object

setBoard(board: { BoardObject });

  • Returns the 2D Array representation of the board

savePositions(): any[]

  • Set's the board from a 2D Array

loadPositions(positions: any[]);

import { Chessboard, Positions } from "@chesslib/core";

var board = Chessboard.fromPosition(Positions.default);

// Get all possible moves
var moves = board.getFENMoves();
if(board.tryMoveFEN(moves[0].from, moves[0].to))
{
    console.log("Move was valid");
}

For more please see examples folder

Use the lib together with:

and

to create your game. Have happy coding :)

License:

Apache 2.0

Got an issue or question?

FAQs

Package last updated on 31 Dec 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc