Class Dice
java.lang.Object
Dice
Dice group for random rolls.
- Since:
- 2014-05-20
-
Field Summary
-
Constructor Summary
ConstructorDescriptionDice
(int sides) Constructor (one die only).Dice
(int num, int sides) Constructor (number, sides).Dice
(int num, int sides, int add) Constructor (number, sides, addition).Dice
(int number, int sides, int mul, int add) Constructor (all fields).Constructor (copy).Constructor (read from string descriptor). -
Method Summary
Modifier and TypeMethodDescriptionprivate int
adjustRoll
(int roll) Apply adjustments after raw dice roll.int
avgRoll()
Compute average roll.int
boundRoll
(int floor) Rolls the dice with specified floor.static boolean
coinFlip()
Flip a coin from a static context.static String
formatBonus
(int bonus) Format additive bonus with sign.static String
formatMultiplier
(int mult) Format multiplicative bonus with sign.int
getAdd()
int
getMul()
int
getNum()
int
getSides()
static void
Initialize the dice random generator.static void
Main test function.int
maxRoll()
Compute the maximum possible roll.int
minRoll()
Compute the minimum possible roll.void
modifyAdd
(int mod) Modify the addition field.int
roll()
Rolls the dice.static int
roll
(int sides) Roll one die from a static context.static int
rollPct()
Roll percentile dice from a static context.void
setAdd
(int add) void
setMul
(int mul) void
setNum
(int num) void
setSides
(int sides) private void
test()
Test case for one dice object.toString()
Identify this object as a string.
-
Field Details
-
number
private int numberNumber of dice. -
sides
private int sidesSides on the dice. -
multiplier
private int multiplierMultiplier to the dice (negative = divisor). -
addition
private int additionAddition to the dice (negative = subtraction). -
random
Random number generator. Maintaining our own generator is more efficient than calls to the Math routine in a dice-heavy program; (1) we avoid checks for null random; (2) we can call nextInt directly and avoid conversion back-and-forth with double.
-
-
Constructor Details
-
Dice
Dice(int sides) Constructor (one die only). -
Dice
Dice(int num, int sides) Constructor (number, sides). -
Dice
Dice(int num, int sides, int add) Constructor (number, sides, addition). -
Dice
Dice(int number, int sides, int mul, int add) Constructor (all fields). -
Dice
Dice(String s) Constructor (read from string descriptor). RegEx code from @user1803551 on StackExchange: http://stackoverflow.com/questions/35020687/ how-to-parse-dice-notation-with-a-java-regular-expression -
Dice
Constructor (copy).
-
-
Method Details
-
getNum
public int getNum() -
getSides
public int getSides() -
getMul
public int getMul() -
getAdd
public int getAdd() -
setNum
public void setNum(int num) -
setSides
public void setSides(int sides) -
setMul
public void setMul(int mul) -
setAdd
public void setAdd(int add) -
initialize
public static void initialize()Initialize the dice random generator. Must call this before any roll() methods. -
roll
public static int roll(int sides) Roll one die from a static context.- Returns:
- The die-roll.
-
coinFlip
public static boolean coinFlip()Flip a coin from a static context.- Returns:
- true if coin is heads
-
rollPct
public static int rollPct()Roll percentile dice from a static context.- Returns:
- The die-roll.
-
adjustRoll
private int adjustRoll(int roll) Apply adjustments after raw dice roll.- Returns:
- Roll after modifiers.
-
roll
public int roll()Rolls the dice.- Returns:
- The dice-roll.
-
boundRoll
public int boundRoll(int floor) Rolls the dice with specified floor.- Returns:
- The bounded dice-roll.
-
minRoll
public int minRoll()Compute the minimum possible roll.- Returns:
- Minimum possible roll.
-
maxRoll
public int maxRoll()Compute the maximum possible roll.- Returns:
- Maximum possible roll.
-
avgRoll
public int avgRoll()Compute average roll.- Returns:
- Average roll.
-
modifyAdd
public void modifyAdd(int mod) Modify the addition field. -
toString
Identify this object as a string. -
formatBonus
Format additive bonus with sign. -
formatMultiplier
Format multiplicative bonus with sign. -
test
private void test()Test case for one dice object. -
main
Main test function.
-