niklasf / chessops

Chess and chess variant rules and operations in TypeScript
https://niklasf.github.io/chessops/
GNU General Public License v3.0
106 stars 34 forks source link

multiple FEN only PGNs are counted as one #164

Closed Siderite closed 8 months ago

Siderite commented 8 months ago

Take a text like:

[FEN "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/2BQKBNR w Kkq - 0 1"]

[FEN "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/2BQKBNR w Kkq - 0 1"]

Is this two games, or just one?

In the current implementation, multiple PGN tags one after the other are considered part of the same game. However, multiple FEN tags should not be valid. There is the workaround to add a * as a "move" in which case parsePgn will see two games.

mliebelt commented 8 months ago

The PGN specification (see section 8.1) states:

The same tag name should not appear more than once in a tag pair section.

In my opinion, FEN should only be used in combination with [SetUp "1"], without it, FEN has no meaning.

About the Game Termination Markers:

Each movetext section has exactly one game termination marker; the marker always occurs as the last element in the movetext. The game termination marker is a symbol that is one of the following four values: "1-0" (White wins), "0-1" (Black wins), "1/2-1/2" (drawn game), and "*" (game in progress, result unknown, or game abandoned). ...

So the game termination is not a workaround, but normally required. A lot of software outside just ignores the PGN specification.

Siderite commented 8 months ago

Well, Lichess uses [Variant "From Position"] instead of [SetUp "1"] and I am sure a lot of software ignores the SetUp tag. They also ignore the game termination.

Of course, this is an edge case. If you consider it not important, you can close it. I just noticed it when I was working on the PGN Editor.

mliebelt commented 8 months ago

@Siderite Just checked it. When I create a study, based on a FEN, make a few moves, and export that as PGN, the result is.

[Result "*"]
[FEN "r1bqkbnr/pppp1ppp/2n5/4p3/4P3/5N2/PPPP1PPP/RNBQKB1R w KQkq - 2 3"]
[SetUp "1"]

3. Bc4 Nf6 *

I removed all tags not relevant for your case. So lichess does it perfectly well, matches the spec 100%. There may be cases, when you get something different, but export PGN from a study leads to that result. Glad that they do it well ...

niklasf commented 8 months ago

To explain what's happening: The PGN parser in this library is extemely relaxed by design. It accepts any input. Nothing is ever rejected.

That's nice, because it can flexibly deal with quirky real-world inputs.

It's also a bit risky, because there is ambiguity in inputs that are out of spec ... like yours, and also the following example:

[White "Siderite"]

[Black "niklasf"]

1. e4

With the empty line between the tags, is that one or two games? I think it makes sense that this would be a single game, so empty lines between headers are ignored. And so they are also ignored in your example.


Aside: In my opinion, requiring a redundant [SetupUp "1"] is a design mistake. So missing [SetupUp "1"] is ignored/accepted.

Siderite commented 8 months ago

@Siderite Just checked it. When I create a study, based on a FEN, make a few moves, and export that as PGN, the result is.


[Result "*"]
[FEN "r1bqkbnr/pppp1ppp/2n5/4p3/4P3/5N2/PPPP1PPP/RNBQKB1R w KQkq - 2 3"]
[SetUp "1"]

You are right about the study, but the same thing doesn't happen with the Analysis Board PGN. Anyway, if it's the standard, I'd better add it in my own PGN export :)

Siderite commented 8 months ago

@niklasf I understood how it works and I think it's indeed a good design. The difference between your example and mine is that a PGN with two FEN tags would be illegal. But I get it. It would be hard to understand where a PGN starts and one begins if you assume they would be legal having just tags. I will close this.