pgn-viewer icon indicating copy to clipboard operation
pgn-viewer copied to clipboard

Chess960 castling is not handled

Open undera opened this issue 6 years ago • 15 comments

Hi, I'm applying this nice viewer to Chess960 PGNs. It works well except castling moves, which breaks it with message No legal move: O-O.

Here is PGN:

[Site "checkmate"]
[Round "191"]
[White "Lisa"]
[Black "Stockfish"]
[Result "0-1"]
[FEN "nrnkrqbb/pppppppp/8/8/8/8/PPPPPPPP/NRNKRQBB w KQkq - 0 1"]
[Variant "Chess960"]

1. e4 {0.00} f5 {0.00} 2. Qe2 {0.00} fxe4 {0.00} 3. Qf1 {0.00} g6 {0.00} 4.
Qe2 {0.00} Nab6 {0.00} 5. Qf1 {0.00} Nc4 {0.00} 6. Qe2 {0.00} Nxb2+ {0.00} 7.
Rxb2 {0.00} Bxb2 {0.00} 8. Qf1 {0.00} Bxa1 {0.00} 9. Qe2 {0.00} Nd6 {0.00} 10.
Qf1 {0.00} Bc4 {0.00} 11. Qe2 {0.00} Bxe2+ {0.00} 12. Kxe2 {0.00} Qf5 {0.00}
13. Kd1 {0.00} Bd4 {0.00} 14. c3 {0.00} Bxf2 {0.00} 15. Re2 {0.00} Bxg1 {0.00}
16. d3 {0.00} exd3 {0.00} 17. Ke1 {0.00} dxe2 {0.00} 18. Kxe2 {0.00}
Qc2+ {0.00} 19. Ke1 {0.00} O-O {0.00} 20. c4 {0.00} Bf2+ {0.00} 21. Kf1 {0.00}
Qd1# {0.00} 0-1

Here is my JS code.

        var match = /^\[FEN "([^"]+)"]/m.exec(pgn);
        var cfg = {
            position: match[1],
            pgn: pgn,
            locale: 'en',
            pieceStyle: 'wikipedia',
            timerTime: 200,
            movesHeight: '200px',
        };
        console.log(cfg);
        var board = pgnView('board', cfg);

undera avatar Aug 17 '19 14:08 undera

Nice idea! I never had thought that this could possibly work. The reason is, that the library used for playing and controlling chess is (as I think) not able to check Chess960. I will check that, and if it is possible will add the option to use the PgnViewerJS for it.

mliebelt avatar Aug 17 '19 16:08 mliebelt

See https://github.com/jhlywa/chess.js/issues/122 for an issue in the library I am using. There is the mentioning of a branch that includes a (partial) implementation, I will give it a try. But this "we need X-FEN instead of FEN" frightens me, that does not sound easy ...

mliebelt avatar Aug 17 '19 17:08 mliebelt

Found the castling rules here, they seem to be simple. Don't know how easy it would be to implement them generally in chess.js, however.

mliebelt avatar Aug 17 '19 18:08 mliebelt

I have just added (in a branch) the version of sesse instead of the original one, and added an example to show the effect.

You have to do the following:

  • Clone the repository: clone https://github.com/mliebelt/PgnViewerJS.git.
  • Go into the directory of the repository: cd PgnViewerJS
  • Checkout branch ticket-121: git checkout ticket-121
  • Run npm install
  • Run grunt dev
  • Run npm install -g http-server (to have a local web server, necessary to run the examples).
  • Run http-server --cors, this will display an URL (with port) that servers that directory. This is named <URL> in the following.
  • Open the browser at <URL>/examples/tickets/ticket121.html
  • Play through the game (including castling).

I have to check the changes of course if it is possible to include them into main line of PgnViewerJS. If there is not too much risk, I will do that, and create a new release for easier user of PgnViewerJS (with support for Chess960).

mliebelt avatar Aug 18 '19 09:08 mliebelt

Playing (in edit mode), I have seen that I have no support for castling that works well. That means, the king is set correct, but the rook not. And I have the problem that castling is sometimes not even possible. So in your game (with Rb1 and Kd1), moving the king to c1 is ambiguous: it could be castling, and it could be a normal move. So playing the moves with pgnView would be ok, but not using pgnEdit to create a game for Chess960.

Of course this is only a first draft. There should be an attribute for it (variant: "chess960", default: "chess"), the read of the TAG [Variant "Chess960"], but this is all trivial in comparison to the change of castling rules.

mliebelt avatar Aug 18 '19 09:08 mliebelt

Thanks for all your research. Currently, I'm only using PGNView and castling in PGN is unambiguous with special O-O-kind declarations.

undera avatar Aug 18 '19 10:08 undera

Are you able to build it for yourself?

mliebelt avatar Aug 18 '19 10:08 mliebelt

I'm not big JS guy. When I'm using it, I generated the config via web UI and downloaded the release and use dist directory. Sorry, I don't have npm or grunt on my machine.

undera avatar Aug 18 '19 10:08 undera

Well, I made it to build dist somehow, played ticket121.html and it worked as expected.

undera avatar Aug 18 '19 10:08 undera

Great! So you have at least a version that works for you, and I have some more time to do some checks and to ensure that nothing else will break then.

mliebelt avatar Aug 18 '19 11:08 mliebelt

Yes, thank you very much for your help.

undera avatar Aug 18 '19 11:08 undera

I have just added (in a branch) the version of sesse instead of the original one, and added an example to show the effect.

You have to do the following:

  • Clone the repository: clone https://github.com/mliebelt/PgnViewerJS.git.
  • Go into the directory of the repository: cd PgnViewerJS
  • Checkout branch ticket-121: git checkout ticket-121
  • Run npm install
  • Run grunt dev
  • Run npm install -g http-server (to have a local web server, necessary to run the examples).
  • Run http-server --cors, this will display an URL (with port) that servers that directory. This is named <URL> in the following.
  • Open the browser at <URL>/examples/tickets/ticket121.html
  • Play through the game (including castling).

I have to check the changes of course if it is possible to include them into main line of PgnViewerJS. If there is not too much risk, I will do that, and create a new release for easier user of PgnViewerJS (with support for Chess960).

Can't find the branch ticket-121

I have just added (in a branch) the version of sesse instead of the original one, and added an example to show the effect.

You have to do the following:

  • Clone the repository: clone https://github.com/mliebelt/PgnViewerJS.git.
  • Go into the directory of the repository: cd PgnViewerJS
  • Checkout branch ticket-121: git checkout ticket-121
  • Run npm install
  • Run grunt dev
  • Run npm install -g http-server (to have a local web server, necessary to run the examples).
  • Run http-server --cors, this will display an URL (with port) that servers that directory. This is named <URL> in the following.
  • Open the browser at <URL>/examples/tickets/ticket121.html
  • Play through the game (including castling).

I have to check the changes of course if it is possible to include them into main line of PgnViewerJS. If there is not too much risk, I will do that, and create a new release for easier user of PgnViewerJS (with support for Chess960).

@mliebelt Thank you, also need this feature, but I can't find branch ticket-121, had it been merged into the main branch?

intothephone avatar Mar 21 '24 08:03 intothephone

@intothephone Sorry for the inconvenience, but I did a trial ~4.5 years ago, that never made it into any release. The commit is there, you can see what I tried here. See the commit on Github. Hopefully that is sufficient to understand the idea. As you may see, the ticket is open ...

mliebelt avatar Mar 21 '24 11:03 mliebelt

Got it, thank you very much! Your project is great!

intothephone avatar Mar 21 '24 12:03 intothephone