Created
October 23, 2017 16:49
-
-
Save anonymous/27d54781868cb97965ac757289f59a0a to your computer and use it in GitHub Desktop.
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
import java.util.*; | |
public class Craps { | |
static int numPlays = 0; | |
static int firstRoll, nextRoll; | |
static String playAgain; | |
//This method will prompt the user if wants to play a game of craps | |
//and stores the input into playAgain | |
public static void wannaPlay(Scanner console) { | |
// Enter code here | |
System.out.print("Wanna play CRAPS? (Yes or No): "); | |
playAgain = console.next().toLowerCase(); | |
} | |
//This method simulates the roll of two dice using random numbers | |
//between 1 & 6, prints what was rolled, and returns the sum | |
public static int diceSum() { | |
// Enter code here | |
int roll1 = (int)(Math.random() * 6) + 1; | |
int roll2 = (int)(Math.random() * 6) + 1; | |
return roll1 + roll2; | |
} | |
//This method keeps rolling and incrementing numPlays until a 7 | |
//or the first roll is rolled. It returns the last roll. | |
public static int keepRolling() { | |
// Enter code here | |
nextRoll = diceSum(); | |
while(nextRoll != 7 && nextRoll != firstRoll) { | |
numPlays += 1; | |
nextRoll = diceSum(); //re-roll the dice | |
} | |
return nextRoll; | |
} | |
//This method will prompt the user if wants to play again | |
//and stores the input into playAgain | |
public static String playAgain (Scanner console) { | |
System.out.print("Wanna play CRAPS AGAIN? (Yes or No): "); | |
return console.next().toLowerCase(); | |
} | |
public static void main (String [] args) { | |
Scanner console = new Scanner(System.in); | |
wannaPlay(console); | |
if(!playAgain.equals("yes")) { | |
System.out.println("Okay then, bye!"); | |
return; | |
} | |
while (true) { | |
firstRoll = diceSum(); | |
numPlays = 1; | |
System.out.println ("First roll was a " + firstRoll); | |
System.out.println (); | |
if (firstRoll == 7 || firstRoll == 12) { | |
System.out.println ("Winner Winner Chicken Dinner"); | |
} | |
else if (firstRoll == 2 || firstRoll == 3 || firstRoll == 12) { | |
System.out.println ("Bummer, you lost on the first roll."); | |
} | |
else { | |
int finalRoll = keepRolling(); | |
if (finalRoll == 7) { | |
System.out.println ("Bummer, you lost on roll number " + numPlays); | |
} | |
if (finalRoll == firstRoll) { | |
System.out.println ("Yippee, you won on roll number " + numPlays); | |
} | |
} | |
System.out.println(); | |
System.out.println("======================================"); | |
System.out.println(); | |
if(!playAgain(console).equals("yes")) { | |
System.out.println("ok bye"); | |
break; | |
} | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment