In class we studied some source code that used random number generation to help play the dice game known as craps. The rules of the basic game we covered in class and may be summarized as follows. The first roll of the dice ends in a win or a loss or a requirement to continue to throw the dice. If the first roll is a 7 or 11 it is a win. If the first roll is a 2, 3, or 12 it is a loss. For any other total (the point as it is called) you must rethrow the dice until you either repeat the point or throw a 7. If a rethrow generates a 7 it is a loss. If a rethrow produces the point it is a win. If neither occurs you must continue to rethrow. The source code is visible on the course web site in the Examples section. The program is titled CrapsGame.java. For this question you are to use the basic game playing code to run a simulation of 10 million games and gather some statistics from the simulation and then display the results.
The statistical information to determine and then to print out is:
- Average # of rolls per game
- The maximum number of rolls used in a single game
- The number of games that needed more than 30 rolls
- The number of wins
- The number of losses
- The probability of a win (displayed to 4 decimal places)
(THIS IS THE QUESTION)
public class CrapsGame{
public static void main(String[] args) {
int time; //number of times the game user want to play
int rolls=0; //number of rolls
int mypoint=0;
int d1; //number user rolled for first dice
int d2; //number user rolled for second dice
int total=0; //total number of rolls
int Numwins=0; //number of wins
int Numlosses = 0; //number of losses
double averolls=0; //average number of rolls per game
double probwin=0; //probability of win
boolean win, rollagain=false;
win = false;
System.out.println("Enter the number of times the game you want to play: ");
time=TextIO.getlnInt();
rolls=0;
for (int i=0; i<time; i++){
d1=(int)(6*Math.random())+1; //user roll dice(generate a random number)
d2=(int)(6*Math.random())+1;
System.out.printf("%d and %d\n",d1,d2);
rolls++;
total = total + rolls;
switch (d1+d2) {
case 7:
case 11:
win=true; //when first roll is 7 or 11, player win
Numwins++;
break;
case 2:
case 3:
case 12:
win=false; //when first roll is 2,3 or 12, player lose
Numlosses++;
break;
default:
mypoint = d1 + d2;
rollagain=true; //when first roll is 1,4,5,6,8,9,10 or 11, player rethrow dice
}
while(rollagain==true){
d1=(int)(6*Math.random())+1;
d2=(int)(6*Math.random())+1;
System.out.printf("%d and %d\n",d1,d2);
if (d1+d2 == mypoint ){
win=true;
rollagain=false;
}
else if ( d1+d2 == 7){
win=false;
rollagain=false;
}
}
if (win){
System.out.printf("***WINNER***\n");
}
else{
System.out.printf("YOU LOSE\n");
}
}
probwin = Numwins / time;
averolls = total / time ;
System.out.printf("Avg # rolls: %.2f\n",averolls);
System.out.printf("Max # rolls: %d\n",rolls);
System.out.printf("# of wins: %d\n",Numwins);
System.out.printf("# of losses: %d\n",Numlosses);
System.out.printf("The probability of a win: %.2f.\n",probwin);
}
}
Here are the questions sirs! my program can run but the calculation part, every single calculation part ex. average rolls per game, number of wins and losses , etc. , doesnt work well. it always gives me wrong numbers. please help me.