In: Computer Science
zyDE 9.16.1: Dice game Craps.
// 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...");
}
}
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...