Question

Java Craps in Java. The following are the rules for a pass bet in the game...

Java

Craps in Java. The following are the rules for a pass bet in the game of craps. Roll two six-sided dice, and let x be their sum. If x is 7 or 11, you win. If x is 2, 3, or 12, you lose. Otherwise, repeatedly roll two the dice until their sum is either x or 7. If their sum is x, you win. If their sum is 7, you lose. Compose a modular program to estimate the probability of winning a pass bet. Modify your program to handle loaded dice, where the probability of a die landing on 1 is taken from the command line, the probability of landing on 6 is 1/6 minus that probability, and 2-5 are assumed equally likely.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Below is the solution:

Craps.java:

import java.util.Random;
import java.util.Scanner;
//import java.lang.String;

public class Craps {
   private final static Random random = new Random(); // Random class
   // main method

   public static void main(String[] args) {
       Scanner keyboard = new Scanner(System.in); // scanner object
       String answer;
       int r1, r2;
       int winScore = 0, trials = 0;
       do {
           r1 = random.nextInt(6) + 1; // store roll1 in a new r1 variable
           r2 = random.nextInt(6) + 1; // store roll2 in a new r2 variable
           // sum of the two rolls
           int thePoint = (r1 + r2); // add the two dice roll
           // ptints the sum of two dice
           System.out.print("You rolled " + thePoint + " . ");
           // check for the win point of sum dice
           if (thePoint == 7 || thePoint == 11) { // if sum of two roll dice is 7 or 11
               System.out.println("You Win!"); // winner
               winScore++; // win times add
               trials++; // try times add
               // cehck for the loose dice score of sum
           } else if (thePoint == 2 || thePoint == 3 || thePoint == 12) { // if sum of two roll dice is 2 or 3 or 12
               System.out.println("You Lose!"); // loser
               trials++; // try times add
           } else { // otherwise
               while (thePoint != 7) { // loop run untill the sum of two dice roll is 7
                   r1 = random.nextInt(6) + 1; // store roll1 in a new r1 variable
                   r2 = random.nextInt(6) + 1; // store roll2 in a new r2 variable
                   int roll2 = (r1 + r2); // add the two dice roll

                   if (thePoint == roll2) {
                       System.out.println("You Win!");
                       winScore++; // win times add
                       trials++; // try times add
                       break;
                   } else if (roll2 == 7) {
                       System.out.println("You Lose!");
                       trials++; // try times add
                   }
               }
           }
           System.out.println("Would you like to play again? [y/n]"); // ask to play again(y/n)
           answer = keyboard.nextLine().toLowerCase(); // accept the letter to play or not
       } while (answer.equals("y"));

       System.out.println("Winning percentage = " + 1.0 * winScore / trials); // prints the winning score
   }
}

output:

You rolled 9 . You Lose!
You Lose!
You Lose!
You Lose!
You Win!
Would you like to play again? [y/n]
y
You rolled 9 . You Lose!
You Lose!
You Win!
Would you like to play again? [y/n]
y
You rolled 7 . You Win!
Would you like to play again? [y/n]
y
You rolled 7 . You Win!
Would you like to play again? [y/n]
y
You rolled 4 . You Win!
Would you like to play again? [y/n]
y
You rolled 11 . You Win!
Would you like to play again? [y/n]
y
You rolled 8 . You Win!
Would you like to play again? [y/n]
y
You rolled 12 . You Lose!
Would you like to play again? [y/n]
y
You rolled 4 . You Win!
Would you like to play again? [y/n]
y
You rolled 7 . You Win!
Would you like to play again? [y/n]
y
You rolled 6 . You Win!
Would you like to play again? [y/n]
n
Winning percentage = 0.5882352941176471

Add a comment
Know the answer?
Add Answer to:
Java Craps in Java. The following are the rules for a pass bet in the game...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Problem 9 A single game of craps (a dice game) consists of at most two rolls...

    Problem 9 A single game of craps (a dice game) consists of at most two rolls of a pair of six sided dice. The ways to win are as follows: Win-the first roll of the pair of dice sums to either 7 or 1 (you win, game over, no second roll Win the first roll of the pair of dice does NOT sum to either 7 or 1 but the sum of the second roll is equal to the sum...

  • 9. In the casino dice game Craps, players make wagers on a sequence of rolls of...

    9. In the casino dice game Craps, players make wagers on a sequence of rolls of a pair of dice. A sequence of rolls starts with the "shooter" making an initial roll of two dice called the "come-out” roll. If the sum of the dice on the initial roll is 7 or 11 then a player with a bet on the Pass Line wins. If the initial roll results in a sum of 2, 3, or 12 ("craps") then a...

  • Craps

    Write a C++ game that plays Craps. Craps is a game played with a pair of dice. The shooter (the player with the dice) rolls a pair of dice and the number of spots showing on the two upward faces are added up. If the opening roll (called the ‘come out roll’) is a 7 or 11, the shooter wins the game. If the opening roll results in a 2 (snake eyes), 3 or 12 (box cars), the shooter loses,...

  • Java programming Write a simulation of the Craps dice game. Craps is a dice game that...

    Java programming Write a simulation of the Craps dice game. Craps is a dice game that revolves around rolling two six-sided dice in an attempt to roll a particular number. Wins and losses are determined by rolling the dice. This assignment gives practice for: printing, loops, variables, if-statements or switch statements, generating random numbers, methods, and classes. Craps game rules: First roll: The first roll (“come-out roll”) wins if the total is a 7 or 11. The first roll loses...

  • In the game of "Craps", a roller plays by rolling two six-sided dice. The score for...

    In the game of "Craps", a roller plays by rolling two six-sided dice. The score for each roll is the sum of the two dice. A roller can win on the first roll by rolling either a sum or 7 or 11. The probability of this occurring can be found using the sample space to be 0.2222. This probability (0.2222) is an example of what type of probability? O Theoretical Probability O Empirical Probability O Sample Probability There is not...

  • Write a c++ program that simulates a million of games in craps. I am having a...

    Write a c++ program that simulates a million of games in craps. I am having a trouble getting to loop a million of times. I am trying to use a const for a million. This is the code so far. #include <iostream> #include <cstdlib>// contains prototypes for functions srand and rand #include <ctime>// contains prototype for function time #include <iomanip> using namespace std; int rollDice(); // rolls dice, calculates and displays sum void printstats(); int totroll = 0, games, point,...

  • in C++ This assignment is to use your knowledge of Chap 1-5 to write a simulation...

    in C++ This assignment is to use your knowledge of Chap 1-5 to write a simulation program to estimate the winning chance of the Craps game. Craps is a popular dice game played in casinos. Here is how to play: Roll two dice. Each die has 6 faces representing values 1,2,3,4,5,and 6, respectively. Check the sum of 2 dice. If the sum is 2, 3, or 12 (called craps), you lose; if the sum is 7 or 11 (called natural),...

  • This is in C++. The program needs to show the points as well. The blue boxes...

    This is in C++. The program needs to show the points as well. The blue boxes show below are the output and it should look like that. Thanks (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, respectively. Check the sum of the two dice. If the sum is 2, 3,...

  • Game of Craps C++ #include <iostream> using namespace std; void roll (int *); // using pointer...

    Game of Craps C++ #include <iostream> using namespace std; void roll (int *); // using pointer to catch the random variable value of two dicerolls at one time . //these global variable with static variable win use to operate the working of code . //static win, account because two times input given by user to add the diceroll with win number and //account (means balance of user) . static int account = 100; static int win = 0; int bet...

  • Please i need helpe with this JAVA code. Write a Java program simulates the dice game...

    Please i need helpe with this JAVA code. Write a Java program simulates the dice game called GAMECrap. For this project, assume that the game is being played between two players, and that the rules are as follows: Problem Statement One of the players goes first. That player announces the size of the bet, and rolls the dice. If the player rolls a 7 or 11, it is called a natural. The player who rolled the dice wins. 2, 3...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT