0
@FXML AnchorPane gamePane;

public void gameStart() {
    if(!Started) {
        board = new Board();
        stones = new Circle[8][8];
        newTurn();
        applyBoard();
        Started = true;
    }
    else {
        DestroyBoard(); // <--- Erase all the stones 
        board = new Board();
        stones = new Circle[8][8];
        newTurn();
        applyBoard();
    }
}

public void applyBoard() { 
    for(int i = 0; i < board.boardsize; i++) {
        for(int j = 0; j < board.boardsize; j++) {
            if(board.board[i][j] != board.EMPTY) {
                if(board.board[i][j] == board.BLACK) {
                    stones[i][j] = new Circle(155 + 90 * j, 85 + 90 * i, 40);
                    stones[i][j].setFill(Color.BLACK);
                    gamePane.getChildren().add(stones[i][j]);
                }
                else if(board.board[i][j] == board.WHITE) {
                    stones[i][j] = new Circle(155 + 90 * j, 85 + 90 * i, 40);
                    stones[i][j].setFill(Color.WHITE);
                    gamePane.getChildren().add(stones[i][j]);
                }
            }
        }
    }
}
public void DestroyBoard() { // <---Test Function and not worked!!
    gamePane.getChildren().remove(stones[3][3]);
}

I Tried to make if press start button again then all stones on board erased and start a new game. As a first step I tried to erase one basic stone, but I can't delete any of stone on the board. What should I do to solve that?

SunBathe
  • 119
  • 2
  • 3
  • 9
  • Possible duplicate of [JavaFX - How to delete a specific Node from an AnchorPane](https://stackoverflow.com/questions/30018663/javafx-how-to-delete-a-specific-node-from-an-anchorpane) – Pospolita Nikita Jun 25 '18 at 16:13
  • I'm pretty sure it does work, but since you create a new `Board` and add the circles based on the initial data in this new instance, you get a board with twice as many circles minus the one you removed. I guess the code is deterministic so the positions and look of the circles remains the same as in the previous displayed version the result looks exactly the same as before... – fabian Jun 25 '18 at 16:22
  • if I do gamepane.getChildren().clear at applyboard() function it works but anywhere except that, not working – SunBathe Jun 26 '18 at 15:03

1 Answers1

3

The stones are stored in an ObservableList within the gamePane container, which you access with the getChildren() method. The list has a very helpful clear() method that removes all items in the list.

So if you are just looking to remove all the stones from gamePane, just call this method:

gamePane.getChildren().clear();
Zephyr
  • 9,885
  • 4
  • 28
  • 63
  • thank for commenting. if i call getChildren.clear() at appyboard() function, it worked but if i call that at destryboard() function, stones are still alive! what mistake did i do? – SunBathe Jun 26 '18 at 02:00
  • Do you call applyBoard() again right after DestroyBoard()? – Zephyr Jun 26 '18 at 02:22
  • Well, if you're deleting all the stones but adding them all back again right away, you won't notice them being removed. – Zephyr Jun 26 '18 at 14:53
  • that'r right so as a test case I tried to only remove a (3,3) located stone and also thats failed. I only can delete stones at applyboard() function which add stones at gamepane.getChildren() . without that function, no way to access stones – SunBathe Jun 26 '18 at 16:36