Write a simple pressyourluck dice game The game works as fol

Write a simple press-your-luck dice game. The game works as follows: Player 1 starts with a score ol zero and rolls two dice. If the player rolls 7, 2, or 12 their turn is over and they lose, If the player rolls any other sure, it is added to their total score, The player can stop rolling at any time - remember if their last roll was a 7, 2 or 12 they lose and Player 2 wins. Alter Player 1 stops rolling# Player 2 rolls the two dice Player 2 continues to roll the dice until they roll a 7, 2 or 12 (Player 2 loses) or they have a total higher than Player 1 (Player 2 wins). Your solution should make use of methods with the following headers: public static int rollDie() return a random valid die value between 1 and 6 public static int newScore(int oldScore, int rollValue) determine if the current roll should be added to the score. If the player rolled 7, 2 or 12 return -1 to show that the player just lost. Otherwise return the updated score, public static int whoGoesFirst() randomly return 1 if player 1 goes first or 2 if player 2 goes first public static String winner (int playerNumber) print a message indicating who won the game public static char keepRolling () A method that prompts the player to ask if they want to keep rolling. Return y or n as needed. main () this is your main method that coordinates all of the other methods as necessary. Collect all input and produce all output from this method. Your game should be able to play a complete human vs. human game. No sample output. Be creative (but kind to your users). You should be able to play human vs. computer AND play multiple games without quitting the program AND keep track of how many games each player has won.

Solution

Please follow the code and comments for description :

CODE :

import java.util.Random; // required imports
import java.util.Scanner;

public class pressTheDice { // 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 2 :
Your Initial Score : 0
Rolling the Die..
Your Total Score : 3
Want to Continue (y or n)?
y
Your Initial Score : 3
Your Total Score : 8
Want to Continue (y or n)?
n
Exiting the Turn.!

Player 1 :
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.

 Write a simple press-your-luck dice game. The game works as follows: Player 1 starts with a score ol zero and rolls two dice. If the player rolls 7, 2, or 12 t
 Write a simple press-your-luck dice game. The game works as follows: Player 1 starts with a score ol zero and rolls two dice. If the player rolls 7, 2, or 12 t
 Write a simple press-your-luck dice game. The game works as follows: Player 1 starts with a score ol zero and rolls two dice. If the player rolls 7, 2, or 12 t
 Write a simple press-your-luck dice game. The game works as follows: Player 1 starts with a score ol zero and rolls two dice. If the player rolls 7, 2, or 12 t

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site