0

I'm trying to create a chess game using JavaFX and I found making the match history quite tricky, after succesful print of the history I'm trying to add posibility to go back to some board state.

I tried this way, but it wouldn't work: The first part is a snippet from my controller Initialize function

        table.setRowFactory( tv -> {
        TableRow<Move> move = new TableRow<Move>();
        move.setOnMouseClicked(event -> {
            if(event.getClickCount()==2 && !move.isEmpty())
                setBoardTo(move.getItem());
            else
                System.out.println("kek");
        });
        return move;
    });

Then we have the function that we go in(it works), but even after changing the board to another and redrawing it, it looks the same way.

    private void setBoardTo(Move move){
    System.out.println(move.boardState.toString());
    System.out.println(board.toString());
    board.setBoard(move.boardState);
    board.draw();
}

Function in Board class that I am using to assign values.

public void setBoard(Board another){
    this.boardFigures = another.boardFigures;
    this.highlightedFields = another.highlightedFields;
    this.hasHighlightedFields = another.hasHighlightedFields;
}

The last part is just a snippet from my Move class(it contains information about moves but i cut it off so the code would look more clean)

public class Move {
private static int globalID=0;
public int ID;
final Board boardState; 

public Move(Board board){
    ID=globalID++;
    boardState = new Board(board);}

What am I doing wrong? If anything else is needed, I'll try to paste it as soon as possible.

thanks,roiek

user3476159
  • 17
  • 1
  • 4
  • Could you ask a specific question rather than posting code snippets. What isn't working?? What error are you getting? Post only relevant code please – smac89 May 19 '15 at 18:38
  • The problem is, in SetBoard function i've checked that move.BoardState is not the same thing as board (via toString) but still i get the same output on my canvas – user3476159 May 19 '15 at 19:00

1 Answers1

0

It's difficult to see what you are tying to do with the limited code, however you mention that you are looking to preserve history but then have this code:

public void setBoard(Board another){
  this.boardFigures = another.boardFigures;
  this.highlightedFields = another.highlightedFields;
  this.hasHighlightedFields = another.hasHighlightedFields;
}

If these are not primitive values, the problem is you're not copying the values of the object, but instead you are storing a reference to that object. If that object changes in the future, the history won't be preserved as you're pointing to the updated object.

If this is the problem you would need to do a real copy of the objects you are trying to preserve. See this example for details about copying objects, and read more about object references here.

Community
  • 1
  • 1
Jim
  • 3,254
  • 1
  • 19
  • 26