-
Notifications
You must be signed in to change notification settings - Fork 2
/
Dice.java
54 lines (48 loc) · 1.3 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
/**
* Dice.java
* This class offers methods for D&D-style dice.
* @author Emma Shumadine
* */
import java.util.*;
public class Dice {
private int sides;
private int numDice;
private static Random rand = new Random();
/**
* Creates a Dice object holding the number and type of dice
* @param diceString a string containing the dice information, ie "1d20"
* */
public Dice(String diceString) {
String[] array = diceString.split("d");
sides = Integer.parseInt(array[1]);
numDice = Integer.parseInt(array[0]);
}
/**
* Rolls the dice and returns the result
* @return the result of the roll
* */
public int roll() {
int total = 0;
for (int i = 1; i <= numDice; i++) {
total += rand.nextInt(sides + 1);
}
return total;
}
/**
* Returns a string representation of the dice
* @return a string representation of the dice
* */
public String toString() {
return numDice + "d" + sides;
}
public static void main(String[] args) {
Dice d1 = new Dice("1d20");
System.out.println(d1);
System.out.println(d1.roll());
Dice d2 = new Dice("3d10");
Dice d3 = new Dice("2d6");
System.out.println(d2);
System.out.println(d3);
System.out.println(d2.roll());
}
}