Skip to content

Latest commit

 

History

History
109 lines (86 loc) · 3.23 KB

chess.ts.chess.loadpgn.md

File metadata and controls

109 lines (86 loc) · 3.23 KB

Home > chess.ts > Chess > loadPgn

Chess.loadPgn() method

Load the moves of a game stored in [Portable Game Notation](http://en.wikipedia.org/wiki/Portable\_Game\_Notation). pgn should be a string. Options is an optional object which may contain a string newline.

The newline is a string representation of a valid RegExp fragment and is used to process the PGN. It defaults to \r?\n. Special characters should not be pre-escaped, but any literal special characters should be escaped as is normal for a RegExp. Keep in mind that backslashes in JavaScript strings must themselves be escaped. Avoid using a newline that may occur elsewhere in a PGN, such as . or x, as this will result in behavior.

The method will throw an error if the PGN was not parsed successfully.

Signature:

loadPgn(pgn: string, options?: {
        newline?: string;
    }): void;

Parameters

Parameter Type Description
pgn string
options { newline?: string; } (Optional)

Returns:

void

Example

const chess = new Chess()
const pgn = [
    '[Event "Casual Game"]',
    '[Site "Berlin GER"]',
    '[Date "1852.??.??"]',
    '[EventDate "?"]',
    '[Round "?"]',
    '[Result "1-0"]',
    '[White "Adolf Anderssen"]',
    '[Black "Jean Dufresne"]',
    '[ECO "C52"]',
    '[WhiteElo "?"]',
    '[BlackElo "?"]',
    '[PlyCount "47"]',
    '',
    '1.e4 e5 2.Nf3 Nc6 3.Bc4 Bc5 4.b4 Bxb4 5.c3 Ba5 6.d4 exd4 7.O-O',
    'd3 8.Qb3 Qf6 9.e5 Qg6 10.Re1 Nge7 11.Ba3 b5 12.Qxb5 Rb8 13.Qa4',
    'Bb6 14.Nbd2 Bb7 15.Ne4 Qf5 16.Bxd3 Qh5 17.Nf6+ gxf6 18.exf6',
    'Rg8 19.Rad1 Qxf3 20.Rxe7+ Nxe7 21.Qxd7+ Kxd7 22.Bf5+ Ke8',
    '23.Bd7+ Kf8 24.Bxe7# 1-0'
]

chess.loadPgn(pgn.join('\n'))
// -> true

chess.fen()
// -> 1r3kr1/pbpBBp1p/1b3P2/8/8/2P2q2/P4PPP/3R2K1 b - - 0 24

chess.ascii()
// -> '  +------------------------+
//     8 | .  r  .  .  .  k  r  . |
//     7 | p  b  p  B  B  p  .  p |
//     6 | .  b  .  .  .  P  .  . |
//     5 | .  .  .  .  .  .  .  . |
//     4 | .  .  .  .  .  .  .  . |
//     3 | .  .  P  .  .  q  .  . |
//     2 | P  .  .  .  .  P  P  P |
//     1 | .  .  .  R  .  .  K  . |
//       +------------------------+
//         a  b  c  d  e  f  g  h'

// Parse non-standard move formats and unusual line separators
const sloppyPgn = [
    '[Event "Wijk aan Zee (Netherlands)"]',
    '[Date "1971.01.26"]',
    '[Result "1-0"]',
    '[White "Tigran Vartanovich Petrosian"]',
    '[Black "Hans Ree"]',
    '[ECO "A29"]',
    '',
    '1. Pc2c4 Pe7e5', // non-standard
    '2. Nc3 Nf6',
    '3. Nf3 Nc6',
    '4. g2g3 Bb4', // non-standard
    '5. Nd5 Nxd5',
    '6. c4xd5 e5-e4', // non-standard
    '7. dxc6 exf3',
    '8. Qb3 1-0'
].join('|')

const options = {
    newline: '\\|', // Literal '|' character escaped
}

chess.loadPgn(sloppyPgn)
// -> false

chess.loadPgn(sloppyPgn, options)
// -> true

chess.fen()
// -> 'r1bqk2r/pppp1ppp/2P5/8/1b6/1Q3pP1/PP1PPP1P/R1B1KB1R b KQkq - 1 8'