-
-
Save bytecodeman/2e2d78b53f40cd04987a173fce3baad3 to your computer and use it in GitHub Desktop.
Craps Simulation - Determine player's percentage wins
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: Simulate number of Craps Games to determine percentage of wins | |
*/ | |
import java.util.Scanner; | |
public class CrapsSimulation { | |
private final static String TITLE = "Craps Simulation"; | |
private final static String CONTINUE_PROMPT = "Do this again? [y/N] "; | |
private final static int LOST = 0, WON = 1, CONTINUE = 2; | |
// ********************************************** | |
// 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() { | |
int gameStatus; | |
int myPoint = 0; | |
int sumOfDice = rollDice(); | |
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(); | |
if (sumOfDice == myPoint) // win by making point | |
gameStatus = WON; | |
else if (sumOfDice == 7) // lose by rolling 7 | |
gameStatus = LOST; | |
} | |
return gameStatus; | |
} | |
// ********************************************** | |
// 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(); | |
sc.nextLine(); //Very Important | |
int wonCount = 0; | |
for (int i = 0; i < games; i++) | |
if (playCraps() == WON) | |
wonCount++; | |
double average = (double) wonCount / (double) games * 100.0; | |
System.out.println("Percentage of Wins: " + average); | |
} | |
// ********************************************** | |
// 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