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

In this project, you will write a “craps” dice program in Java. First, you will

ID: 3667116 • Letter: I

Question

In this project, you will write a “craps” dice program in Java. First, you will need to know the rules of the game:


-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.
-If the sum of the dice are 7 or 11, the player wins.
-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 sum of seven is rolled (in which case the player loses).
-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.

In addition to the program, create a UML diagram for you Die class (as well as another classes you create). Provide this as a pdf.

Hints:

Random Number Generator:

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

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

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

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

Dice.java(Class representing a dice)

import java.util.Random;;
public class Dice {

   //Value on dice
   private int value;
   //number of times a particular dice is rolled
   private int numberOfRolls;
   //total rolls of all dices
   static int totalRolls=0;
  
  
   public int getNumberOfRolls() {
       return numberOfRolls;
   }

   public int getValue() {
       return value;
   }

   public void setValue(int value) {
       this.value = value;
   }
  
   public void roll()
   {
       Random r =new Random();
       this.value=r.nextInt(6)+1;
       numberOfRolls++;
       totalRolls++;
   }
  
   public Dice()
   {
       numberOfRolls=0;
   }
  
}

Game.java(Class for simulating the game)

import java.io.*;
public class Game {

   public static void main(String[] args)throws IOException {
      
       InputStreamReader ir=new InputStreamReader(System.in);
       BufferedReader br=new BufferedReader(ir);
      
       System.out.println("Enter your name");
       String name=br.readLine();
      
       Dice dice1=new Dice();
       Dice dice2=new Dice();
      
       //Rolling the dices
       dice1.roll();
       dice2.roll();
          
       int sum=dice1.getValue()+dice2.getValue();
       System.out.println("Value of dice 1 "+dice1.getValue());
       System.out.println("Value of dice 2 "+dice2.getValue());
      
       if(sum==2||sum==3||sum==12)
       {
           System.out.println(name+" You have lost as your score is "+sum);
       }
       else if(sum==7||sum==11)
       {
           System.out.println(name+" You have won as your score is "+sum);
       }
       else
       {
           System.out.println(name+" You must continue rolling as your score is "+sum);
           int oldsum=sum;
           while(true)
           {
               dice1.roll();
               dice2.roll();
              
               System.out.println("Value of dice 1 "+dice1.getValue());
               System.out.println("Value of dice 2 "+dice2.getValue());
              
               sum=dice1.getValue()+dice2.getValue();
              
               if(sum==7)
               {
                   System.out.println(name+" You have lost as your score is "+sum);
                   break;
               }
               else if(sum==oldsum)
               {
                   System.out.println(name+" You have won as your score is "+sum);
                   break;
               }
               System.out.println(name+" You must continue rolling as your score is "+sum);
           }
       }
   }
}
      

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