-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathDice.java
More file actions
33 lines (30 loc) · 818 Bytes
/
Dice.java
File metadata and controls
33 lines (30 loc) · 818 Bytes
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
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Dice {
private static class Die{
private int result;
private Die(){
this.result =(int) (Math.random()*(6 - 1 + 1) + 1);
}
private int roll(){
return (this.result = (int) (Math.random()*(6 - 1 + 1) + 1));
}
}
private final List<Die> dice;
public Dice(int numberOfDice){
dice = Stream
.generate(Die::new)
.limit(numberOfDice)
.collect(Collectors.toList());
}
public int tossAndSum(){
return dice.stream()
.mapToInt(Die::roll)
.sum();
}
public List<Die> getDice(){
return dice;
}
}