-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDice.java
55 lines (52 loc) · 1.03 KB
/
Dice.java
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
import java.util.Random;
/** An object that creates a die with any number of sides.
*
* @Methods roll(), rollExploding()
* @author Quinn Thorsnes
* @version <2013-10-20>
*/
public class Dice
{
private int size;
/** Constructs the Dice object.
*
* @param s - integer representing the number of sides on your die
*/
public Dice(int s)
{
size = s;
}
/** The method that rolls your die.
*
* @return the result of the die roll as an integer.
*/
public int roll()
{
int result;
Random foobar = new Random();
result = foobar.nextInt(size) + 1;
return result;
}
/** A modification on the roll method for exploding dice. If a die comes up maximum it is re-rolled and the new value added.
*
* @return the result of the exploding die roll as an integer.
*/
public int rollExploding()
{
int result;
result = this.roll();
boolean exploding = true;
while (exploding)
{
if (result == size)
{
result = result + this.roll();
}
else
{
exploding = false;
}
}
return result;
}
}