forked from ChrisMayfield/ThinkJavaCode2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGrid.java
More file actions
178 lines (158 loc) · 4.31 KB
/
Grid.java
File metadata and controls
178 lines (158 loc) · 4.31 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import java.awt.Graphics;
/**
* 2D array of cells that represent Conway's Game of Life.
*/
public class Grid {
private final int rows;
private final int cols;
private final int size;
/** Cells stored in row-major order. */
private Cell[][] array;
/**
* Constructs a drawing of given size.
*
* @param rows number of rows
* @param cols number of columns
* @param size pixels per cell
*/
public Grid(int rows, int cols, int size) {
// store the configuration
this.rows = rows;
this.cols = cols;
this.size = size;
// build 2D array of cells
this.array = new Cell[rows][cols];
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
this.array[r][c] = new Cell(c * size, r * size, size);
}
}
}
/**
* Paints the grid on the screen.
*
* @param g graphics context
*/
public void draw(Graphics g) {
for (Cell[] row : array) {
for (Cell cell : row) {
cell.draw(g);
}
}
}
/**
* Toggles the cell color.
*
* @param r row index
* @param c column index
*/
public void flip(int r, int c) {
Cell cell = array[r][c];
if (cell.isOff()) {
cell.turnOn();
} else {
cell.turnOff();
}
}
/**
* @return total height of the grid
*/
public int height() {
return rows * size;
}
/**
* @return total width of the grid
*/
public int width() {
return cols * size;
}
/**
* Counts the number of live neighbors (without going out of bounds).
*
* @param r row index
* @param c column index
* @return number of live neighbors
*/
public int countAlive(int r, int c) {
int count = 0;
// previous row
if (r > 0) {
if (c > 0 && array[r - 1][c - 1].isOn()) {
count++;
}
if (array[r - 1][c].isOn()) {
count++;
}
if (c < cols - 1 && array[r - 1][c + 1].isOn()) {
count++;
}
}
// current row
if (c > 0 && array[r][c - 1].isOn()) {
count++;
}
if (c < cols - 1 && array[r][c + 1].isOn()) {
count++;
}
// next row
if (r < rows - 1) {
if (c > 0 && array[r + 1][c - 1].isOn()) {
count++;
}
if (array[r + 1][c].isOn()) {
count++;
}
if (c < cols - 1 && array[r + 1][c + 1].isOn()) {
count++;
}
}
return count;
}
/**
* Apply the rules of Conway's Game of Life.
*
* @param cell the cell to update
* @param count number of live neighbors
*/
public void updateCell(Cell cell, int count) {
if (cell.isOn()) {
if (count < 2) {
// Any live cell with fewer than two live neighbors dies,
// as if by underpopulation.
cell.turnOff();
} else if (count > 3) {
// Any live cell with more than three live neighbors dies,
// as if by overpopulation.
cell.turnOff();
} else {
// Any live cell with two or three live neighbors lives on
// to the next generation.
cell.turnOn();
}
} else {
if (count == 3) {
// Any dead cell with exactly three live neighbors
// becomes a live cell, as if by reproduction.
cell.turnOn();
}
}
}
/**
* Simulates one round of Conway's Game of Life.
*/
public void update() {
// count neighbors before changing anything
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);
}
}
// update each cell based on neighbor counts
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
updateCell(array[r][c], counts[r][c]);
}
}
}
}