-
-
Save bytecodeman/d320f43296d2d1294f32b38158e6b4fa to your computer and use it in GitHub Desktop.
Craps Statistics Analysis - Find Average Length of a craps game.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Name: Tony Silvestri | |
* Date: 11/4/2019 | |
* Course Number: CSC-111 | |
* Course Name: Intro to Java | |
* Problem Number: N/A | |
* Email: silvestri@stcc.edu | |
* Description: Craps Statistics Analysis - Find average length of a craps game | |
*/ | |
import java.util.Scanner; | |
public class CrapsStatsAnalysis { | |
private final static String TITLE = "Craps Statistics Analysis"; | |
private final static String CONTINUE_PROMPT = "Do this again? [y/N] "; | |
// ********************************************** | |
// Put as many methods you need here | |
// roll dice, calculate sum and display results | |
private static int rollDice() { | |
// pick random die values | |
int die1 = 1 + (int) (6 * Math.random()); // 1,2,3,4,5,6 | |
int die2 = 1 + (int) (6 * Math.random()); | |
return die1 + die2; // sum die values | |
} | |
private static int playCraps() { | |
final int LOST = 0, WON = 1, CONTINUE = 2; | |
int countRolls = 0; | |
int gameStatus; | |
int myPoint = 0; | |
int sumOfDice = rollDice(); | |
countRolls++; | |
switch (sumOfDice) { | |
// win on first roll | |
case 7: | |
case 11: | |
gameStatus = WON; | |
break; | |
// lose on first roll | |
case 2: | |
case 3: | |
case 12: | |
gameStatus = LOST; | |
break; | |
default: | |
gameStatus = CONTINUE; | |
myPoint = sumOfDice; | |
break; | |
} | |
while (gameStatus == CONTINUE) { | |
sumOfDice = rollDice(); | |
countRolls++; | |
if (sumOfDice == myPoint) // win by making point | |
gameStatus = WON; | |
else if (sumOfDice == 7) // lose by rolling 7 | |
gameStatus = LOST; | |
} | |
return gameStatus == WON ? countRolls : -countRolls; | |
} | |
private static void generateStats(int numberOfGames, int catchAll) { | |
int gameCounts[] = new int[catchAll + 1]; | |
int wonCount = 0; | |
for (int i = 0; i < numberOfGames; i++) { | |
int countRoll = playCraps(); | |
if (countRoll >= 0) | |
wonCount++; | |
else | |
countRoll = -countRoll; | |
if (countRoll >= catchAll) | |
gameCounts[catchAll]++; | |
else | |
gameCounts[countRoll]++; | |
} | |
reportOnAnalysis(gameCounts, numberOfGames, wonCount); | |
} | |
private static void reportOnAnalysis(int[] gameCounts, int numberOfGames, int gamesWon) { | |
double average = (double) gamesWon / (double) numberOfGames * 100.0; | |
System.out.printf("\nPercentage of Wins: %.4f\n", average); | |
System.out.println("\nNumber of Rolls Needed to Finish Game"); | |
for (int i = 1; i < gameCounts.length - 1; i++) | |
System.out.printf("%4d: %,8d\n", i, gameCounts[i]); | |
System.out.printf("%3d+: %,8d\n", gameCounts.length - 1, gameCounts[gameCounts.length - 1]); | |
int sum = 0; | |
for (int i = 1; i < gameCounts.length; i++) | |
sum += i * gameCounts[i]; | |
average = (double)sum / (double) numberOfGames; | |
System.out.printf("\nAverage Length of Game: %.4f rolls.\n\n", average); | |
} | |
// ********************************************** | |
// Start your logic coding in the process method | |
private static void process(Scanner sc, String args[]) { | |
System.out.print("Enter number of games: "); | |
int games = sc.nextInt(); | |
System.out.print("Enter top end catch all value: "); | |
int catchAll = sc.nextInt(); | |
sc.nextLine(); //Very Important | |
generateStats(games, catchAll); | |
} | |
// ********************************************** | |
// Do not change the doThisAgain method | |
private static boolean doThisAgain(Scanner sc, String prompt) { | |
System.out.print(prompt); | |
String doOver = sc.nextLine(); | |
return doOver.trim().equalsIgnoreCase("Y"); | |
} | |
// ********************************************** | |
// Do not change the main method | |
public static void main(String args[]) { | |
System.out.println("Welcome to " + TITLE); | |
Scanner sc = new Scanner(System.in); | |
do { | |
process(sc, args); | |
} while (doThisAgain(sc, CONTINUE_PROMPT)); | |
sc.close(); | |
System.out.println("Thank you for using " + TITLE); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment