Newer
Older
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import java.util.Random;
/**
* {@link GameOfLife} instances run <i>The Game of Life</i>.
*/
public class GameOfLife {
private final Random random = new Random();
private final Grid grid;
private int generationNumber;
/**
* Creates a new {@code GameOfLife} instance given the underlying {@link Grid}.
*
* @param grid the underlying {@link Grid}
* @throws NullPointerException if {@code grid} is {@code null}
*/
public GameOfLife(Grid grid) {
// this.grid = requireNonNull(grid, "grid is null");
this.grid = grid;
grid.randomGeneration(random);
}
/**
* Transitions into the next generationNumber.
*/
public void next() {
grid.nextGeneration();
generationNumber++;
}
/**
* Returns the current generationNumber.
*
* @return the current generationNumber
*/
private int getGenerationNumber() {
return generationNumber;
}
/**
* Returns the {@link Grid}.
*
* @return the {@link Grid}
*/
public Grid getGrid() {
return grid;
}
/**
* Plays the game.
*/
public void play(int maxGenerations) {
for(int generation =0; generation < maxGenerations; generation++){
this.next();
System.out.println("generation : " + generation);
}
}
/**
* Pauses the game.
*/
public void pause() {
// timeline.pause();
}
/**
* Clears the current game.
*/
public void clear() {
pause();
grid.clear();
generationNumber = 0;
}
/**
* Clears the current game and randomly generates a new one.
*/
public void reset() {
clear();
grid.randomGeneration(random);
}
}