Skip to content

victorocna/chess-moments

Repository files navigation

Chess moments

This package is a javascript PGN parser that transforms PGN files into chess "moments". In its simplest form, a chess "moment" has a chess move and its corresponding FEN.

Chess moments includes comments, shapes, variations, special characters, everything that you need in order to correctly display a chess game in javascript.

Installation

npm i chess-moments
# or
yarn add chess-moments

API

The API returns various arrays of objects (chess moments). These chese moments have specific keys like move, fen, depth and index

The first chess moment is always an object without the move key. It only has fen, depth, index and optionally comment or shapes.

{
  "move": "e4",
  "fen": "rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1",
  "comment": "Opening the queen and the bishop",
  "depth": 1
}

.flat()

Returns a single level deep array of chess moments. The depth key delimits between variants and subvariants played in the chess game.

.tree()

Returns a two level deep array of chess moments. The first level splits between variants and subvariants played in the chess game.

.train()

Returns a two level deep array of chess moments. It is useful for hiding future moves in chess training.

Extras

.addMomentToTree()

Adds a new move to the chess tree

.getNextMoments()

Move forward in the chess tree

.getPrevMoment()

Move back in the chess tree

.momentsToPgn()

Transform the chess tree into PGN

JSON output

The basic PGN file 1. e4 e5 * will generated the following chess moments in JSON format:

[
  {
    "fen": "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1",
    "depth": 1,
    "index": 0
  },
  {
    "move": "e4",
    "fen": "rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1",
    "depth": 1,
    "index": 1
  },
  {
    "move": "e5",
    "fen": "rnbqkbnr/pppp1ppp/8/4p3/4P3/8/PPPP1PPP/RNBQKBNR w KQkq e6 0 2",
    "depth": 1,
    "index": 2
  }
]

Limitations

This package does not support the following features:

About

PGN parser that transforms PGN files into chess "moments"

Topics

Resources

Stars

Watchers

Forks

Contributors 2

  •  
  •