/* * Die.java * * Created on February 12, 2003, 12:16 AM */ package dice; import java.util.Random; /** * * @author mike */ public class Die { private int value = BADVAL; // 0 means not rolled private int numSides = 6; private static Random randGen = new Random(); // Constants public static final int BADVAL = 0; public static final int minSides = 4; public static final int maxSides = 11; public static final int defaultSides = 6; /** * Creates a new default instance of Die - with 6 sides and no initial value */ public Die() { // defaults work here mostly // do need to create a random number generator //randGen = new Random(); } /** * Creates a new instance of Die - with a given number of sides and no initial value */ public Die(int sides) { // if the number of sides is bad - just make 6 sided if ((sides > maxSides) || (sides < minSides)) { sides = 6; } numSides = sides; // need to create a random number generator //randGen = new Random(); } /** * Creates a new instance of Die - with a given number of sides and given initial value */ public Die(int sides, int val) { // if the number of sides is bad - just make 6 sided if ((sides > maxSides) || (sides < minSides)) { sides = 6; } numSides = sides; // if initial value is not between 1 and #sides - just leave unititialized // but if ok, set it if ((val >= 1) && (val <= numSides)) { value = val; } // need to create a random number generator //randGen = new Random(); } /** * report on what the current value shown on the dice is */ public int getValue() { return value; } /** * roll the dice and report the value shown on the die */ public int getRoll() { Die rolled = roll(); int val = rolled.getValue(); return val; } public Die roll() { // create value from 1 to # sides int newVal = randGen.nextInt(numSides) + 1; // change the value shown // should always succeeed because previous statement puts the value in the // right range boolean ok = setValue(newVal); // return the current die in case it is needed for something return this; } /** * report the number of sides on this die * (you think I'm joking - there are other size dice besides 6 sided) */ public int getNumSides() { return numSides; } /** * change the value shown on the die * private because client code can't cheat - must use one of the roll methods */ private boolean setValue(int val) { // if value is not between 1 and #sides - set to bad value (0) // but if ok, set it if ((val >= 1) && (val <= numSides)) { value = val; return true; } else { value = BADVAL; return false; } } /** * test if two die objects are equal (in contents) * Policy - just check value shown on dice - makes easier for client code to * check die for having a particular value */ public boolean equals(Object obj) { // better safe than sorry if (! (obj instanceof Die) ) { // can't be equal if not same type of object return false; } Die toCompare = (Die) obj; if (getValue() == toCompare.getValue() ) { // match return true; } else { return false; } } /** * show the current value shown on the die * (Policy - user not interested in how many sides - they know that) * Policy - bad value - show as #BAD# */ public String toString() { String res; if (getValue() == BADVAL) { res = "#BAD#"; } else { res = "" + getValue(); } return res; } /** * @param args the command line arguments * used to test class */ public static void main(String[] args) { Die twodice [] = new Die[2]; twodice[0] = new Die(); // 6 sided, no value twodice[1] = new Die(); // 6 sided, no value for (int time = 0; time < 10; time++) { // roll the dice int val1 = twodice[0].getRoll(); int val2 = twodice[1].getRoll(); // show the dice System.out.println("Dice rolled: " + val1 + " " + val2 + " Sum: " + (val1 + val2)); System.out.println(" (dice equal? " + twodice[0].equals(twodice[1]) + " )"); } Die eightsides = new Die(8); for (int time = 0; time < 10; time++) { // roll the dice Die temp = eightsides.roll(); int tempv = temp.getValue(); int temps = temp.getNumSides(); System.out.println("Die with " + temps + " sides has value: " + tempv); } } }