/** * GameDie represents a game die, such as * can be rolled in a game to get a * somewhat-random integer in a certain range * * <p> adapted from the C++ class Dice from * Ower Astrachan's "A Computer Science * Tapestry", 2nd edition, McGraw Hill, p. * 214, 217 * </p> * * @author Owen Astrachan * @author adapted by Sharon Tuttle * @author impl'd by <team members here today> * @version 2015-02-10 */ public class GameDie { // data fields private int numSides; private int top; private final static int DEFAULT_SIDES = 6; /** * create a new GameDie instance * with the default number of sides, * whose top is assumed to be 1 initially */ public GameDie() { this.numSides = this.DEFAULT_SIDES; this.top = 1; } /** * create a new GameDie with the specified * number of sides, whose top is assumed * to be 1 initially * * @param desiredNumSides number of sides * of the new die instance */ public GameDie(int desiredNumSides) { this.numSides = desiredNumSides; this.top = 1; } /** * return the current top value * of the calling GameDie instance * * @return the number on the top of this die */ public int getTop() { return this.top; } /** * roll this calling die, setting the * die's top to the result, expected to * be in [1, numSides], and also return * the rolled result * * @return the rolled result */ public int roll() { this.top = (int) Math.ceil(Math.random() * this.numSides); if (this.top == 0) { this.top = 1; } return this.top; } }