Write a program that simulates a game of craps using these rules without human i
ID: 3710593 • Letter: W
Question
Write a program that simulates a game of craps using these rules without human input. Instead of asking for a wage, the program should calculate whether the player would win or lose. The program should simulate rolling two dice and calculate the sum. Add a loop so that the program plays 10,000 games. Add counters that count how many times the player wins and how many times the player loses. At the end of the 10,000 games compute the probability of winning (wins/ (wins + losses)) and output this value
Methods:
Sample Output
Thank you!
Your program should: Declare the variables numOfWins and numOfLoss to keep track of the number of times player wins or loses Add a loop to your program so that the game is played 10,000 times Add another loop so the player can start a new set of games. Declare a constant for the number of games being played. (10,000) Simulate rolling the two dice and calculate the sum. This first roll will establish the comeOutRoll. (use the Random class to generate random numbers for the dice) Pass this first comeOutRoll to a method called winOrLose. This method will return a string. If it returns: o "seven" means a loss. therefore the variable numOfLoss must be incremented in the main method o "the point" means a win. Therefore, the variable numOfWin must be incremented in the main method. o "win": means a win. Then the variable numOfWins must be incremented in the main method. o "loss": means a loss then the variable numOfLoss must be incremented in the main method. o "The Point": means a point has been established. Therefore set the variable thePoint to the comeOutRoll. Then the method keepRolling must be called to roll the dice until a 7 or "the point" is rolled. This method will return a string: At the end of the 10,000 games, call the method winProbability to compute the probability of winning (wins/wins+ losses) Output the value returned Ask the user if she/he wants to start a new game. Output a good by message if the user does not want to play again.Explanation / Answer
//Code to copy
//Game.java
import java.util.Scanner;
public class Game {
public static void main(String[] args) {
//calling methods
description();
play();
}
/**play method*/
private static void play() {
Scanner scan=new Scanner(System.in);
int numOfwin = 0,numOfLoss=0, thePoint=0, comeOutRoll=0;
boolean playagain=true;
String gameStatus="";
String choice="";
final int numOfPlays=10000;
//run the loop until user enter no stop
while(playagain)
{
//run play for 10000 times
for (int i = 0; i < numOfPlays; i++)
{
int die1=(int) (Math.random()*6+1);
int die2=(int) (Math.random()*6+1);
comeOutRoll=die1+die2;
gameStatus=winOrLoss(comeOutRoll);
if(gameStatus.equals("win"))
numOfwin++;
else if(gameStatus.equals("Loss"))
numOfLoss++;
else if(gameStatus.equals("thePoint"))
{
gameStatus=keepRolling(comeOutRoll);
if(gameStatus.equals("win"))
numOfwin++;
else
numOfLoss++;
}
}
System.out.println(" In the simulation we :");
System.out.println(" won "+numOfwin+" times");
System.out.println(" lost "+numOfLoss+" times");
System.out.printf(" for a probability %5.4f ",winProbability(numOfwin, numOfLoss));
numOfwin = 0;
numOfLoss=0;
thePoint=0;
comeOutRoll=0;
System.out.println("Hit enter key to continue");
scan.nextLine();
do {
System.out.println("Would you like to play another game yes/no?");
choice=scan.nextLine();
} while (!choice.equals("yes") && !choice.equals("no"));
if(choice.equals("no"))
playagain=false;
}
System.out.println("Have a nice day. GoodBye");
}
/*Returns win or loss*/
public static String winOrLoss(int comeOutRoll)
{
if(comeOutRoll==7 || comeOutRoll==11)
return "win";
else if(comeOutRoll==2 || comeOutRoll==3 || comeOutRoll==12)
return "Loss";
else
return "thePoint";
}
//Description of program
public static void description()
{
System.out.println("Computer will play a game for you. Here are the rules of the game:");
System.out.println("Two six sided dice is rolled");
System.out.println("Come out roll: The first roll of the dice in a craps round");
System.out.println("A come out roll of 7 or 11 automatically wins");
System.out.println("A come out roll of 2, 3 , or 12 automatically losses");
System.out.println("A come out roll of 4, 5, 6 ,8 , 9, or 10 becomes The Point if the player gest the point he/she will keep playing");
System.out.println("by rolling the dice until he/she gets a 7 or the point.");
System.out.println("If the point is rolled first, then the player wins the bet.");
System.out.println("If a 7 is rolled first, then the player loses.");
}
/*The method keepRolling takes thePoint as integer
* and continues the game until seven or thepoint comes out*/
public static String keepRolling(int thePoint)
{
int sum=0;
int roll1,roll2=0;
roll1 = (int)(Math.random()*5)+1;
roll2 = (int)(Math.random()*5)+1;
sum=roll1+roll2;
while(sum!=thePoint && sum!=7)
{
roll1 = (int)(Math.random()*5)+1;
roll2 = (int)(Math.random()*5)+1;
sum = roll1 + roll2;
}
if(sum==7)
return "seven";
else
return "thePoint";
}
/*Returns the probability of the win*/
public static double winProbability(int wins, int losses)
{
return ((double)wins/(wins+losses));
}
}
Sample Output:
Computer will play a game for you. Here are the rules of the game:
Two six sided dice is rolled
Come out roll: The first roll of the dice in a craps round
A come out roll of 7 or 11 automatically wins
A come out roll of 2, 3 , or 12 automatically losses
A come out roll of 4, 5, 6 ,8 , 9, or 10 becomes The Point if the player gest the point he/she will keep playing
by rolling the dice until he/she gets a 7 or the point.
If the point is rolled first, then the player wins the bet.
If a 7 is rolled first, then the player loses.
In the simulation we :
won 2153 times
lost 7847 times
for a probability 0.2153
Hit enter key to continue
Would you like to play another game yes/no?
dsfdf
Would you like to play another game yes/no?
sdf
Would you like to play another game yes/no?
ds
Would you like to play another game yes/no?
123
Would you like to play another game yes/no?
yes
In the simulation we :
won 2200 times
lost 7800 times
for a probability 0.2200
Hit enter key to continue
yes
Would you like to play another game yes/no?
no
Have a nice day. GoodBye
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.