Question

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 if the total is a 2, 3, or 12 (“craps”).

If any of these five numbers is rolled on the first roll, tally the win or loss, and the round is over.

If none of the above is rolled on a first roll, then the value rolled is saved, i.e., a “point” is established. Subsequent rolls: Continue rolling the dice until either a 7 is rolled or the point is rolled.

If the point is rolled before a 7, then tally a win. If a 7 is rolled before the point, tally a loss. Then the round is over. Program specifications: 1) Run 100,000 rounds. 2) Print out information about each round.

3) Keep track of the wins and losses for all rounds played.

4) Print out the total number of round wins and losses over 100,000 rounds. You should print rounds exactly as shown on the next page. Only print the round information for the first 10 rounds. The key task, which I suggest you do first, is to get your logic for the game working. After you’ve gotten this working, put this code in a loop and run it for ten or twenty rounds, keeping track of the number of wins and losses, and printing out the total wins and losses at the end. After that, the final touch will be to make the detailed round printing, and then only printing the first ten rounds. Then you can increase the total rounds played to 100,000, and you’re done! It may be helpful to create a class to store the game information. For instance, you could create a class called CrapsRound that holds the first roll, point, result of the round, and a toString method that prints the round information in the correct format. Then you can create a class that holds an array of CrapsRound objects to simulate running many rounds.

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

// Craps.java

public class Craps {

   // uniform random integer in [0, n)
   public static int uniform(int n) {
       return (int) (Math.random() * n);
   }

   // return sum of two dice
   public static int sumOfTwoDice() {
       int x = 1 + uniform(6);
       int y = 1 + uniform(6);
       return x + y;
   }

   /***************************************************************************
   * Pass bet:
   *
   * Player rolls two dice. Let x be sum. - if x is 7 or 11 instant win - if x is
   * 2, 3, or 12 instant loss - otherwise player repeatedly rolls two dice until
   * sum is x or 7 if sum is x then win if sum is 7 then lose
   ***************************************************************************/
   public static boolean winsPassBet() {
       int x = sumOfTwoDice();
       if (x == 7 || x == 11)
           return true;
       if (x == 2 || x == 3 || x == 12)
           return false;

       while (true) {
           int y = sumOfTwoDice();
           if (y == 7)
               return false;
           if (y == x)
               return true;
       }
   }

   /***************************************************************************
   * Run simulation of pass bet n times Output winning percentage.
   ***************************************************************************/
   public static void main(String[] args) {
       int trials = 1000000; // number of pass bets to simulate
       int wins = 0; // number of pass bets won

       for (int t = 0; t < trials; t++)
           if (winsPassBet())
               wins++;

       System.out.println("Winning percentage = " + 1.0 * wins / trials);
   }

}

Screenshot of Output:

Add a comment
Know the answer?
Add Answer to:
Java programming Write a simulation of the Craps dice game. Craps is a dice game that...
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
  • 2. "Craps" is a game played by rolling two fair dice. To play one round of...

    2. "Craps" is a game played by rolling two fair dice. To play one round of this game, the player rolls the dice and the outcome is determined by the following rules: If the total number of dots is 7 or 11 (a "natural"), then the player wins. If the total number of dots is 2, 3, or 12 C'craps"), then the player loses. If the total number of dots is 4, 5, 6,8,9, or 10, then this number is...

  • 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...

  • 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,...

  • 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...

  • 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...

  • 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 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...

  • In craps, the "shooter" (the person rolling the dice) wins on the first roll (the "come-out"...

    In craps, the "shooter" (the person rolling the dice) wins on the first roll (the "come-out" roll) if they roll a 7 or 11 and loses if they roll a 2, 3, or 12, in which case the round ends. Otherwise, the first roll becomes the "point", and the round continues. What is the probability the shooter wins on the come-out roll? What is the probability the shooter loses on the come-out roll? What is the probability the point is...

  • Please write the program in python: 3. Design and implement a simulation of the game of...

    Please write the program in python: 3. Design and implement a simulation of the game of volleyball. Normal volleyball is played like racquetball, in that a team can only score points when it is serving. Games are played to 15, but must be won by at least two points. 7. Craps is a dice game played at many casinos. A player rolls a pair of normal six-sided dice. If the initial roll is 2, 3, or 12, the player loses....

  • C# Code: Write the code that simulates the gambling game of craps. To play the game,...

    C# Code: Write the code that simulates the gambling game of craps. To play the game, a player rolls a pair of dice (2 die). After the dice come to rest, the sum of the faces of the 2 die is calculated. If the sum is 7 or 11 on the first throw, the player wins and the game is over. If the sum is 2, 3, or 12 on the first throw, the player loses and the game is...

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