All Projects → shaack → cm-pgn

shaack / cm-pgn

Licence: MIT license
Parse and create PGNs (Portable Game Notation for chess games)

Programming Languages

javascript
184084 projects - #8 most used programming language
PEG.js
56 projects

Projects that are alternatives of or similar to cm-pgn

chess-puzzle-maker
Creates chess puzzles from chess games and positions
Stars: ✭ 34 (+112.5%)
Mutual labels:  chess, pgn
kokopu
A JavaScript/TypeScript library implementing the chess game rules and providing tools to read/write the standard chess file formats.
Stars: ✭ 18 (+12.5%)
Mutual labels:  chess, pgn
php-chess
A chess library for PHP.
Stars: ✭ 42 (+162.5%)
Mutual labels:  chess, pgn
chess
An R package to read, write, create and explore chess games
Stars: ✭ 60 (+275%)
Mutual labels:  chess, pgn
chessalyzer.js
A JavaScript library for batch analyzing chess games
Stars: ✭ 14 (-12.5%)
Mutual labels:  chess, pgn
rust-pgn-reader
Fast non-allocating and streaming reader for chess games in PGN notation
Stars: ✭ 46 (+187.5%)
Mutual labels:  chess, pgn
liPGN
This program uses the lichess REST API to retrieve all the games of a user and transform them into a big PGN file that you can then import into your favourite game analysis tool (ie. scid)
Stars: ✭ 14 (-12.5%)
Mutual labels:  chess, pgn
fastchess
Predicts the best chess move with 27.5% accuracy by a single matrix multiplication
Stars: ✭ 75 (+368.75%)
Mutual labels:  chess
Chess.NET
[No longer maintained] A .NET chess library, written in C#
Stars: ✭ 48 (+200%)
Mutual labels:  chess
Zerofish
An implementation of the AlphaZero algorithm for chess
Stars: ✭ 34 (+112.5%)
Mutual labels:  chess
protochess
Online multiplayer chess website that lets you build custom pieces/boards. Written in Svelte + Rust.
Stars: ✭ 95 (+493.75%)
Mutual labels:  chess
pgn-tactics-generator
Generate chess puzzles / tactics from a pgn file
Stars: ✭ 83 (+418.75%)
Mutual labels:  chess
ChineseChessOL
Online Heads-Up version of Chinese Chess built with Unity 3D
Stars: ✭ 15 (-6.25%)
Mutual labels:  chess
Demolito
UCI Chess Engine
Stars: ✭ 41 (+156.25%)
Mutual labels:  chess
chessboard-recognizer
Uses neural networks to extract chess positions from images
Stars: ✭ 59 (+268.75%)
Mutual labels:  chess
Online-Chess
A chess website where people can play against each other online.
Stars: ✭ 28 (+75%)
Mutual labels:  chess
Chessman
Chess analysis Universal Windows 10 application.
Stars: ✭ 14 (-12.5%)
Mutual labels:  chess
chess-openings
An aggregated data set of chess opening names
Stars: ✭ 223 (+1293.75%)
Mutual labels:  chess
bevy chess
Chess demo in Bevy
Stars: ✭ 59 (+268.75%)
Mutual labels:  chess
Mzinga
Open-source software to play the board game Hive.
Stars: ✭ 57 (+256.25%)
Mutual labels:  chess

cm-pgn

Parser for PGNs (Portable Game Notation)

This is as ES6 Module for parsing and rendering of PGNs (Portable Game Notation).

The API is similar to history() of chess.js, but this module supports variations, nags and comments in the pgn.

We used the nice grammar file from PgnViewerJS of mliebelt to create the parser.

Install

npm install cm-pgn

Usage

Use the Pgn class as JS Module:

<script type="module">
    import {Pgn} from "./PATH/TO/cm-pgn/src/Pgn.js"
    // parse pgn
    const pgn = new Pgn(`[Site "Berlin"]
[Date "1989.07.02"]
[White "Haack, Stefan"]
[Black "Maier, Karsten"]

1. e4 e5 (e6) 2. Nf3 $1 {Great move!} Nc6 *`)
</script>

Pgn constructor

constructor(pgnString = "", props = {})

if you set { sloppy: true } in props, some non-standard move notations will be accepted. See also .move(move, options) from chess.js.

Data structure

The pgn has a pgn.header and a pgn.history.

pgn.header

The header holds the PGN header elements in the key value object tags.

pgn.header.tags = {
    Site: "Berlin",
    Date: "1989.07.02",
    White: "Haack, Stefan",
    Black: "Maier, Karsten"
}

pgn.history

The moves are stored in an array. Every element of that array has the following structure

pgn.history.moves[i] = {
    color: "w", // the moving color
    fen: "rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1", // the fen after that move
    flags: "b", // the flags, like described below
    from: "e2", // the square from
    next: {color: "b", from: "e7", to: "e6", flags: "n", piece: "p", /*…*/}, // a pointer to the next move 
    piece: "p", // the piece type 
    ply: 1, // the ply number
    previous: undefined, // a pointer to the previous move
    san: "e4", // the move in SAN notation
    to: "e4", // the square to
    variation: (4) [{/*…*/}, {/*…*/}, {/*…*/}, {/*…*/}], // a pointer to the begin of the current variation
    variations: [] // all variations starting with that move
}

pgn.history.moves[i].flags

  • 'n' - a non-capture
  • 'b' - a pawn push of two squares
  • 'e' - an en passant capture
  • 'c' - a standard capture
  • 'p' - a promotion
  • 'k' - kingside castling
  • 'q' - queenside castling

pgn.history.moves[i].piece

  • 'p' - pawn
  • 'n' - knight
  • 'b' - bishop
  • 'r' - root
  • 'q' - queen
  • 'k' - king

Examples

const history = pgn.history
assert.equal(4, history.moves.length)
assert.equal(history.moves[0].san, "e4")
assert.equal(history.moves[1].variations.length, 1)
assert.equal(history.moves[1].variations[0][0].san, "e6")
assert.equal(history.moves[2].nag, "$1")
assert.equal(history.moves[2].commentAfter, "Great move!")
assert.equal(history.moves[2].fen, "rnbqkbnr/pppp1ppp/8/4p3/4P3/5N2/PPPP1PPP/RNBQKB1R b KQkq - 1 2")
assert.equal(history.moves[3].from, "b8")
assert.equal(history.moves[3].to, "c6")
assert.equal(history.moves[3].san, "Nc6")
assert.equal(history.moves[3].previous.san, "Nf3")
assert.equal(history.moves[3].previous.next.san, "Nc6")

Development

This module uses PEG.js for parser generation. The parser (pgnParser.js) in src/cm-pgn/parser/ is generated from the grammar file src/grammar/pgn.pegjs.

To recreate the parser after modification of src/grammar/pgn.pegjs, run bin/generate-parser.sh.

Testing

Run the unit tests

External Links

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].