Files
  • Main.java
Main.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
import java.util.Random;
import java.util.stream.IntStream;

class Main {
  public static void main(String[] args) {
    SixWatcher watcher = new SixWatcher();
    new Die().consecutiveRolls()
//      .peek(x -> System.out.printf("%d: %d%n", watcher.totalSeen, x))
      .peek(System.out::println)
      .anyMatch(rolled -> watcher.hasThreeInRow(rolled));
    
    System.out.printf("%d tries to get three 6s in a row%n",
      watcher.totalSeen);
  }
}

class Die {
  IntStream consecutiveRolls() {
    return new Random().ints(1, 7).sequential();
  }  
}

class SixWatcher {
  int totalSeen;
  int sixesInRow;
  
  boolean hasThreeInRow(int rolled) {
    totalSeen++;
    if (rolled != 6) {
      sixesInRow = 0;
      return false;
    }
    return ++sixesInRow == 3;
  }
}