Problem : In the game of craps, a pass line bet proceeds as follows. Two six-sid
ID: 3714012 • Letter: P
Question
Problem: In the game of craps, a pass line bet proceeds as follows. Two six-sided die are rolled: the first roll of the dice in a craps round is called the “come out roll.” A come out roll of 7 or 11 automatically wins, and a come out roll of 2, 3, or 12 automatically loses. If 4, 5, 6, 8, 9, or 10 is rolled on the come out roll that number becomes “the point.” The player keeps rolling the dice until either 7 or the point is rolled. If the point is rolled first, then the player wins the bet. If a 7 is rolled first, then the player loses.
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.
Note: to simulate the dice rolling use the Random class to generate numbers between 1 and 6 inclusive.
Here is an example series:
The shooter throws the dice on a come out roll, which starts a new series, and a 5 is rolled. (Recall that rolling a 7 or 11 would have been winners and 2, 3, or 12 would have been losers.)
The shooter has established a point of 5.
The shooter throws the dice again and rolls an 8.
Nothing happens and player rolls the dice again.
The shooter throws the dice again and rolls a 3. (Note that rolling a 2, 3, 11, or 12 after a point is established means nothing)
The shooter throws the dice again and rolls a 5.
This is the shooter's point so this is a win and the series ends.
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:
“seven” means a loss. therefore the variable numOfLoss must be incremented in the main method
“the point” means a win. Therefore, the variable numOfWin must be incremented in the main method.
“win”: means a win. Then the variable numOfWins must be incremented in the main method.
“loss”: means a loss then the variable numOfLoss must be incremented in the main method.
“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.
List of the methods for the Craps game
1. main method: Has only one line of code which is a call to the method play
2. play method
winOrLoss method: in this method you can only have one return statement.
description method: outputs the description of the game
keepRolling method : you must only use one return statement.
winProability method :
Sample Output
Explanation / Answer
Completed the code to simulate game of craps as you mentioned. Everything is working perfectly and Javadoc comments are included everywhere the important statements are written. Thanks
// CrapsSimulation.java
import java.util.Random;
import java.util.Scanner;
public class CrapsSimulation {
/**
* required variables
*/
static int numOfWin, numOfLoss, thePoint, comeOutRoll, numOfPlays;
static Random random = new Random();
static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
// displaying the description
description();
// playing the simulation
play();
}
/**
* method to play the simulation
*/
public static void play() {
numOfWin = 0;
numOfLoss = 0;
thePoint = 0;
comeOutRoll = 0;
numOfPlays = 0;
boolean playAgain = true;
while (playAgain) {
for (int i = 0; i < 10000; i++) {
/**
* Rolling two values between 1-6
*/
int diceValue1 = random.nextInt(6) + 1;
int diceValue2 = random.nextInt(6) + 1;
// summing up
comeOutRoll = diceValue1 + diceValue2;
// checking for win or loss or thePoint
String winOrLoss = winOrLoss(comeOutRoll);
if (winOrLoss.equalsIgnoreCase("win")) {
// win
numOfWin++;
} else if (winOrLoss.equalsIgnoreCase("loss")) {
// loss
numOfLoss++;
} else {
// thePoint
thePoint = comeOutRoll;
// rolling again and again until seven or thePoint
winOrLoss = keepRolling(thePoint, random);
if (winOrLoss.equalsIgnoreCase("seven")) {
// win
numOfLoss++;
} else {
// lost
numOfWin++;
}
}
}
/**
* Calculating the probability
*/
double prob = winProability(numOfWin, numOfLoss);
/**
* Displaying the stats
*/
System.out.println("In the simulation we :");
System.out.println(" won " + numOfWin + " times");
System.out.println(" lost " + numOfLoss + " times");
System.out.println(" for a probability of " + prob);
/**
* resetting counters
*/
numOfWin = 0;
numOfLoss = 0;
thePoint = 0;
comeOutRoll = 0;
numOfPlays = 0;
String choice = "";
/**
* prompting user to choose either to play again or not
* (continuously)
*/
while (!choice.equalsIgnoreCase("yes")
&& !choice.equalsIgnoreCase("no")) {
System.out
.print("Would you like to play another game yes/no? ");
choice = scanner.nextLine();
if (choice.equalsIgnoreCase("yes")) {
playAgain = true;
} else if (choice.equalsIgnoreCase("no")) {
// end of loop
playAgain = false;
System.out.println("Have a nice day. GoodBye");
}
}
}
}
/**
* if comeOutroll is 7 or 11 return “win” , if comeOutRoll is 2, 3, or 12
* return the string “loss”, if comeOutRoll is 4, 5, 6,8, 9, or 10 return
* the string”thePoint”
*
*/
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";
}
}
/**
* method to print the description
*/
public static void description() {
System.out
.println("Computer will play a craps 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 gets 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. ");
}
/**
* method to keep rolling the dice until you get thePoint or your get seven.
*
* @param rnd
* - random object
* @return the string “seven” if you get a seven, “thePoint” if you get the
* point
*/
public static String keepRolling(int thePoint, Random rnd) {
int diceValue1 = rnd.nextInt(6) + 1;
int diceValue2 = rnd.nextInt(6) + 1;
int sum = diceValue1 + diceValue2;
String returnString;
if (sum == 7) {
returnString = "seven";
} else if (sum == thePoint) {
returnString = "thePoint";
} else {
returnString = keepRolling(thePoint, rnd);
}
return returnString;
}
/**
* method to calculate the probability of winning
*/
public static double winProability(int wins, int losses) {
double prob = (double) wins / (wins + losses);
return prob;
}
}
/*OUTPUT*/
Computer will play a craps 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 gets 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 4923 times
lost 5077 times
for a probability of 0.4923
Would you like to play another game yes/no? hj
Would you like to play another game yes/no? yes
In the simulation we :
won 4941 times
lost 5059 times
for a probability of 0.4941
Would you like to play another game yes/no? yes
In the simulation we :
won 4973 times
lost 5027 times
for a probability of 0.4973
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.