Write a simple pressyourluck dice game The game works as fol
Solution
Please follow the code and comments for description :
CODE :
import java.util.Random; // required imports
import java.util.Scanner;
public class pressYourLuckDice { // class to run the code
public static int rollDie() { // method used to run the dice
Random r = new Random();
int High = 7;
int Low = 1;
int value = r.nextInt(High - Low) + Low; // getting the value based on the higher and the lower values
return value;
}
public static int newScore(int oldScore, int rollValue) { // method that returns the new score value based on the conditions
if (rollValue == 2 || rollValue == 7 || rollValue == 12) {
return -1;
} else {
return (oldScore + rollValue); // return the value
}
}
@SuppressWarnings(\"empty-statement\")
public static int whoGoesFirst() { // check if who is to start the game
Random rand = new Random();
int High = 3;
int Low = 1;
int firstPlay = rand.nextInt(High - Low) + Low; // rnadomly generating the number
return firstPlay; // return the value
}
public static String winner(int playerNumber) { // method to return the string for the result winner
return \"Player \" + playerNumber + \" You Won.!\";
}
public static char keepRolling() { // method to check if the rolling is needed or not
Scanner sc = new Scanner(System.in); // scanner class to get the data from the user
System.out.println(\"Want to Continue (y or n)?\");
char inp = sc.next().toLowerCase().charAt(0); // get the data
return inp; // return the value
}
public static int player1() { // method regarding the player 1
int score = 0, rollValue = 0, newScoreVal = 0, totalScore = 0, rollValue1 = 0, rollValue2 = 0; // required initialisations
System.out.println(\"\ Player 1 : \"); // prompt about the data
System.out.println(\"Your Initial Score : \" + score);
System.out.println(\"Rolling the Die..\");
rollValue1 = rollDie(); // rolling the die
rollValue2 = rollDie();
rollValue = rollValue1 + rollValue2; // getting the total value
newScoreVal = newScore(score, rollValue); // check for the value
if (newScoreVal == -1) { // message if the value is negative
System.out.println(\"Your Turn is Over.\ Sorry. You Lost.!\");
totalScore = 0;
} else {
totalScore = newScoreVal;
System.out.println(\"Your Total Score : \" + totalScore);
char c = keepRolling(); // method to check if rolling needed again
while (c == \'y\') {
System.out.println(\"Your Initial Score : \" + totalScore);
rollValue1 = rollDie();
rollValue2 = rollDie();
rollValue = rollValue1 + rollValue2;
newScoreVal = newScore(score, rollValue);
if (newScoreVal == -1) {
System.out.println(\"Your Turn is Over.\ Sorry. You Lost.!\");
totalScore = 0;
} else {
totalScore = totalScore + newScoreVal;
System.out.println(\"Your Total Score : \" + totalScore);
}
c = keepRolling(); // method to check if rolling needed again
}
System.out.println(\"Exiting the Turn.!\");
}
return totalScore;
}
public static int player2() { // method regarding the player 2
int score1 = 0, rollValue1 = 0, newScoreVal1 = 0, totalScore1 = 0, rollValue21 = 0, rollValue22 = 0; // required initialisations
System.out.println(\"\ Player 2 : \"); // prompt about the data
System.out.println(\"Your Initial Score : \" + score1);
System.out.println(\"Rolling the Die..\");
rollValue21 = rollDie();
rollValue22 = rollDie();
rollValue1 = rollValue21 + rollValue22; // getting the total value
newScoreVal1 = newScore(score1, rollValue1);// check for the value
if (newScoreVal1 == -1) { // message if the value is negative
System.out.println(\"Your Turn is Over.\ Sorry. You Lost.!\");
totalScore1 = 0;
} else {
totalScore1 = newScoreVal1;
System.out.println(\"Your Total Score : \" + totalScore1);
char c = keepRolling(); // method to check if rolling needed again
while (c == \'y\') {
System.out.println(\"Your Initial Score : \" + totalScore1);
rollValue21 = rollDie();
rollValue22 = rollDie();
rollValue1 = rollValue21 + rollValue22;
newScoreVal1 = newScore(score1, rollValue1);
if (newScoreVal1 == -1) {
System.out.println(\"Your Turn is Over.\ Sorry. You Lost.!\");
totalScore1 = 0;
} else {
totalScore1 = totalScore1 + newScoreVal1;
System.out.println(\"Your Total Score : \" + totalScore1);
}
c = keepRolling(); // method to check if rolling needed again
}
System.out.println(\"Exiting the Turn.!\");
}
return totalScore1;
}
public static void main(String[] args) { // driver method
System.out.println(\"Welcome to the Dice Game.!\"); // prompt about the data
System.out.println(\"\ Lets Check Who has to Start...\");
int play = whoGoesFirst(); // get the value
int score1, score2; // local variables
if (play == 1) { // check for the data and start the game
score1 = player1();
score2 = player2();
if (score1 > score2) { // check for the total score
System.out.println(winner(1));
} else {
System.out.println(winner(2));
}
} else if (play == 2) {
score2 = player2();
score1 = player1();
if (score1 > score2) {
System.out.println(winner(1)); // print the winner
} else {
System.out.println(winner(2));
}
}
}
}
OUTPUT :
Welcome to the Dice Game.!
Lets Check Who has to Start...
Player 1 :
Your Initial Score : 0
Rolling the Die..
Your Total Score : 4
Want to Continue (y or n)?
n
Exiting the Turn.!
Player 2 :
Your Initial Score : 0
Rolling the Die..
Your Total Score : 5
Want to Continue (y or n)?
n
Exiting the Turn.!
Player 2 You Won.!
Hope this is helpful.



