[Java] Pausing a while loop
Posted: Fri 24 Apr, 2009 1:08 am
Okay, I have a game called Pig I've been working on. It's a dice game that has human players and computer players. When a computer player goes, it simply rolls the dice until it is told to stop (like if it loses the round). I want to set a delay for each time the computer rolls the dice so it doesn't simply go 10 times before passing the dice.
I've tried Thread.sleep(delay), but then the GUI doesn't update the information (like the current roll on the dice). I conducted an experiment and found that if I use a JOptionPane to make the pause, the GUI updates perfectly.
I want to make the program produce the same results, but without the JOptionPane being present.
Any ideas?
Oh, I also tried using a Timer, but it produced the same results as the Thread.sleep().
Here's the code for the computer:
I've tried Thread.sleep(delay), but then the GUI doesn't update the information (like the current roll on the dice). I conducted an experiment and found that if I use a JOptionPane to make the pause, the GUI updates perfectly.
I want to make the program produce the same results, but without the JOptionPane being present.
Any ideas?
Oh, I also tried using a Timer, but it produced the same results as the Thread.sleep().
Here's the code for the computer:
Code: Select all
if (comp) {
while (!pass) {
if (computerScore+roundScore>=GOAL) {
status = name+" has won the game!";
update();
end=true;
} else {
if (computerScore+roundScore>=humanScores+TURNOVER) {
status = "The "+name+" has turned over the dice by default.";
update();
JOptionPane.showMessageDialog(null, status);
status = "";
pass=true;
} else {
rollDice();
if (losePoints()) {
computerScore = 0;
roundScore = 0;
pass=true;
} else
if (loseRound()) {
roundScore = 0;
pass=true;
} else
roundScore += points;
//I want to use a Timer here, instead of JOptionPane.
update();
JOptionPane.showMessageDialog(null, name+" has rolled: "+toString());
System.out.println(toString());
}
}
}
}