summaryrefslogtreecommitdiff
path: root/dice-lang/src/bjc/dicelang/v2/DiceBox.java
diff options
context:
space:
mode:
authorbculkin2442 <bjculkin@mix.wvu.edu>2017-02-11 08:44:28 -0500
committerbculkin2442 <bjculkin@mix.wvu.edu>2017-02-11 08:44:28 -0500
commit1cf218ba93396c7be7f4b3ee25d8008a41777273 (patch)
tree24fbb7a13c206c75e7c828862e04a5d0ae64cd5a /dice-lang/src/bjc/dicelang/v2/DiceBox.java
parent6ca8c6764a8b8769a7a295c0479962a6588580a2 (diff)
Parse double and some dice
Diffstat (limited to 'dice-lang/src/bjc/dicelang/v2/DiceBox.java')
-rw-r--r--dice-lang/src/bjc/dicelang/v2/DiceBox.java102
1 files changed, 102 insertions, 0 deletions
diff --git a/dice-lang/src/bjc/dicelang/v2/DiceBox.java b/dice-lang/src/bjc/dicelang/v2/DiceBox.java
new file mode 100644
index 0000000..86bce4d
--- /dev/null
+++ b/dice-lang/src/bjc/dicelang/v2/DiceBox.java
@@ -0,0 +1,102 @@
+package bjc.dicelang.v2;
+
+import java.util.Random;
+
+public class DiceBox {
+ private static final Random rng = new Random();
+
+ public interface Die {
+ boolean canOptimize();
+ int optimize();
+
+ int roll();
+ }
+
+ private static class ScalarDie implements Die {
+ private int val;
+
+ public ScalarDie(int vl) {
+ val = vl;
+ }
+
+ public boolean canOptimize() {
+ return true;
+ }
+
+ public int optimize() {
+ return val;
+ }
+
+ public int roll() {
+ return val;
+ }
+
+ public String toString() {
+ return Integer.toString(val);
+ }
+ }
+
+ private static class SimpleDie implements Die {
+ private int numDice;
+ private int diceSize;
+
+ public SimpleDie(int nDice, int size) {
+ numDice = nDice;
+ diceSize = size;
+ }
+
+ public boolean canOptimize() {
+ if(diceSize == 1) return true;
+ else return false;
+ }
+
+ public int optimize() {
+ return numDice;
+ }
+
+ public int roll() {
+ int total = 0;
+
+ for(int i = 0; i < numDice; i++) {
+ total += rng.nextInt(i) + 1;
+ }
+
+ return total;
+ }
+
+ public String toString() {
+ return numDice + "d" + diceSize;
+ }
+ }
+
+ public static Die parseExpression(String exp) {
+ if(!isValidExpression(exp)) return null;
+
+ if(exp.matches(scalarDiePattern)) {
+ return new ScalarDie(Integer.parseInt(exp));
+ } else if(exp.matches(simpleDiePattern)) {
+ String[] dieParts = exp.split("d");
+
+ if(dieParts[0].equals("")) {
+ return new SimpleDie(1, Integer.parseInt(dieParts[1]));
+ } else {
+ return new SimpleDie(Integer.parseInt(dieParts[0]), Integer.parseInt(dieParts[1]));
+ }
+ }
+
+ return null;
+ }
+
+ private static final String scalarDiePattern = "[\\+\\-]?\\d+";
+ private static final String simpleDiePattern = "(?:\\d+)?d\\d+";
+
+ public static boolean isValidExpression(String exp) {
+ if(exp.matches(scalarDiePattern)) {
+ return true;
+ } else if(exp.matches(simpleDiePattern)) {
+ return true;
+ } else {
+ return false;
+ }
+ }
+}