You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
34 lines
680 B
34 lines
680 B
package model;
|
|
|
|
import java.util.Random;
|
|
|
|
public class Dice {
|
|
private static final Random RANDOM = new Random();
|
|
private static final int NB_FACES_DEFAULT = 6;
|
|
|
|
private final int nbFaces;
|
|
private int value;
|
|
|
|
public Dice(int nbFaces) {
|
|
this.nbFaces = nbFaces;
|
|
}
|
|
|
|
public Dice() {
|
|
this(NB_FACES_DEFAULT);
|
|
}
|
|
|
|
public int getValue() throws IllegalAccessException {
|
|
if (value == 0)
|
|
throw new IllegalAccessException("The dice must be rolled first.");
|
|
return value;
|
|
}
|
|
|
|
public int roll() {
|
|
return value = RANDOM.nextInt(nbFaces) + 1;
|
|
}
|
|
|
|
public void clear() {
|
|
value = 0;
|
|
}
|
|
}
|