Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Need help writing a Craps program using DrJava. The instruction/requirements are

ID: 3666068 • Letter: N

Question

Need help writing a Craps program using DrJava. The instruction/requirements are below:

First, you will need to know the rules of the game:

1. The player roles two dice, with a resulting score between 2-12. On this initial role, if the score is 2, 3, or 12, the player loses.

2. If the sum of the dice are 7 or 11, the player wins.

3. For any other score (4, 5, 6, 8, 9, 10), the number is remembered and the player must continue rolling the dice until either

a. A sum of seven is rolled (in which case the player loses).

b. The initial number is achieved (in which case the player wins)

__________________________________________________________________________________________________________

Your program should do the following:

Prompt the user to enter their name – this name should be used in messages to the player

Simulate the initial rolling the dice by use of a random number generator (see hints below), and determine if the player wins or loses or needs to continue rolling. Put out an appropriate message that includes the players name, the dice values and total, and win/loss/continue rolling status.

Continue to simulate rolling until the player wins or loses. For each of these rolls, put out an appropriate status message (with the players name, dice values and total and win/loss/continue rolling status).

Use objects to represent the Dice. The Die value must be kept as a private field, and you need to use an Accessor method to get its value. You can have a method to set it (optional), but you must have a public roll method to set its value to a random value.

Keep track of the number of rolls of each die AND overall for all of the Dice. This must be kept within the Die class.

__________________________________________________________________________________________________________

Additonal requirements:

b) Use dialog boxes for all input and the final win/lose output.

c) Program prints out the average sum of the dice value.

d) Program randomly puts out one of the following messages when the player wins:

a. Hey, (player name here) you’re a winner!

b. Way to go (player name here), you’ve won!

c. Yeah (player name here), whatever, so you’ve won.

And randomly puts out one of the following messages when the player loses:

d. (player name here) LOSER!

e. Sorry (player name here), you’ve lost.

f. Well (player name here), sometimes you win and sometimes you lose. I win and you LOST!

e) Program keeps track of the values rolled for each Die, within the Die object (i.e. as an instance variable). Your program should make use of an Arraylist (and use a wrapper class) to hold the variable number of roll values for the Die, and should provide a replay method that allows a caller to get each of the Die’s values, e.g. mydie.replay(1) would get the first value that the Die had. (You can make this zero based if you desire, i.e. mydie.replay(0) would get the first value.) Your program should make use of this to “play back” the Die values at the end of the game.

f) Programs allows a “simulation mode”. When specified, your program should prompt for the number of iterations and then simulate the playing of the game for that number of iterations. The simulation should keep track of (and output when complete) the number of wins, number of losses, longest winning streak, longest losing streak.

g) Create a UML diagram for your Die class (as well as another classes you create)

__________________________________________________________________________________________________________

Hints

Random Number Generator:

o A Random number generator class can be included in your program by the java.util.Random package.

o To create a random number generator object, you instantiate one via a statement such as:

Random generator = new Random(); // use the random # generator class

o To obtain a random number, you can use the nextInt() method in the Random class to get a number. nextInt() expects to know the range of numbers, between 0-n. For example, the following call obtains integers between 1 and 10:

int die1 = generator.nextInt(10) + 1;// get a random # between 1 and 10

Explanation / Answer

Answer

/*

* To change this template, choose Tools | Templates

* and open the template in the editor.

*/

package diceapplication;

/**

*

* @author HARE KRISHNA

*/

class Dicegame {

    /**

     * @param args the command line arguments

     */

    public static void main(String[] args) {

        // TODO code application logic here

        String Play;

            boolean playerTurn = true;

            final int WIN = 100;

            final int COMPMAX = 20;

            int play, playerScore = 0, compScore = 0, compEnd, points;

            int die1Roll, die2Roll;

        int dice1;

        int dice2;

        int count = 0;

        int theSum = 0;

        int lowest = Integer.MAX_VALUE;

        int finalSum = 0;

        int diceSum;

        int totalSum=0;

        while (count < Integer.parseInt(args[0])) {

            count = count + 1;

            diceSum=0;

            theSum=0;

            while (diceSum!=7) {

                diceSum = 0;

                dice1 = 1 + (int) ((Math.random() * (6 - 1)) + 1);

                dice2 = 1 + (int) ((Math.random() * (6 - 1)) + 1);

                diceSum = dice1 + dice2;

                if (diceSum != 7) {

                    theSum = theSum + diceSum;

                }

                //System.out.println("the sum is "+theSum);

            }

            if (theSum > finalSum) {

                finalSum = theSum;

            }

            if (theSum < lowest) {

                lowest = theSum;

            }

            totalSum=totalSum+theSum;

        }

        double average=(double)totalSum/(Double.parseDouble(args[0]));

        System.out.println("After " + args[0] + " simulations: ");

        System.out.println("Biggest sum: " + finalSum);

        System.out.println("Smallest sum: " + lowest);

        System.out.println("The average is: "+average);

            Die die1 = new Die();

            Die die2 = new Die();

//-------------------------------------------------------------------------------------------------

//Main method creates object of class and calls the play method to initiate gameplay.

//-------------------------------------------------------------------------------------------------

            public static void main (String[] args)

            {

                        JOptionPane.showMessageDialog(null, "Welcome to M@'s dig game.");

                        Dicegame dig = new Dicegame();

                        dig.play();

            }

//---------------------------------------------------------------------------------

//Loops game and controls who has dice.

//---------------------------------------------------------------------------------

            public void play()

            {

                        while(playerTurn)

                        {

                        Play = JOptionPane.showInputDialog("Please enter 1 to roll the die or 2 to let the computer roll the die.");

                                    play = Integer.parseInt (Play);

                                    if (play == 1)

                                    {

                                                playerTurn = true;

                                    }

                                    else

                                    {

                                                playerTurn = false;

                                    }

                                    if (playerTurn == false)

                                    {

                                                computerRoll();

                                    }

                                    else

                                    {

                                                playerRoll();

                                    }

                        }

            }

//---------------------------------------------------------------------------------

//Rolls for the computer and gives corresponding points.

//---------------------------------------------------------------------------------

public void computerRoll()

{

                                                while (playerTurn == false)

                                {

                                                            die1Roll = die1.roll();

                                                            die2Roll = die2.roll();

                                                            JOptionPane.showMessageDialog(null, "Computer rolled: " + die1Roll + " + " + die2Roll);

                                                            compScore = points(die1Roll, die2Roll, compScore);

                                                            if (die1Roll == 1 || die2Roll == 1)

                                                            {

                                                                        playerTurn = true;

                                                            }

                                                            JOptionPane.showMessageDialog(null, "Player Score: " + playerScore + "   " + "Computer Score: " + compScore);

                                                            if (compEnd >= COMPMAX)

                                                            {

                                                                        playerTurn = true;

                                                                        compEnd = 0;

                                                            }

                                                }

                                                if (compScore >= WIN)

                                                {

                                                            win();

                                                }

}

//---------------------------------------------------------------------------------

//Rolls for the player and gives corresponding points.

//---------------------------------------------------------------------------------

public void playerRoll()

{

                                                die1Roll = die1.roll();

                                                die2Roll = die2.roll();

                                                JOptionPane.showMessageDialog(null, "You rolled: " + die1Roll + " + " + die2Roll);

                                                playerScore = points(die1Roll, die2Roll, playerScore);

                                                JOptionPane.showMessageDialog(null, "Player Score: " + playerScore + "   " + "Computer Score: " + compScore);

                                                if (die1Roll == 1 || die2Roll == 1)

                                                {

                                                            playerTurn = false;

                                                            computerRoll();

                                                }

                                                if (playerScore >= WIN)

                                                {

                                                            win();

                                                }

}

//------------------------------------------------------------------------------------

//Calculates points for players and checks to see they roll 1 or double 1s.

//------------------------------------------------------------------------------------

            public int points(int die1Roll, int die2Roll, int points)

            {

                        if (die1Roll == 1 && die2Roll == 1)

                        {

                                    JOptionPane.showMessageDialog(null, "Lost all points!");

                                    points = 0;

                                    compEnd = 0;

                                    return points;

                        }

                        else if (die1Roll == 1 || die2Roll == 1)

                        {

                                    JOptionPane.showMessageDialog(null, "Received no points!");

                                    if (points >= die1Roll + die2Roll)

                                    {

                                                points -= die1Roll + die2Roll;

                                    }

                                    else

                                    {

                                                points = 0;

                                    }

                                    return points;

                        }

                        else

                        {

                                    compEnd += die1Roll + die2Roll;

                                    points += die1Roll + die2Roll;

                                    return points;

                        }

            }

//---------------------------------------------------------------------------------

//Tests whether or not player or computer won.

//---------------------------------------------------------------------------------

            public void win()

            {

                        if (playerScore >= 100)

                        {

                                    JOptionPane.showMessageDialog(null, "Congratulations! You Win!");

                                    System.exit(0);

                        }

                        if (compScore >= 100)

                        {

                                    JOptionPane.showMessageDialog(null, "You lose!");

                                    System.exit(0);

                        }

            }

    }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote