So I have my program using JButtons with actionListeners attached, but when the program hits a certain point where it should wait for an action to be performed, it skips right over the waiting, and just continues on. I am wondering if it is due to there being no IOException being thrown, but if I throw it, it just returns a bunch of errors, which creates a huge mess.
And yes, I know that I am mixing command line with swing. That is because it started out as command line, and now I am making it swing.
Can anyone point out an easier way to fix this?
Here is my code:
public static void choiceRerollDice() {
if (!canRerollDiceOne && !canRerollDiceTwo && !canRerollDiceThree && !canRerollDiceFour && !canRerollDiceFive) {
System.out.println("Sorry, but you may not reroll any more dice...");
displayDiceValues();
System.exit(0);
}
else {
System.out.println("Would you like to reroll any (more) dice? (yes/no)");
area = "choiceReroll";
}
}
public static void rerollChoice(String choiceReroll) {
switch (choiceReroll) {
case "yes":
rerollDice();
break;
case "no":
//endTurn();
displayDiceValues();
f.validate();
f.repaint();
//calculatePlayer1Score();
//System.out.println("Thank you for playing!");
//System.out.println("Goodbye!");
System.exit(0);
break;
default:
invalidInput();
}
}
public static void rerollDice() {
Scanner is = new Scanner(System.in);
System.out.println("Which dice would you like to reroll? (Click the box under the dice!)");
rollSel = is.next();
switch (rollSel) {
case "roll":
if (willRerollDiceOne) {
if (canRerollDiceOne) {
diceOne = 0;
rollDiceOne();
canRerollDiceOne = false;
box1.setEnabled(false);
}
else {
System.out.println("error");
}
}
else {
}
if (willRerollDiceTwo) {
if (canRerollDiceTwo) {
diceTwo = 0;
rollDiceTwo();
canRerollDiceTwo = false;
box2.setEnabled(false);
}
else {
System.out.println("error");
}
}
else {
}
if (willRerollDiceThree) {
if (canRerollDiceThree) {
diceThree = 0;
rollDiceThree();
canRerollDiceThree = false;
box3.setEnabled(false);
}
}
else {
}
if (willRerollDiceFour) {
if (canRerollDiceFour) {
diceFour = 0;
rollDiceFour();
canRerollDiceFour = false;
box4.setEnabled(false);
}
}
else {
}
if (willRerollDiceFive) {
if (canRerollDiceFive) {
diceFive = 0;
rollDiceFive();
canRerollDiceFive = false;
box5.setEnabled(false);
}
}
else {
}
box1.setSelected(false);
box2.setSelected(false);
box3.setSelected(false);
box4.setSelected(false);
box5.setSelected(false);
f.validate();
f.repaint();
break;
default:
System.out.println("Error...");
break;
}
choiceRerollDice();
}
Here are the JButtons:
public static JButton textYes = new JButton("Yes");
public static JButton textNo = new JButton("No");
And for the actionListeners:
textYes.addActionListener(this);
textNo.addActionListener(this);
And the actionPerformed():
if ("choiceReroll".equals(area)) {
if(e.getSource() == textYes){
rerollChoice("yes");
}
if(e.getSource() == textNo){
rerollChoice("no");
}
}
But instead of stopping to wait for input at:
else {
System.out.println("Would you like to reroll any (more) dice? (yes/no)");
area = "choiceReroll";
}
It just continues on to rerollDice()
Any ideas?
General Suggestions:
while (true)
loops, at present you don't need to use background threads. I would get rid of it and only use it if needed.Specific Suggestions:
inside of actionPerformed:
You'll see that rerollChoice("yes") is called when the textYes button is pressed.
Continue sprinkling your code with println's to see what I mean. More specific recs may be forthcoming. Or better -- learn to use and then use a debugger.
Edit
For example, here is a slightly overly long sample program that shows some of what I mean. Note that it is composed of several classes and enums, the latter to hold "state" values of objects.
OK, but seriously, so far this is the best damn dice game program that I've yet written. Ha!
This displays as:
First Roll:
Second Roll:
Reset: