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

IN JAVA: Write a program that simulates playing a dice game. The game is played

ID: 3848835 • Letter: I

Question

IN JAVA: Write a program that simulates playing a dice game. The game is played by rolling two dice. Each dice gives a number from 1 to 6. If the dice roll is a double (each dice has the same value), then you win. If the dice are different you lose. A winning roll scores 5 points, a losing roll losing 1 point. There are 10,000 rolls in one game. Find the average point total after playing 100 different games. Points can go negative. Also print out the highest and lowest point totals achieved at any time in any of the 100 games.

Explanation / Answer

public class PairOfDiceGame {
  
   private static int dice1;
   private static int dice2;
  
  
   /* Function for simulating two dice rolls */
   public static boolean roll(){
       dice1= (int)(Math.random()*6)+1;    //Will generate a random number between 1 to 6
       dice2= (int)(Math.random()*6)+1;
      
       if(dice1==dice2)
           return true;
       else
           return false;
   }
  
   /* Function for 10,000 rolls */
   public static int game(){
       int points=0;
       for(int i=0;i<10000;i++){          
           if(roll()==true)
               points+=5;
           else
               points-=1;
       }
       return points;
   }
  
   public static void main(String[] args) {
      
       int total=0;
       int tempTotal=0;           //a temporary value for comparing the maximum and minimum value after each game
       double avg=0.0;               //average for 100 games
       int maxPoint=-999999999;   //for storing the maximum value in 100 games
       int minPoint=99999999;     //for storing the minimum value in 100 games
      
       /*loop for 100 games */
      
       for(int i=0;i<100;i++){

           tempTotal=game();      // call to start the 10,000 rolls for each game
          
           if(tempTotal>maxPoint)
               maxPoint=tempTotal;
           if(tempTotal<minPoint)
               minPoint=tempTotal;
          
           total+=tempTotal;
       }
       avg=(double)total/100;
      
       System.out.println("Average Points after playing 100 different games is: "+avg);
       System.out.println("Highest total points achieved: "+maxPoint);
       System.out.println("Lowest total points achieved: "+minPoint);
      
   }
}