Created
November 11, 2014 19:26
-
-
Save seronis/d6f3fde4abe7bfc72a05 to your computer and use it in GitHub Desktop.
Dice class for reddit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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