Question

In: Computer Science

We will simulate a dice game in which 2 dice are thrown. If the roll is...

We will simulate a dice game in which 2 dice are thrown.

  • If the roll is 7 or 11, you win.
  • If the roll is 2, 3, or 12, you lose
  • If the roll is any other value, it establishes a point.
    • If with a point established, that point is rolled again before a 7, you win.
    • If, with a point established, a 7 is rolled before the point is rolled again you lose.

Build your algorithm incrementally. First write a program that simulates a roll of two dice, then outputs if it is a simple win (7 or 11), or a simple loss (2 or 3 or 12), or something else.

  1. Here is the algorithm.

//get a random number between 1 and 6, call it d1

//get a second random number between 1 and 6, call it d2.

//get the total & print it so we know what it was

//If the total is 7 or 11 print “Congratulations, you win”

//If the total is 2 or 3 or 12 print “You lose”

//If neither of these print “something else”  

  1. Now fix the part where we wrote “something else”. In this case, we rolled a number which we called total. We need to keep rolling the dice and looking at the new total each time. Output the new total each time so you can check if your program is working correctly. If the new total is the same as the total, we win. If the new total is 7, we lose. And if something else we roll again……

Sample output:

The total is 10
The new total is 8
The new total is 10
You win

Sample output:

The total is 9
The new total is 4
The new total is 8
The new total is 10
The new total is 5
The new total is 7
You lose

Sample output:

The total is 7
Congratulations, you win

Sample output:

The total is 12
You lose

Code language: Java use if-else statement

Solutions

Expert Solution

Since the question requires us to roll the dice and calculate total again and again, it is better if we create a function for the same. We are generating random numbers with the help of Math.random(). Everything that needs to be explained has been explained in the comments of the code. Further, the code output and code screenshot are available with this answer for your reference.

Java Program :

import java.util.*;
public class twoDice
{
   public static void main(String[] args) {
  
   // Get total
int total = getTotal();
       System.out.println("The total is " + total);
      
       // If total is 7 or 11, we win
       if(total == 7 || total == 11){
       System.out.println("Congratulations, you win");
       }
      
       // If total is 2,3 or 12, we lose
       else if(total == 2 || total == 3 || total == 12){
       System.out.println("You lose");
       }
      
       // Once a point is fixed, we calculate newTotal till total gets equal to newTotal
       else {
       int newTotal=0;
       while(newTotal!=total){
       newTotal=getTotal();
       System.out.println("The new total is "+newTotal);
      
       // If newTotal get equal to 7, we lose
       if(newTotal==7){
       System.out.println("You lose");
       return;
       }
       }
       System.out.println("Congratulations, you win");
       }
   }
  
  
   // Function to calculate our total
   public static int getTotal(){
   int min = 1;
   int max = 6;
   int range = max-min+1;
   int d1 = (int)(Math.random()*range)+min;
   int d2 = (int)(Math.random()*range)+min;
   int total = d1 + d2;
   return total;
   }
}

Code screenshot :

Output screenshots :

Solution ends.

Please comment and let me know if you have any further doubts. Please upvote this answer if you like it.

Thank you.

Have a good day.


Related Solutions

This problem concerns the dice game craps. On the first roll of two dice, you win...
This problem concerns the dice game craps. On the first roll of two dice, you win instantly with a sum of 7 or 11 and lose instantly with a roll of 2,3, or 12. If you roll another sum, say 5, then you continue to roll until you either roll a 5 again (win) or roll a 7 (lose). How do you solve for the probability of winning?
Two players A and B play a game of dice . They roll a pair of...
Two players A and B play a game of dice . They roll a pair of dice alternately . The player who rolls 7 first wins . If A starts then find the probability of B winning the game ?
DESCRIPTION Complete the given program to simulate the roll of two 6-sided dice and count the...
DESCRIPTION Complete the given program to simulate the roll of two 6-sided dice and count the number of rolls of each possible roll value (2 to 12). The number of rolls will be input by the user. Count the rolls of each type and then "draw" a histogram by printing a "*" for each roll of each type. The shape of the histogram will follow the "probability distribution" for large numbers of rolls, i.e., it will show more rolls for...
1. Assume we roll 6 dice. What is the probability that the roll contains: a. 6...
1. Assume we roll 6 dice. What is the probability that the roll contains: a. 6 of the same number b. 3 of one number, 2 of a second number, and 1 non-matching number c. 3 pairs of different numbers
for monopoly board game, Suppose you are on “GO”. On your next roll of the dice,...
for monopoly board game, Suppose you are on “GO”. On your next roll of the dice, what is the probability that: a. You land on Jail b. You land on any Light Blue property (Connecticut Ave, Vermont Ave, or Oriental Ave.) c. You land on Mediterranian Ave d. You land on Chance or Community Chest 2. Suppose you are on “Jail” a. What space (or spaces) is/are most likely to be landed on on your next roll? Justify your response....
. Three Dice of a Kind Consider the following game: You roll six 6-sided dice d1,…,d6...
. Three Dice of a Kind Consider the following game: You roll six 6-sided dice d1,…,d6 and you win if some number appears 3 or more times. For example, if you roll: (3,3,5,4,6,6) then you lose. If you roll (4,1,3,6,4,4) then you win. What is the probability that you win this game?
Fair Dice We roll a fair dice 10 times and register how many times we obtained...
Fair Dice We roll a fair dice 10 times and register how many times we obtained 5. (a) Find the probability to obtain 5 seven times. (b) Estimate the number of fives that will come out with the probability 0.35. (c) What is the probability of geting 30 fives when rolling a fair dice 45 times? (d) How many fives will come out with a probability of 0.25, when rollong a fair dice 45 times?
Suppose you’re playing a game where you consecutively roll 3 dice. After each roll you may...
Suppose you’re playing a game where you consecutively roll 3 dice. After each roll you may choose to either roll the next dice or sacrifice one die to reroll any number of the previous dice. If you get a number greater than 5 you win, but if you roll doubles or a number less than 6 you lose. Considering each roll a separate state what is the approximate branching factor? Justify. Draw the full state space considering only the number...
In the game of Craps, you roll two dice. When you bet on a “snake eyes”,...
In the game of Craps, you roll two dice. When you bet on a “snake eyes”, meaning a 1 on both dice, you win $30 for each $1 you bet. Otherwise, you lose your dollar. What is the probability of winning this bet? What is the expected value of making this bet? If you play this game 100 times, how much would you expect to lose?
In a dice game, Alice wins 5 (tokens) if she guesses the roll in advance; otherwise...
In a dice game, Alice wins 5 (tokens) if she guesses the roll in advance; otherwise she loses 1. The dice shows six equally likely values as usual. Verify that Alice's expected win G1 is 0. In a second experiment, Bob has two different dice in his sleeve, of which he selects one with equal probability and rolls them. One shows only even numbers, the other only odd numbers. The expected profit G2 is still 0. In the third case,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT