Question

In: Computer Science

zyDE 9.16.1: Dice game Craps. Modify the playerWins() method to update highCredits. The current solution does...

zyDE 9.16.1: Dice game Craps.

  • Modify the playerWins() method to update highCredits. The current solution does not work correctly.
  • Increment numTurns in the most appropriate method. The current solution does not work correctly. As a result, the percentage of wins calculation attempts to divide by zero.

// Simulates a dice game called Craps
import java.util.Random;
import java.text.DecimalFormat;
import java.util.Scanner;

public class Craps {
// Game statistics
private int diceTotal;
private int credits;
private int numTurns;
private int numWins;
private int highCredits;
private int startCredits;

// Random number generator to simulate dice
private Random diceThrower;

// Constructor initializes instance members
public Craps() {
diceTotal = 0;
diceThrower = new Random();
}

// Return current credits
public int getCredits() {
return credits;
}

// Set number of credits and check for
// negative values
public void setCredits(int amount) {
credits = 0;
if (amount > 0) {
credits = amount;
}
}

// Generates two random numbers (1 - 6)
// to simulate two dice
private void rollDice() {
int d1 = diceThrower.nextInt(6) + 1;
int d2 = diceThrower.nextInt(6) + 1;
diceTotal = d1 + d2;
}

// Update statistics when player wins
private void playerWins() {
++credits;
++numWins;
  
// FIX ME: update highCredits if new high is achieved
highCredits = credits;
}

// Print game statistics
private void printStatistics() {
DecimalFormat fmt = new DecimalFormat("#,###.#");
double percentWins = ((double) numWins / numTurns) * 100.0;
System.out.println("\n\nGame Results");
System.out.println("Start Credits: " + fmt.format(startCredits));
System.out.println("Turns: " + fmt.format(numTurns));
System.out.println("Wins: " + fmt.format(numWins)
+ " (" + fmt.format(percentWins) + "%)");
System.out.println("High Credits: " + fmt.format(highCredits));
}

// Set game statistics to starting conditions
private void resetStatistics() {
numTurns = 0;
numWins = 0;
highCredits = 0;
startCredits = credits;
}

// Simulates one player turn
// Returns true if player wins and false if player loses
public boolean takeOneTurn() {
boolean playerWon;

// Player takes initial roll
rollDice();

// FIX ME: Increment numTurns

// Player wins with 7 or 11
if (diceTotal == 7 || diceTotal == 11) {
playerWon = true;
playerWins();
}
// Player loses with 2, 3 or 12   
else if (diceTotal <= 3 || diceTotal == 12) {
playerWon = false;
--credits;
}
// Player keeps rolling until 7 or the point
else {
int point = diceTotal;

do {
rollDice();
} while (diceTotal != point && diceTotal != 7);
  
// Player wins with the point
if (diceTotal == point) {
playerWon = true;
playerWins();
}
// Player loses with 7   
else {
playerWon = false;
--credits;
}
}
return playerWon;
}

// Play a specified number of turns and track
// the percentage of player wins
public void takeMultipleTurns(int numTurns) {
setCredits(10);
resetStatistics();
for (int i = 0; i < numTurns; ++i) {
takeOneTurn();
}
}

// Play until credits = 0 given a starting amount
public void playUntilBroke(int startingCredits) {
setCredits(startingCredits);
resetStatistics();
while (credits > 0) {
takeOneTurn();
}
}

// main() method uses an object of type Craps
public static void main(String [] args) {
Scanner scnr = new Scanner(System.in);
Craps crapsGame = new Craps();
int startCredits = 10;
  
// Continue as long as player chooses to
do {
crapsGame.playUntilBroke(startCredits);
crapsGame.printStatistics();

System.out.print("Enter starting credits (0 to quit): ");
startCredits = scnr.nextInt();
} while (startCredits > 0);
  
System.out.println("\nGood bye...");
}   
}

Solutions

Expert Solution

Here is the fixed code for this problem. Let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

// Craps.java

import java.util.Random;

import java.text.DecimalFormat;

import java.util.Scanner;

public class Craps {

      // Game statistics

      private int diceTotal;

      private int credits;

      private int numTurns;

      private int numWins;

      private int highCredits;

      private int startCredits;

      // Random number generator to simulate dice

      private Random diceThrower;

      // Constructor initializes instance members

      public Craps() {

            diceTotal = 0;

            diceThrower = new Random();

      }

      // Return current credits

      public int getCredits() {

            return credits;

      }

      // Set number of credits and check for

      // negative values

      public void setCredits(int amount) {

            credits = 0;

            if (amount > 0) {

                  credits = amount;

            }

      }

      // Generates two random numbers (1 - 6)

      // to simulate two dice

      private void rollDice() {

            int d1 = diceThrower.nextInt(6) + 1;

            int d2 = diceThrower.nextInt(6) + 1;

            diceTotal = d1 + d2;

      }

      // Update statistics when player wins

      private void playerWins() {

            ++credits;

            ++numWins;

            // FIXed: update highCredits if new high is achieved

            if (credits > highCredits)

                  highCredits = credits;

      }

      // Print game statistics

      private void printStatistics() {

            DecimalFormat fmt = new DecimalFormat("#,###.#");

            double percentWins = ((double) numWins / numTurns) * 100.0;

            System.out.println("\n\nGame Results");

            System.out.println("Start Credits: " + fmt.format(startCredits));

            System.out.println("Turns: " + fmt.format(numTurns));

            System.out.println("Wins: " + fmt.format(numWins) + " ("

                        + fmt.format(percentWins) + "%)");

            System.out.println("High Credits: " + fmt.format(highCredits));

      }

      // Set game statistics to starting conditions

      private void resetStatistics() {

            numTurns = 0;

            numWins = 0;

            highCredits = 0;

            startCredits = credits;

      }

      // Simulates one player turn

      // Returns true if player wins and false if player loses

      public boolean takeOneTurn() {

            boolean playerWon;

            // Player takes initial roll

            rollDice();

            // FIXed: Increment numTurns

            numTurns++;

            // Player wins with 7 or 11

            if (diceTotal == 7 || diceTotal == 11) {

                  playerWon = true;

                  playerWins();

            }

            // Player loses with 2, 3 or 12

            else if (diceTotal <= 3 || diceTotal == 12) {

                  playerWon = false;

                  --credits;

            }

            // Player keeps rolling until 7 or the point

            else {

                  int point = diceTotal;

                  do {

                        rollDice();

                  } while (diceTotal != point && diceTotal != 7);

                  // Player wins with the point

                  if (diceTotal == point) {

                        playerWon = true;

                        playerWins();

                  }

                  // Player loses with 7

                  else {

                        playerWon = false;

                        --credits;

                  }

            }

            return playerWon;

      }

      // Play a specified number of turns and track

      // the percentage of player wins

      public void takeMultipleTurns(int numTurns) {

            setCredits(10);

            resetStatistics();

            for (int i = 0; i < numTurns; ++i) {

                  takeOneTurn();

            }

      }

      // Play until credits = 0 given a starting amount

      public void playUntilBroke(int startingCredits) {

            setCredits(startingCredits);

            resetStatistics();

            while (credits > 0) {

                  takeOneTurn();

            }

      }

      // main() method uses an object of type Craps

      public static void main(String[] args) {

            Scanner scnr = new Scanner(System.in);

            Craps crapsGame = new Craps();

            int startCredits = 10;

            // Continue as long as player chooses to

            do {

                  crapsGame.playUntilBroke(startCredits);

                  crapsGame.printStatistics();

                  System.out.print("Enter starting credits (0 to quit): ");

                  startCredits = scnr.nextInt();

            } while (startCredits > 0);

            System.out.println("\nGood bye...");

      }

}

/*OUTPUT*/

Game Results

Start Credits: 10

Turns: 158

Wins: 74 (46.8%)

High Credits: 14

Enter starting credits (0 to quit): 100

Game Results

Start Credits: 100

Turns: 2,324

Wins: 1,112 (47.8%)

High Credits: 112

Enter starting credits (0 to quit): 5

Game Results

Start Credits: 5

Turns: 23

Wins: 9 (39.1%)

High Credits: 9

Enter starting credits (0 to quit): 1000

Game Results

Start Credits: 1,000

Turns: 73,488

Wins: 36,244 (49.3%)

High Credits: 1,001

Enter starting credits (0 to quit): 0

Good bye...


Related Solutions

Craps is a dice game in which the players make wagers on the outcome of the...
Craps is a dice game in which the players make wagers on the outcome of the roll, or a series of rolls, of a pair of dice. Most outcomes depend on the sum of the up faces of two, fair, six-sided dice. 1) What is the lower class boundary of the "6" class? 2) Find the mean. 3) Find the standard deviation. 4) Find the z-score for snake eyes.
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?
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 the game of craps, a player (known as the shooter) rolls two fair six-sided dice....
In the game of craps, a player (known as the shooter) rolls two fair six-sided dice. The shooter immediately loses if the sum of the dice is 2, 3, or 12 and immediately wins if the sum of the dice is 7 or 11 on the first roll. If the sum is anything else (4, 5, 6, 8, 9, or 10), that number becomes the point and the shooter rolls again. The shooter now wins by rolling that same point...
For this assignment, write a program that uses functions to simulate a game of Craps. Craps...
For this assignment, write a program that uses functions to simulate a game of Craps. Craps is a game of chance where a player (the shooter) will roll 2 six-sided dice. The sum of the dice will determine whether the player (and anyone that has placed a bet) wins immediately, loses immediately, or if the game continues. If the sum of the first roll of the dice (known as the come-out roll) is equal to 7 or 11, the player...
Consider the game of craps designed by Econ 261 Hotel students. The game consists of rolling...
Consider the game of craps designed by Econ 261 Hotel students. The game consists of rolling two fair six-sided dice. You win a dollar if the sum of the dots on the two dice is 2, 3, 4, or 5; if the sum of the dots on the two dice is 9, 10, 11, or 12 you lose a dollar. You win nothing, (that is you get $0) if the sum is 6, 7, or 8. The variance of X,...
2) This question is about providing game logic for the game of craps we developed its...
2) This question is about providing game logic for the game of craps we developed its shell in class. THe cpp of the class is attached to this project. At the end of the game, you should ask user if wants to play another game, if so, make it happen. Otherwise quit. Here is the attached cpp file for que2:- /*** This is an implementation of the famous 'Game of Chance' called 'craps'. It is a dice game. A player...
In Craps once the point is set, the shooter continues to roll the dice until either...
In Craps once the point is set, the shooter continues to roll the dice until either the point comes up (in which case the shooter wins) or a 7 comes up (in which case the shooter loses). At that time, the round ends. Suppose the point has just been set at 6. What is the probability that the round will end in 5 rolls or fewer (not including the come-out roll)? Suppose the point has just been set at 4....
Overview For this assignment, write a program that uses functions to simulate a game of Craps....
Overview For this assignment, write a program that uses functions to simulate a game of Craps. Craps is a game of chance where a player (the shooter) will roll 2 six-sided dice. The sum of the dice will determine whether the player (and anyone that has placed a bet) wins immediately, loses immediately, or if the game continues. If the sum of the first roll of the dice (known as the come-out roll) is equal to 7 or 11, the...
Use at least 50000 simulations to answer the following questions. In the game of craps, the...
Use at least 50000 simulations to answer the following questions. In the game of craps, the shooter rolls two dice and wins if the sum of the dice is 7 or 11 ("natural"); he loses if the sum is 2,3, or 12 (craps). If the sum is 4, 5, 6, 8, 9, or 10, then the result is not yet decided. He must roll the dice again and again, as often as is necessary until the initial sum, be it...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT