Skip to content

Instantly share code, notes, and snippets.

@seronis
Created November 11, 2014 19:26
Show Gist options
  • Save seronis/d6f3fde4abe7bfc72a05 to your computer and use it in GitHub Desktop.
Save seronis/d6f3fde4abe7bfc72a05 to your computer and use it in GitHub Desktop.
Dice class for reddit
package com.seronis.reddit.dicebag;
import java.util.Random;
public abstract class Die {
protected int value;
protected int sides;
protected boolean froze;
protected Random rand = new Random();
protected Die(int _sides) {
this.froze = false;
this.sides = _sides;
this.value = 0;
}
public int Roll() {
if( !froze ) {
value = rand.nextInt(sides);
}
return GetValue();
}
public abstract int GetValue();
public abstract String Display();
public void Lock() {
froze = true;
}
public void Unlock() {
froze = false;
}
public boolean isLocked() {
return froze;
}
}
package com.seronis.reddit.dicebag;
public class DieNumeric extends Die {
protected DieNumeric(int _sides) {
super(_sides);
}
@Override
public int GetValue() {
return value+1;
}
@Override
public String Display() {
return "" + GetValue();
}
}
package com.seronis.reddit.dicebag;
public class DieSymbolic extends Die {
String[] labels;
public DieSymbolic(String[] _labels) {
super(_labels.length);
labels = new String[sides];
for(int pos=0; pos<sides; ++pos) {
labels[pos] = _labels[pos];
}
}
@Override
public int GetValue() {
return value+1;
}
@Override
public String Display() {
return labels[value];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment