Last active
April 17, 2016 11:44
-
-
Save JoshCode/58e5e7e8ed848f56926ed43bc5afac3d 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.ArrayList; | |
import java.util.List; | |
public class Launcher { | |
public static void main(String[] args) { | |
List<String> rolls = roll(); | |
if (rolls.contains("error")) { | |
System.out.println("ERROR!"); | |
} else { | |
Integer total = rolls.size(); | |
Integer timesLife = count(rolls, "life"); | |
Integer timesStable = count(rolls, "stable"); | |
Integer timesSurvive = timesLife + timesStable; | |
Integer timesDeath = count(rolls, "death"); | |
System.out.println(total + " possibilities."); | |
System.out.println(timesLife + " times life, which is " + ((double) timesLife / total) * 100 + " percent"); | |
System.out.println(timesStable + " times stable, which is " + ((double) timesStable / total) * 100 + " percent"); | |
System.out.println(timesDeath + " times death, which is " + ((double) timesDeath / total) * 100 + " percent"); | |
System.out.println(); | |
System.out.println(timesSurvive + " times survived, which is " + ((double) timesSurvive / total) * 100 + " percent"); | |
} | |
} | |
private static List<String> roll() { | |
ArrayList<String> result = new ArrayList<>(); | |
ArrayList<Integer> rolls; | |
for (int i = 20; i > 0; i--) { | |
rolls = new ArrayList<>(); | |
rolls.add(i); | |
result.addAll(roll(rolls)); | |
} | |
return result; | |
} | |
private static List<String> roll(List<Integer> initialRolls) { | |
ArrayList<String> result; | |
switch (outcome(initialRolls)) { | |
case "life": | |
result = new ArrayList<>(); | |
result.add("life"); | |
break; | |
case "stable": | |
result = new ArrayList<>(); | |
result.add("stable"); | |
break; | |
case "death": | |
result = new ArrayList<>(); | |
result.add("death"); | |
break; | |
case "more": | |
result = new ArrayList<>(); | |
for (int i = 20; i > 0; i--) { | |
ArrayList<Integer> rolls = new ArrayList<>(); | |
rolls.addAll(initialRolls); | |
rolls.add(i); | |
result.addAll(roll(rolls)); | |
} | |
break; | |
default: | |
result = new ArrayList<>(); | |
result.add("error"); | |
break; | |
} | |
return result; | |
} | |
private static String outcome(List<Integer> rolls) { | |
if (rolls.contains(20)) { | |
return "life"; | |
} else { | |
int countSuc = 0, countFail = 0; | |
for (Integer roll : rolls) { | |
if (roll >= 10) | |
countSuc++; | |
else if (roll == 1) | |
countFail += 2; | |
else | |
countFail++; | |
} | |
if (countSuc >= 3) { | |
return "stable"; | |
} else if (countFail >= 3) { | |
return "death"; | |
} else { | |
return "more"; | |
} | |
} | |
} | |
private static Integer count(List<String> list, String value) { | |
Integer result = 0; | |
for (String str : list) { | |
if (str.equals(value)) | |
result++; | |
} | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment