chess.js icon indicating copy to clipboard operation
chess.js copied to clipboard

add back() and next() method

Open ebrehault opened this issue 11 years ago • 15 comments

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.

ebrehault avatar Dec 22 '13 10:12 ebrehault

I'm also looking for this feature. Does anyone know the status on this?

JimmyMow avatar May 14 '14 22:05 JimmyMow

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.

davecom avatar May 14 '14 22:05 davecom

Are you talking about variations? E.g.

  1. e4 e5 2. Nc3 (1... e6 2. d4) 2... Nf6

where the line starting with 1... e6 is an alternative history.

ghost avatar May 15 '14 10:05 ghost

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.

ebrehault avatar May 15 '14 11:05 ebrehault

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.

davecom avatar May 15 '14 14:05 davecom

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.

ebrehault avatar May 15 '14 15:05 ebrehault

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

JimmyMow avatar May 19 '14 22:05 JimmyMow

I can also make a pull request and put up the code I wrote

JimmyMow avatar May 19 '14 22:05 JimmyMow

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 .

aaronfi avatar May 19 '14 23:05 aaronfi

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

jhlywa avatar May 20 '14 01:05 jhlywa

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.

bokybanton avatar Dec 07 '14 15:12 bokybanton

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.

fenimore avatar May 03 '15 21:05 fenimore

@JimmyMow :

Here's my PGN variation support: https://github.com/aaronfi/chess-es6.js

aaronfi avatar Dec 09 '15 09:12 aaronfi

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;
        }

Telewa avatar Jul 03 '17 15:07 Telewa

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.

ssommelet21 avatar Aug 23 '22 15:08 ssommelet21