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
class Main {
  public static void main(String[] args) {
    showNext(Cell.DEAD);
    System.out.println();
    showNext(Cell.ALIVE);
  }

  private static void showNext(Cell cell) {
    for (int i = 0; i <= 8; i++) {
      System.out.printf("%s.next(%d) -> %s%n", cell, i, cell.next(i));
    }
  }

}

enum Cell {

    ALIVE {
      @Override Cell next(int neighbors) {
        return (neighbors == 2 || neighbors == 3) 
          ? this : DEAD;
      }  
    }, 
    
    DEAD {
      @Override Cell next(int neighbors) {
        return (neighbors == 3) ? ALIVE : this;
      }
    };

    abstract Cell next(int neighbors);
}