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

Write a C++ program to play the dice game "Craps". Craps is one of the most...
Write a C++ program to play the dice game "Craps". Craps is one of the most popular games of chance and is played in casinos and back alleys throughout the world. The rules of the game are straightforward: A player rolls two dice. Each die has six faces. These faces contain 1, 2, 3, 4, 5, and 6 spots. After the dice have come to rest, the sum of the spots on the two upward faces is calculated. If the...
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?
Objective Make a function to serve as a Craps dice game simulator. If you are not...
Objective Make a function to serve as a Craps dice game simulator. If you are not familiar Craps is a game that involves rolling two six-sided dice and adding up the total. We are not implementing the full rules of the game, just a function that returns the value of BOTH dice and their total. Details In the function you create: Make a function that uses pointers and pass by reference to return THREE separate outputs (arrays are not allowed)....
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...
In the game of craps, a pass line bet proceeds as follows: Two six-sided dice are...
In the game of craps, a pass line bet proceeds as follows: Two six-sided dice are rolled; the first roll of the dice in a craps round is called the “come out roll.” A come out roll of 7 or 11 automatically wins, and a come out roll of 2, 3, or 12 automatically loses. If 4, 5, 6, 8, 9, or 10 is rolled on the come out roll, that number becomes “the point.” The player keeps rolling the...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT