Hi Everybody.
So, I came back here, as promised.
I have solved my problem.
But in solving this problem, that has created even more questions that I would like someone in this Forum to answer please.
First of all the explanation of the solution.
This is an Oversimplification.
But it's a necessary Oversimplification.
So, in my Tic-Tac-Toe Program, there is a While Loop.
Iniside that while loop:
proceedGame is a boolean
public void start(Stage primaryStage) {
while(proceedGame) {
Player1 makes its move
Player2 makes its move
}
}
Instead of using the Stage primaryStage of the Application.start() method, I've created a second Stage.
I've named this stage boardStage
Stage boardStage = new Stage();
boardStage.setTitle("Tic-tac-toe board");
boardStage.setScene(scene);
After the player1 makes its move, I update the ImageViewer using the char[][] board (X or O or E (Empty) ).
I update the GridPane with that ImageViewer.
Instead of using the method boardStage.show() I use the method showAndWait()
boardStage.showAndWait();
Next the player2 makes its move.
But if I place boardStage.showAndWait() in there I get an Exception, because the Stage was already visible before.
So I have to change the method to this;
@Override
public void start(Stage primaryStage) {
while(proceedGame) {
Player1 makes its move
boardStage.showAndWait();
boardStage.hide();
Player2 makes its move
boardStage.showAndWait();
boardStage.hide();
}
}
Now the Board is presented correctly during the game, but it does not Show after the game ends.
There are two possible scenarios for the Tic-Tac-Toe game to End.
And these two possible scenarios are also the exits for the program to end.
Scenario 1:
Victory (One Player Wins)
Scenario 2:
No Victory and the Game Ends
There is no Victory.
All the board positions have been filled with an X or an O
In this case the game ends with a Draw
So, in all of these two scenarios, I added
boardStage.show();
Now after the program ends the Stage of the Board is presented.
So this was the explanation of how I solved this problem.
Thank you,
Rogério
--- Update ---
Hello, again.
The first part of the explanation was getting quite long.
By solving this problem, that has created even more questions that I would like someone in this Forum to answer please.
Why is it that in the Stage primaryStage, I cannot use primaryStage.showAndWait() ?
When I have the following code, which Stage is the primary Stage and which one is the secondary (because the Stage primaryStage is not being used) ?
@Override
public void start(Stage primaryStage) {
Stage boardStage = new Stage();
boardStage.setTitle("Tic-tac-toe board");
boardStage.setScene(scene);
while(proceedGame) {
Player1 makes its move
boardStage.showAndWait();
boardStage.hide();
Player2 makes its move
boardStage.showAndWait();
boardStage.hide();
}
}
In the code above, when the program runs the Stage primaryStage is created with init and start and then the primaryStage creates the secondary stage ?
Thank you,
Rogério