Giter VIP home page Giter VIP logo

pgn-handler's Introduction

PGN-Handler

PGN-Handler is a package containing a collection of functions for handling PGN files, aka Portable Game Notaion, including a parser, modification functions, and validation function.

Development Progress

This project is still under development. Currently it only has a parser, a validation function, and some modifier functions for the parsed result, the last two types of functions are essentially wrapper functions for chess.js.

API

parseRaw(filename)

parseRaw(filename) takes a PGN filename and parse the file. It returns the raw output from the parser.

> console.log(JSON.stringify(parseRaw('test_pgns/test_legal_moves.pgn'), null, 2));
{
  "header": {
    "event": "F/S Return Match",
    "site": "Belgrade, Serbia JUG",
    "date": {
      "year": "1992",
      "month": "11",
      "day": "04"
    },
    "round": null,
    "white": "Fischer, Robert J.",
    "black": "Spassky, Boris V.",
    "result": "Draw"
  },
  "moves": [
    {
      "move": 1,
      "white": {
        "type": 1,
        "piece": "P",
        "from": {
          "file": null,
          "rank": null
        },
        "to": {
          "file": "e",
          "rank": "4"
        },
        "suffixParsed": null,
        "san": "e4"
      },
      "black": {
        "type": 1,
        "piece": "P",
        "from": {
          "file": null,
          "rank": null
        },
        "to": {
          "file": "e",
          "rank": "5"
        },
        "suffixParsed": null,
        "san": "e5",
        "rav": [
          {
            "move": 1,
            "white": null,
            "black": {
              "type": 1,
              "piece": "P",
              "from": {
                "file": null,
                "rank": null
              },
              "to": {
                "file": "c",
                "rank": "4"
              },
              "suffixParsed": null,
              "san": "c4"
            }
          }
        ]
      }
    },
    {
      "move": 2,
      "white": {
        "type": 1,
        "piece": "N",
        "from": {
          "file": null,
          "rank": null
        },
        "to": {
          "file": "f",
          "rank": "3"
        },
        "suffixParsed": null,
        "san": "Nf3"
      },
      "black": null
    }
  ]
}

This PGN file is in this repo

parse(filename)

parse(filename) is a wrapper function of parseRaw(filename). In addition to what parseRaw(filename) can do, parse(filename):

  • Labels if the current move is legal or not. If it's not then the following moves will not get labeled
  • Fills in the starting position of each move(fills in from)
  • Add current FEN after each move
  • Judge the result of game and correct the result in header if necessary
> console.log(JSON.stringify(parse('test_pgns/test_legal_moves.pgn'), null, 2));
{
  "header": {
    "event": "F/S Return Match",
    "site": "Belgrade, Serbia JUG",
    "date": {
      "year": "1992",
      "month": "11",
      "day": "04"
    },
    "round": null,
    "white": "Fischer, Robert J.",
    "black": "Spassky, Boris V.",
    "result": "Draw"
  },
  "moves": [
    {
      "move": 1,
      "white": {
        "type": 1,
        "piece": "P",
        "from": {
          "file": null,
          "rank": null
        },
        "to": {
          "file": "e",
          "rank": "4"
        },
        "suffixParsed": null,
        "san": "e4",
        "legalMove": true,
        "fen": "rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1"
      },
      "black": {
        "type": 1,
        "piece": "P",
        "from": {
          "file": null,
          "rank": null
        },
        "to": {
          "file": "e",
          "rank": "5"
        },
        "suffixParsed": null,
        "san": "e5",
        "rav": [
          {
            "move": 1,
            "white": null,
            "black": {
              "type": 1,
              "piece": "P",
              "from": {
                "file": null,
                "rank": null
              },
              "to": {
                "file": "c",
                "rank": "4"
              },
              "suffixParsed": null,
              "san": "c4",
              "legalMove": false
            }
          }
        ],
        "legalMove": true,
        "fen": "rnbqkbnr/pppp1ppp/8/4p3/4P3/8/PPPP1PPP/RNBQKBNR w KQkq e6 0 2"
      }
    },
    {
      "move": 2,
      "white": {
        "type": 1,
        "piece": "N",
        "from": {
          "file": null,
          "rank": null
        },
        "to": {
          "file": "f",
          "rank": "3"
        },
        "suffixParsed": null,
        "san": "Nf3",
        "legalMove": true,
        "fen": "rnbqkbnr/pppp1ppp/8/4p3/4P3/5N2/PPPP1PPP/RNBQKB1R b KQkq - 1 2"
      },
      "black": null
    }
  ],
  "resultOnBoard": "Unfinished"
}

Contributing

Please run npx eslint changed_file.js before commiting

pgn-handler's People

Contributors

alpacamax avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.