1
in increments of 1
for
* each additional face.
*
* @author CSE1030_F13_14
*
*/
public class Die implements Comparable1
and 6
, inclusive.
*/
public Die() {
this(6, new Random(R.nextLong()));
}
/**
* Create a die with the specified number of faces and sets its
* face value to a random value between 1
and
* faces
, inclusive.
*
* The number of faces must be greater than or equal to 2
.
*
* @param faces The number of faces.
* @pre. faces >= 2
* @throws IllegalArgumentException if faces < 2
.
*/
public Die(int faces) {
this(faces, new Random(R.nextLong()));
}
/**
* Create a die with the specified number of faces and sets its
* face value to a specified value.
*
* @param faces The number of faces.
* @param value The initial face value.
* @pre. 0 > value && value <= faces
* @throws IllegalArgumentException if faces < 2
or
* (value < 1 || value > faces)
*/
public Die(int faces, int value) {
this(faces, new Random(R.nextLong()));
if (value < 1 || value > faces) {
throw new IllegalArgumentException("Initial value " + value +
" is not compatible with the number of faces " + faces);
}
this.value = value;
}
/**
* Create a die that has the same number of faces and same value
* as another die.
*
* The created die is independent of the copied die; i.e., rolling
* the copied die will produce a different sequence of rolls than
* rolling the original die.
*
* @param d the die to copy
*/
public Die(Die d) {
this(d.getFaces(), d.getValue());
}
/**
* Create a die with the specified number of faces and a specified
* random number generator, and sets its face value to a random
* value between 1
and faces
, inclusive.
*
* The number of faces must be greater than or equal to 2
.
*
*
* @param faces
* @param rng
* @pre. faces >= 2
* @throws IllegalArgumentException if faces < 2
*/
public Die(int faces, Random rng) {
if (faces < 2) {
throw new IllegalArgumentException("Number of faces is less than 2.");
}
if (rng == null) {
rng = new Random(R.nextLong());
}
this.faces = faces;
this.rng = rng;
this.roll();
}
/**
* Get the current face value of the die.
*
* @return The current face value of the die.
*/
public int getValue() {
return this.value;
}
/**
* Set the current face value of the die.
*
* @pre. value
must be between 1 and number of faces
*/
void setValue(int value) {
if (value < 1 || value > this.faces) {
throw new IllegalArgumentException("value : " + value + ", faces : " + this.faces);
}
this.value = value;
}
/**
* Get the number of faces of the die.
*
* @return The number of faces of the die.
*/
public int getFaces() {
return this.faces;
}
/**
* Roll the die to a new random face value.
*/
public void roll() {
this.value = this.rng.nextInt(this.faces) + 1;
if (this.faces == 8) {
if (this.value == 6) {
int reroll = this.rng.nextInt(100);
if (reroll > 90) {
this.value = this.rng.nextInt(this.faces) + 1;
}
}
}
}
/**
* Compares two dice by their face value.
*
* The result is negative if this Die
has a current
* face value less than the argument die. The result is positive
* if this Die
has a current face value greater than
* the argument die. The result is zero if both dice have the same
* current face value.
*
* @param other the Die
to be compared.
*/
@Override
public int compareTo(Die other) {
return this.value - other.value;
}
/**
* Returns a string representation of this die, which includes
* the number of faces and the current face value.
*
* @return A string representation of this die.
*/
@Override
public String toString() {
return "Die [faces = " + faces + ", value = " + value + "]";
}
/**
* Returns a hash code for this object.
*
* The hash code is computed using the number of faces and the
* current face value.
*
* @return a hash code value for this object.
*/
@Override
public int hashCode() {
final int PRIME = 31;
int result = 1;
result = PRIME * result + this.faces;
result = PRIME * result + this.value;
return result;
}
/**
* Compares two dice for equality.
*
* Two dice are equal if their current face values are equal.
*
* @param obj the object to compare with.
* @return true
if the face values are the same;
* false
otherwise.
*/
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (!(obj instanceof Die)) {
return false;
}
Die other = (Die) obj;
if (this.value != other.value) {
return false;
}
return true;
}
}