Skip to content

Instantly share code, notes, and snippets.

@ywindish
Created August 17, 2012 02:08
Show Gist options
  • Save ywindish/3375308 to your computer and use it in GitHub Desktop.
Save ywindish/3375308 to your computer and use it in GitHub Desktop.
Dice
package jp.windish.utils;
/**
* Dice rolling class.
* <pre>
* Dice d1 = new Dice(); // 2d6 (default)
* Dice d3 = new Dice(100); // d100
* Dice d2 = new Dice(2, 8); // 2d8
* System.out.println(d1.roll()); //=> 1 to 6 at random
* </pre>
* TODO D66 support
*/
public class Dice {
private int mFace = 6;
private int mTime = 2;
public int roll() {
int result = 0;
for (int i = 0; i < mTime; i++) {
result += ((int) (Math.random() * mFace)) + 1;
}
return result;
}
public Dice() {}
public Dice(int face) {
setFace(face);
}
public Dice(int time, int face) {
setFace(face);
setTime(time);
}
public void setFace(int face) {
mFace = face <= 0 ? 1 : face;
}
public int getFace() {
return mFace;
}
public void setTime(int time) {
mTime = time <= 0 ? 1 : time;
}
public int getTime() {
return mTime;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment