Professional Documents
Culture Documents
*;
import java.util.Arrays;
import java.util.Random;
int playAgain = 0;
//Loop while confirmation is 0
while (playAgain == 0) {
//Simulate the game
simulate();
//Prompt the user to play again,
//if yes, the value returned is 0 otherwise 1 ending the loop
playAgain = JOptionPane.showConfirmDialog(
null,
"Play Again?",
"Confirm to Play Again.",
JOptionPane.YES_NO_OPTION);
}
}
//Loop until the attempt reaches 0 or the bank amount money is equal or
less than zero
for (int round = 1; round <= 5; round++) {
String roundStatus;
int currentWinnings;
gameResult = gameResult
+ "Round " + round + ": "
+ "Bet Amount: £" + bet
+ ", Dices: " + dice1 + "-" + dice2
+ ", Amount Won/Lose: £" + currentWinnings
+ "\n";
//Decrement attempt by 1
attempts--;
while (true) {
//Check if player name is not blank, if true return the player name
if (!playerName.equals("")) {
return playerName;
} else {
showErrorMessageDialogBox(
"Invalid input for name. Try again.",
"Invalid input");
}
}
}
int bet = 0;
while (true) {
//Implement try catch to capture bad input from user
try {
//Display attempts remaining and get the bet from the user
String input = JOptionPane.showInputDialog(
"Current Money: £" + money + "\n" +
"Attempts Remaining: " + attempts + "\n" +
" Enter your bet (1 - 4 only)");
//Convert the input to integer
bet = Integer.parseInt(input);
//Determine if bet is between 1 and 4 then proceed,
//otherwise throw exception
if (bet < 1 || bet > 4) {
showErrorMessageDialogBox(
"Invalid bet. Must be £1 - £4 only",
"Invalid input");
} else if (bet > money) {
showErrorMessageDialogBox(
"Invalid bet. Cannot be greater than current money",
"Invalid input");
} else {
return bet;
}
} catch (NumberFormatException e) {
showErrorMessageDialogBox(
"Invalid value for bet. Try again.",
"Invalid input");
}
}
}
//Get the current score from the value of position - 1 (since index
starts from 0);
Score current = scoresArray[position - 1];
scoreResult = scoreResult + "\t"
+ "No. " + position
+ ": " + current.toString() + "\n";
}