mliebelt / pgn-viewer

Simple PGN viewer with the necessary features to display chess games
GNU General Public License v3.0
161 stars 44 forks source link

Chess960 castling is not handled #121

Open undera opened 5 years ago

undera commented 5 years ago

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);
mliebelt commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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:

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 commented 5 years ago

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.

undera commented 5 years ago

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

mliebelt commented 5 years ago

Are you able to build it for yourself?

undera commented 5 years ago

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 commented 5 years ago

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

mliebelt commented 5 years ago

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.

undera commented 5 years ago

Yes, thank you very much for your help.

intothephone commented 6 months ago

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?

mliebelt commented 6 months ago

@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 ...

intothephone commented 6 months ago

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

intothephone commented 6 months ago

@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 I've tried your commit, it works perfectly for short castle, but not work for long castle: O-O-O, take following pgn as example(Ding Liren VS Carlsen at 2024 Freestyle Chess GOAT Prel):

  1. f4 f5 2. c4 c6 3. g3 g6 4. Nd3 Nd6 5. Ne3 Ne6 6. Ne5 g5 7. c5 gxf4 8. gxf4 Nxf4 9. d4 Nf7 10. Nxf7 Bxf7 11. Nxf5 Bg6 12. e4 d5 13. cxd6 exd6 14. Bf2 Qf8 15. Qg4 Bxf5 16. Qxf5 Qxf5 17. exf5 O-O 18. Be4 d5 19. Bc2 Nh3 20. Rf1 Nxf2 21. Rxf2 Bxd4 22. Rg2+ Kf7 23. O-O-O Rg8 24. Re2 Bf6 25. Rde1 Rg4 26. Re6 Rg2 27. R1e2 Rxe2 28. Rxe2 Rg8 29. Bd3 Rg4 30. Kc2 Rh4 31. b3 Be7 32. Rf2 Bc5 0-1

O-O is ok at step 17, but fails at step 23 for O-O-O

I know it has been long years for this commit, and this also should be a feature request for the backend chess.js lib, but I would be appreciate for your help if you can make long castle works too.

Anyway, great work, thanks again!