Reputation: 1
so Im doing a project of a tic tac toe using artificial intelligence, and I wanted to make an option so you could replay the game to see what happend in the last round, My intentions were to make the replay as its actually playing right now, which means after each move will be shown there wil lbe a delay and then the next one will occur.And instead of working as expected the delay jsut goes all at a time without anything happening until suddenyl the whole game just shows up without beinge displayed as a game but as a finishing screen' anyway here is the code:
replayMode=true;
setStatus("Replay Playing...");
ifButtonsEnabled(true);
initBoard();
int turn=0;
Move current;
for(int i=0;i<replayer.getSizeOfVect();i++)
{
current=replayer.getMoveAt(i);
if(i%2==0)
{
buttons[current.getI()][current.getJ()].setText("X");
}
else
{
buttons[current.getI()][current.getJ()].setText("O");
}
try {
Thread.sleep(650);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
setStatus("Replay ended.");
replayMode=false;
Upvotes: 0
Views: 50
Reputation: 8551
Usually when you see this kind of effect in a GUI program, it's because you need to yield control back to the GUI thread's event loop for any drawing to happen. Rather than Thread.sleep
I recommend using a timer with a 650ms interval and doing one "step" of the animation with each tick of the timer.
Upvotes: 3