Introduction to Java 10th Edition Queston 6.32 *6.30 Game: craps) Craps is a pop
ID: 3602061 • Letter: I
Question
Introduction to Java 10th Edition
Queston 6.32
*6.30 Game: craps) Craps is a popular dice game played in casinos. Write a program to play a variation of the game, as follows: Roll two dice. Each die has six faces representing values 1, 2, ., and 6, respec tively. Check the sum of the two dice. If the sum is 2, 3, or 12 (called craps), you lose; if the sum is 7 or 11 (called natural), you win; if the sum is another value (i.e., 4, 5, 6, 8, 9, or 10), a point is established. Continue to roll the dice until either a 7 or the same point value is rolled. If 7 is rolled, you lose. Otherwise, you win. Your program acts as a single player. Here are some sample runs. You rolled 5 + 6 = 11 You wirn You rolled 1 + 2 = 3 You lose You rolled 4+4=8 point is 8 You rolled 6 + 2 = 8 You wirn You rolled 3 + 2 = 5 point is 5 You rolled 2 + 5 = 7 You loseExplanation / Answer
Code:
public class Roll {
public static void main(String[] args) {
//initializing an integer variable to hold the value of total number of wins
int num_of_wins = 0;
//for loop to roll the dice 10,000 times. The loop will run for 10,000 times
for (int i = 0; i < 10000; i++) {
String result ="";
//Calling roll_Dice method. This method returns a random number between 1 to 6. This is stored in roll_number_1 which will be the value of first roll
int roll_number_1 = roll_Dice();
//Calling check_result method. We are passing the value of roll 1. Method returns the string "You win" or "You lose"
result = check_result(roll_number_1);
//If the result is not win or lose, Second dice is rolled. if values of first roll and second roll are equal the value of "result" is "You win."
if(result.equals("point is"+roll_number_1))
{
//a boolean variable to check whether second roll satisfy the second roll conditions
boolean check_condition = false;
//If second roll condition is satisfied loop will exit. Else Second roll will be rolled untill the condition is satisfied
while(check_condition == false){
//Calling roll_Dice method. This method returns a random number between 1 to 6. This is stored in roll_number_2 which will be the value of second 2 roll
int roll_number_2 = roll_Dice();
//Checking if the values of both the rolls is equal to 7
if(roll_number_2 == 7)
{
result = "You lose.";
check_condition = true;
}
else
//Checking if the values of both the rolls is equal or not
if(roll_number_1 == roll_number_2)
{
result = "You win.";
check_condition = true;
}else
if(roll_number_1 != roll_number_2)
{
result = "You lose.";
check_condition = false;
}
}
}
//if loop to check whether value of "result" is "You win"
if (result.equals("You win."))
{
//if value of "result" is "You win." we are incrementing the variable "num_of_wins". This will give the number of times result is "You win."
num_of_wins++;
}
}
//Printing the value of "num_of_wins". This the number of time the result is "You win."
System.out.println("The total number of wins: " + num_of_wins);
}
public static String check_result(int roll_value) {
//initializing an empty string to hold the result
String result ="";
//checking whether value of roll is 7 or 11, to check win condition
if (roll_value == 7 || roll_value == 11)
{
result = "You win.";
}else
//checking whether value of roll is 2 or 3 or 11, to check lose condition
if (roll_value == 2 || roll_value == 3 || roll_value == 12)
{
result = "You lose.";
}else
{
//If it is none of the above conditions are satisfied the below string is returned
result = "point is"+roll_value;
}
//returning the result variable
return result;
}
public static int roll_Dice() {
//Getting a random number between 1 and 6 for first roll
int first_roll = (int) (Math.random() * 6 + 1);
//Getting a random number between 1 and 6 for second roll
int second_roll = (int) (Math.random() * 6 + 1);
// returning the sum of both the rolls
return first_roll+second_roll;
}
}
OutPut:
On first run -> The total number of wins: 4850
On seecond run -> The total number of wins: 4914
Note -> the number of wins vary for every run as the result will be random
Kindly comment if you need any help with the code.
Thanks
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.