blob: 192daaa9e9e7dc80e6e71368e49140ef4fbfacab (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
package bjc.dicelang;
import java.util.Random;
/**
* A single polyhedral dice
*
* @author ben
*
*/
public class Die implements IDiceExpression {
/**
* Random # gen to use for dice
*/
private static Random rng = new Random();
/**
* Number of sides this die has
*/
private int nSides;
/**
* Create a die with the specified number of sides
*
* @param nSides
* The number of sides this dice has
*/
public Die(int nSides) {
if (nSides < 1) {
throw new UnsupportedOperationException(
"Dice with less than 1 side are not supported");
}
this.nSides = nSides;
}
@Override
public boolean canOptimize() {
return nSides == 1;
}
@Override
public int optimize() {
if (nSides != 1) {
throw new UnsupportedOperationException(
"Can't optimize " + nSides + "-sided dice");
}
return 1;
}
/*
* (non-Javadoc)
*
* @see bjc.utils.dice.IDiceExpression#roll()
*/
@Override
public int roll() {
return rng.nextInt(nSides) + 1;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "d" + nSides;
}
}
|