forked from ChrisMayfield/ThinkJavaCode2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConway.java
More file actions
149 lines (134 loc) · 3.75 KB
/
Conway.java
File metadata and controls
149 lines (134 loc) · 3.75 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import javax.swing.JFrame;
/**
* Conway's Game of Life.
*
* @author Chris Mayfield
* @version 7.1.0
*/
public class Conway {
private GridCanvas grid;
/**
* Creates a grid with two Blinkers.
*/
public Conway() {
grid = new GridCanvas(5, 10, 20);
grid.turnOn(2, 1);
grid.turnOn(2, 2);
grid.turnOn(2, 3);
grid.turnOn(1, 7);
grid.turnOn(2, 7);
grid.turnOn(3, 7);
}
/**
* Counts the number of live neighbors around a cell.
*
* @param r row index
* @param c column index
* @return number of live neighbors
*/
private int countAlive(int r, int c) {
int count = 0;
count += grid.test(r - 1, c - 1);
count += grid.test(r - 1, c);
count += grid.test(r - 1, c + 1);
count += grid.test(r, c - 1);
count += grid.test(r, c + 1);
count += grid.test(r + 1, c - 1);
count += grid.test(r + 1, c);
count += grid.test(r + 1, c + 1);
return count;
}
/**
* Apply the update rules of Conway's Game of Life.
*
* @param cell the cell to update
* @param count number of live neighbors
*/
private static void updateCell(Cell cell, int count) {
if (cell.isOn()) {
if (count < 2 || count > 3) {
// Any live cell with fewer than two live neighbors dies,
// as if by underpopulation.
// Any live cell with more than three live neighbors dies,
// as if by overpopulation.
cell.turnOff();
}
} else {
if (count == 3) {
// Any dead cell with exactly three live neighbors
// becomes a live cell, as if by reproduction.
cell.turnOn();
}
}
}
/**
* Counts the neighbors before changing anything.
*
* @return number of neighbors for each cell
*/
private int[][] countNeighbors() {
int rows = grid.numRows();
int cols = grid.numCols();
int[][] counts = new int[rows][cols];
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
counts[r][c] = countAlive(r, c);
}
}
return counts;
}
/**
* Updates each cell based on neighbor counts.
*
* @param counts number of neighbors for each cell
*/
private void updateGrid(int[][] counts) {
int rows = grid.numRows();
int cols = grid.numCols();
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
Cell cell = grid.getCell(r, c);
updateCell(cell, counts[r][c]);
}
}
}
/**
* Simulates one round of Conway's Game of Life.
*/
public void update() {
int[][] counts = countNeighbors();
updateGrid(counts);
}
/**
* The simulation loop.
*/
private void mainloop() {
while (true) {
// update the drawing
this.update();
grid.repaint();
// delay the simulation
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// do nothing
}
}
}
/**
* Creates and runs the simulation.
*
* @param args command-line arguments
*/
public static void main(String[] args) {
String title = "Conway's Game of Life";
Conway game = new Conway();
JFrame frame = new JFrame(title);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.add(game.grid);
frame.pack();
frame.setVisible(true);
game.mainloop();
}
}