Class Dice


public class Dice extends Object
Dice group for random rolls.
Since:
2014-05-20
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private int
    Addition to the dice (negative = subtraction).
    private int
    Multiplier to the dice (negative = divisor).
    private int
    Number of dice.
    private static Random
    Random number generator.
    private int
    Sides on the dice.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Dice(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 Type
    Method
    Description
    private int
    adjustRoll(int roll)
    Apply adjustments after raw dice roll.
    int
    Compute average roll.
    int
    boundRoll(int floor)
    Rolls the dice with specified floor.
    static boolean
    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
     
    int
     
    int
     
    int
     
    static void
    Initialize the dice random generator.
    static void
    main(String[] args)
    Main test function.
    int
    Compute the maximum possible roll.
    int
    Compute the minimum possible roll.
    void
    modifyAdd(int mod)
    Modify the addition field.
    int
    Rolls the dice.
    static int
    roll(int sides)
    Roll one die from a static context.
    static int
    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 case for one dice object.
    Identify this object as a string.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

    • number

      private int number
      Number of dice.
    • sides

      private int sides
      Sides on the dice.
    • multiplier

      private int multiplier
      Multiplier to the dice (negative = divisor).
    • addition

      private int addition
      Addition to the dice (negative = subtraction).
    • random

      private static Random 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

      public Dice(Dice d)
      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

      public String toString()
      Identify this object as a string.
      Overrides:
      toString in class Object
    • formatBonus

      public static String formatBonus(int bonus)
      Format additive bonus with sign.
    • formatMultiplier

      public static String formatMultiplier(int mult)
      Format multiplicative bonus with sign.
    • test

      private void test()
      Test case for one dice object.
    • main

      public static void main(String[] args)
      Main test function.