Skip to content

Instantly share code, notes, and snippets.

@Lydon-01
Last active November 21, 2017 07:44
Show Gist options
  • Save Lydon-01/2c111054caaafd3f88cd1cc73703e18c to your computer and use it in GitHub Desktop.
Save Lydon-01/2c111054caaafd3f88cd1cc73703e18c to your computer and use it in GitHub Desktop.
Roll the dice, roll again if the same, otherwise return the total.
/* THIS IS BROKEN, NEEDS TO BE FIXED
* This question is taken from Udacity Lesson 4, Problem Set Question 8
*/
public class hello{
public static void main(String[] args){
roll();
System.out.println(monopolyRoll());
}
// now some code
/* QUIZ
* 1. Generate two random numbers in the 1 to 6 range.
* 2. If they are not the same, return the sum of all numbers rolled so far.
* 3. If they are the same, keep track of the total rolled so far and return to step 1.
*/
public static int roll(){
// roll the 6 sided dice and cast to int
double roll1= Math.random() * 6 + 1;
int rollInt = (int) roll1;
System.out.println("Roll Output: " + roll()); //test
System.out.println("");
return rollInt;
}
// when not the same, add the numbers to a total. If the same, return the current total
public static int monopolyRoll(){
int total = 0;
int rolls = 0;
while(rolls<3){
// put two random roll ints into an array
int [] rollArray = {roll(),roll()};
rolls +=1;
System.out.println("Dice 1 Output: " + rollArray[0]); //test
System.out.println("Dice 2 Output: " + rollArray[1]); //test
System.out.println("");
// now do the comparison tests
if(rollArray[0]==rollArray[1]){
total = total+rollArray[0]+rollArray[1];
} else if(rollArray[0]!=rollArray[1]){
total = total+rollArray[0]+rollArray[1];
return total;
}
//total = (int) totalInt;
}
return total;
}
// stop here
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment