'variable reassignment issue in java
I have my code behaving the way I'm expecting it to behave except for one last issue. In the while loop within void main() while (gameStatus == Status.CONTINUE) I'm trying to set the variable gameStatus back to its original state of Status gameStatus; I've come to realize that this isn't as simple in Java as in other languages. I've tried several solutions but I'm either having scoping issues or having to introduce more complexity by instantiating a new Craps object.
It's obvious I don't understand the language enough to reason my way on how to just reassign a a variable so any explanations would be appreciated. My goal is just to set the variable gameStatus to its original state to the effect of Status gameStatus; so the code won't jump into the while loop while (gameStatus == Status.CONTINUE) if the gameStatus changes within that loop to either Status.WIN or Status.LOST.
Thanks in advance for your help.
import java.util.EnumSet;
import java.util.Random;
import javax.crypto.spec.GCMParameterSpec;
import javax.swing.JOptionPane;
public class Craps {
// create random number generator for use in method rollDice
private static final Random randomNumbers = new Random();
// enumeration with contants that represent the game status
private enum Status {
CONTINUE, WON, LOST
};
// constants that represent common rolls of the dice
private static final int SNAKE_EYES = 2;
private static final int TREY = 3;
private static final int SEVEN = 7;
private static final int YO_LEVEN = 11;
private static final int BOX_CARS = 12;
private static int bankBalance = 1000; // initial value for balance
// private static Status gameStatus;
// play one game of craps
public static void main(String[] args) {
while (bankBalance > 0)
{
// prompt user for the value of wager
int wager = Integer.parseInt(JOptionPane.showInputDialog("Enter your wager"));
if (bankBalance > 0 && bankBalance >= wager) {
int myPoint = 0; // point if no win or loss on first roll
Status gameStatus; // can contain CONTINUE
int sumOfDice = rollDice(); // first roll of dice
// determines which status and point based on first roll
switch (sumOfDice) {
case SEVEN:
case YO_LEVEN:
gameStatus = Status.WON;
chatter(Status.WON);
setBankBalance(wager, Status.WON); // increase bankBalance equal to the wager amount
break;
case SNAKE_EYES:
case TREY:
case BOX_CARS:
gameStatus = Status.LOST;
chatter(Status.LOST);
setBankBalance(wager, Status.LOST); // decreases bankBalance equal to the wager amount
break;
default:
gameStatus = Status.CONTINUE;
myPoint = sumOfDice;
System.out.printf("Point is %d\n", myPoint);
break;
} // end switch
// while game is not complete
while (gameStatus == Status.CONTINUE) {
sumOfDice = rollDice(); // roll dice
myPoint = sumOfDice;
System.out.printf("Point is %d\n", myPoint);
// determine game status
if (sumOfDice == myPoint) {
gameStatus = Status.WON;
chatter(Status.WON); // / display "won" messages
setBankBalance(wager, Status.WON); // increase bankBalance equal to the wager amount
gameStatus; // set to initial gameStatus state
} else if (sumOfDice == SEVEN) {
gameStatus = Status.LOST;
chatter(Status.LOST); // display "lost" messages:
setBankBalance(wager, Status.LOST); // decreases bankBalance equal to the wager amount
gameStatus; // set to initial gameStatus state
}
} // end while gameStatus
} else {
System.out.println("Sorry, Your balance is not enough to place that wager.");
System.out.println("Enter a wager that's equal to and greater than your bank balance.");
System.out.printf("Your bank balance is %d\n", bankBalance); // display bank balance
} // end if/else bankBalance >= 0 && bankBalance >= wager
} // end main while loop bankBalance > 0
// end game and/or display "No more money" message
System.out.println("Sorry. You don't have any more money to play");
System.out.printf("Your balance is %d\n", bankBalance);
} // end void main ===========================================
// roll dice, calculate sum and display results
public static int rollDice() {
// pick random die values
int die1 = 1 + randomNumbers.nextInt(6); // first die roll
int die2 = 1 + randomNumbers.nextInt(6); // second die roll
int sum = die1 + die2; // sum of die values
// display results of this roll
System.out.printf("Player rolled %d + %d = %d\n", die1, die2, sum);
return sum; // returns sum of dice
} // end method rollDice
public static void setBankBalance(int wager, Enum gameStatus) {
if (gameStatus == Status.WON) {
bankBalance += wager;
System.out.printf("Your balance is %d\n", bankBalance);
} else if (gameStatus == Status.LOST) {
bankBalance -= wager;
System.out.printf("Your balance is %d\n", bankBalance);
}
} // end setBankBalance method
// chatter method
public static void chatter( Enum gameStatus )
{
int chooseMessage = randomNumbers.nextInt(5);
if (gameStatus == Status.WON) {
String[] congratulatoryMessagesWON = {
"Wooohooo You Won!\n",
"You just keep on winning! You're an animal!\n",
"Winning is your middle name!\n",
"If you look up 'winner' in the dictionary, it's your picture on there!\n",
"Can't stop. Won't stop Winning!\n",
};
System.out.printf(congratulatoryMessagesWON[chooseMessage]);
} else if (gameStatus == Status.LOST) {
String[] congratulatoryMessagesLOST = {
"Bummer. You lost. Better luck next time.\n",
"You lost this round. You can't win all the time.\n",
"Losing is part of life. Better luck next time.\n",
"Luck isn't on your side this time. You lost this round.\n",
"Losing sucks but it's part of life. We all lose sometimes.\n",
};
System.out.printf(congratulatoryMessagesLOST[chooseMessage]);
} else if (gameStatus == Status.CONTINUE) {
String[] defaultMessages = {
"Play on!\n",
"Keep making those bets\n",
"Keep those dices rolling!\n",
"You can't win if you don't play! Keep the game going!\n",
"You might get lucky...There's only one way to find out. Keep playing!\n",
};
System.out.printf(defaultMessages[chooseMessage]);
}
} // end chatter method
} // end class Craps
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
