chess.js
chess.js copied to clipboard
add back() and next() method
Those 2 methods do not modify the history, which must remain the reference, they just use a secondary list named future, which stores FEN snapshots.
I'm also looking for this feature. Does anyone know the status on this?
Can you tell us more about the use case? Right now there is the method undo() which goes back a move. It also returns the move that it just went back, so you can go forward by calling move() with the object returned by undo() as a parameter.
Are you talking about variations? E.g.
- e4 e5 2. Nc3 (1... e6 2. d4) 2... Nf6
where the line starting with 1... e6 is an alternative history.
What I have implemented in this pull request is different than the undo mechanism (which does modify the history): it allows to navigate into the history.
Oh yeah I understand that. I'm asking what use cases you're using it for that aren't covered by undo() or just navigating through the history.
In my own case, I use chessboardjs and chess.js to load and display a PGN. And two buttons (Back and Next) allow the user to play the PGN position by position. Quite a basic scenario.
But I do not know about the @JimmyMow use case.
I also am using the chessboard.js too, and built in a variation feature and also a feature to load the pgn and then be able to rewind and fast forward without adjusting the history or the Chess objects knowledge of the actual game. Just curious if you guys had any thoughts on adding that as a feature here or if that was out of the scope
I can also make a pull request and put up the code I wrote
I wrote the same feature -- full variations support, with some additional custom logic for my purposes.
I majorly refactored all of chess.js to accommodate this -- with little perf loss.
On Mon, May 19, 2014 at 3:45 PM, JimmyMow [email protected] wrote:
I can also make a pull request and put up the code I wrote
— Reply to this email directly or view it on GitHubhttps://github.com/jhlywa/chess.js/pull/48#issuecomment-43567800 .
@JimmyMow I'd be interested in seeing your implementation.
The current PR works fine, but I dislike how it requires instantiation of another Chess() object in back(). I'm also not sure we need to preserve the move history. Instead, preserve the PGN moves and simply replay them - that way a call to fen() always return the current board position. Deviation (by calling move() instead of next()) could automatically create a new variation.
It might be best to spec out a few unit tests that show intended behavior prior to coding this up. Any thoughts? Even better, any volunteers?
The idea of next() and back() methods are quite interesting. I think this idea also need the method to get the 'move nº X' in actual position with game_fen();
For example in this pgn game we are showing in the board this position: 1. e4, e5 2. Kf3, Kc6 3. Bc4, Bc5 Right now we are handling the game with js, using arrays, etc, and get the position number (3) manually, it will be usefull we can implement this handler to chess.js class.
These functions are really useful for me; for those using chessboard.js, you can integrate next() and back() with something like board.position(game.back())
It would be nice to see a function which skips to the first move, inside this future[] list of tmp FEN positions.
@JimmyMow :
Here's my PGN variation support: https://github.com/aaronfi/chess-es6.js
recommendation for inside the back function:
if (previous > -1) {
future.push(tmp.fen());
}
To safeguard against multiple clicks to the back button beyond the moves list. So that it becomes:
back: function () {
var moves = this.history();
var tmp = new Chess();
var previous = moves.length - future.length - 1;
for (var i = 0; i < previous; i++) {
tmp.move(moves[i]);
}
var previous_fen = tmp.fen();
tmp.move(moves[previous]);
// safeguard against multiple clicks to the back button beyond the moves list
if (previous > -1) {
future.push(tmp.fen());
}
return previous_fen;
}
Hi guys !
i have done this : https://github.com/ssommelet21/chessjsextention/blob/main/index.js
To manage variations and goback. Check the script for a demo.