Skip to content

Commit f351425

Browse files
committed
put all Color logic in Cell
1 parent 6a7b08b commit f351425

3 files changed

Lines changed: 75 additions & 50 deletions

File tree

ch16/Cell.java

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@
66
*/
77
public class Cell {
88

9+
public static final Color OFF = Color.WHITE;
10+
public static final Color ON = Color.BLACK;
11+
912
private final int x;
1013
private final int y;
1114
private final int size;
1215
private Color color;
1316

1417
/**
15-
* Constructs a new cell.
18+
* Constructs a new cell, initially turned off.
1619
*
1720
* @param x the X coordinate
1821
* @param y the Y coordinate
@@ -22,14 +25,19 @@ public Cell(int x, int y, int size) {
2225
this.x = x;
2326
this.y = y;
2427
this.size = size;
25-
this.color = Color.WHITE;
28+
this.color = OFF;
2629
}
2730

2831
/**
29-
* @return true if the cell is alive
32+
* Paints the cell on the screen.
33+
*
34+
* @param g graphics context
3035
*/
31-
public boolean alive() {
32-
return this.color == Color.BLACK;
36+
public void draw(Graphics g) {
37+
g.setColor(this.color);
38+
g.fillRect(this.x + 1, this.y + 1, this.size - 1, this.size - 1);
39+
g.setColor(Color.LIGHT_GRAY);
40+
g.drawRect(this.x, this.y, this.size, this.size);
3341
}
3442

3543
/**
@@ -47,15 +55,31 @@ public void setColor(Color color) {
4755
}
4856

4957
/**
50-
* Paints the cell on the screen.
51-
*
52-
* @param g graphics context
58+
* @return true if the cell is on
5359
*/
54-
public void draw(Graphics g) {
55-
g.setColor(this.color);
56-
g.fillRect(this.x + 1, this.y + 1, this.size - 1, this.size - 1);
57-
g.setColor(Color.LIGHT_GRAY);
58-
g.drawRect(this.x, this.y, this.size, this.size);
60+
public boolean isOff() {
61+
return this.color == OFF;
62+
}
63+
64+
/**
65+
* @return true if the cell is off
66+
*/
67+
public boolean isOn() {
68+
return this.color == ON;
69+
}
70+
71+
/**
72+
* Sets the cell's color to OFF.
73+
*/
74+
public void turnOff() {
75+
this.color = OFF;
76+
}
77+
78+
/**
79+
* Sets the cell's color to ON.
80+
*/
81+
public void turnOn() {
82+
this.color = ON;
5983
}
6084

6185
}

ch16/Grid.java

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import java.awt.Canvas;
2-
import java.awt.Color;
32
import java.awt.Graphics;
43

54
/**
6-
* 2D array of cells.
5+
* Drawing of a 2D array of cells.
76
*/
87
public class Grid extends Canvas {
98

@@ -42,12 +41,12 @@ public Grid(int rows, int cols, int size) {
4241
* @param r row index
4342
* @param c column index
4443
*/
45-
public void flip(int r, int c) {
44+
public void toggle(int r, int c) {
4645
Cell cell = array[r][c];
47-
if (cell.alive()) {
48-
cell.setColor(Color.WHITE);
46+
if (cell.isOff()) {
47+
cell.turnOn();
4948
} else {
50-
cell.setColor(Color.BLACK);
49+
cell.turnOff();
5150
}
5251
}
5352

@@ -58,39 +57,39 @@ public void flip(int r, int c) {
5857
* @param c column index
5958
* @return number of live neighbors
6059
*/
61-
public int countLive(int r, int c) {
60+
public int countAlive(int r, int c) {
6261
int count = 0;
6362

6463
// previous row
6564
if (r > 0) {
66-
if (c > 0 && array[r - 1][c - 1].alive()) {
65+
if (c > 0 && array[r - 1][c - 1].isOn()) {
6766
count++;
6867
}
69-
if (array[r - 1][c].alive()) {
68+
if (array[r - 1][c].isOn()) {
7069
count++;
7170
}
72-
if (c < cols - 1 && array[r - 1][c + 1].alive()) {
71+
if (c < cols - 1 && array[r - 1][c + 1].isOn()) {
7372
count++;
7473
}
7574
}
7675

7776
// current row
78-
if (c > 0 && array[r][c - 1].alive()) {
77+
if (c > 0 && array[r][c - 1].isOn()) {
7978
count++;
8079
}
81-
if (c < cols - 1 && array[r][c + 1].alive()) {
80+
if (c < cols - 1 && array[r][c + 1].isOn()) {
8281
count++;
8382
}
8483

8584
// next row
8685
if (r < rows - 1) {
87-
if (c > 0 && array[r + 1][c - 1].alive()) {
86+
if (c > 0 && array[r + 1][c - 1].isOn()) {
8887
count++;
8988
}
90-
if (array[r + 1][c].alive()) {
89+
if (array[r + 1][c].isOn()) {
9190
count++;
9291
}
93-
if (c < cols - 1 && array[r + 1][c + 1].alive()) {
92+
if (c < cols - 1 && array[r + 1][c + 1].isOn()) {
9493
count++;
9594
}
9695
}
@@ -105,25 +104,25 @@ public int countLive(int r, int c) {
105104
* @param count number of live neighbors
106105
*/
107106
public void updateCell(Cell cell, int count) {
108-
if (cell.alive()) {
107+
if (cell.isOn()) {
109108
if (count < 2) {
110109
// Any live cell with fewer than two live neighbors dies,
111110
// as if by underpopulation.
112-
cell.setColor(Color.WHITE);
111+
cell.turnOff();
113112
} else if (count > 3) {
114113
// Any live cell with more than three live neighbors dies,
115114
// as if by overpopulation.
116-
cell.setColor(Color.WHITE);
115+
cell.turnOff();
117116
} else {
118117
// Any live cell with two or three live neighbors lives on
119118
// to the next generation.
120-
cell.setColor(Color.BLACK);
119+
cell.turnOn();
121120
}
122121
} else {
123122
if (count == 3) {
124123
// Any dead cell with exactly three live neighbors
125124
// becomes a live cell, as if by reproduction.
126-
cell.setColor(Color.BLACK);
125+
cell.turnOn();
127126
}
128127
}
129128
}
@@ -133,19 +132,18 @@ public void updateCell(Cell cell, int count) {
133132
*/
134133
public void playGame() {
135134

136-
// count neighbors
135+
// count neighbors before changing anything
137136
int[][] counts = new int[rows][cols];
138137
for (int r = 0; r < rows; r++) {
139138
for (int c = 0; c < cols; c++) {
140-
counts[r][c] = countLive(r, c);
139+
counts[r][c] = countAlive(r, c);
141140
}
142141
}
143142

144-
// update cells
143+
// update each cell based on neighbor counts
145144
for (int r = 0; r < rows; r++) {
146145
for (int c = 0; c < cols; c++) {
147-
Cell cell = array[r][c];
148-
updateCell(cell, counts[r][c]);
146+
updateCell(array[r][c], counts[r][c]);
149147
}
150148
}
151149
}
@@ -155,6 +153,7 @@ public void playGame() {
155153
*
156154
* @param g graphics context
157155
*/
156+
@Override
158157
public void paint(Graphics g) {
159158
for (Cell[] row : array) {
160159
for (Cell cell : row) {
@@ -164,11 +163,13 @@ public void paint(Graphics g) {
164163
}
165164

166165
/**
167-
* Overriding this method makes the simulation more smooth, because the
168-
* Canvas does not need to be cleared before redrawing.
166+
* Overriding this method helps the simulation run more smoothly. Normally
167+
* the Canvas is cleared before painting, but there is no need to clear it
168+
* since paint draws the entire grid.
169169
*
170170
* @param g graphics context
171171
*/
172+
@Override
172173
public void update(Graphics g) {
173174
paint(g);
174175
}

ch16/Main.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
import javax.swing.JFrame;
22

33
/**
4-
* Example simulation of moving objects.
4+
* Example simulation of Conway's Game of Life.
55
*/
66
public class Main {
77

88
/**
9-
* Sets up the drawing, creates the actors, and runs the simulation.
9+
* Sets up the drawing, creates the frame, and plays the game.
1010
*
1111
* @param args command-line arguments
1212
*/
1313
public static void main(String[] args) {
1414

1515
// set up the grid and initialize cells
1616
Grid grid = new Grid(30, 25, 20);
17-
grid.flip(1, 2);
18-
grid.flip(2, 2);
19-
grid.flip(3, 2);
20-
grid.flip(6, 1);
21-
grid.flip(7, 2);
22-
grid.flip(7, 3);
23-
grid.flip(8, 1);
24-
grid.flip(8, 2);
17+
grid.toggle(1, 2);
18+
grid.toggle(2, 2);
19+
grid.toggle(3, 2);
20+
grid.toggle(6, 1);
21+
grid.toggle(7, 2);
22+
grid.toggle(7, 3);
23+
grid.toggle(8, 1);
24+
grid.toggle(8, 2);
2525

2626
// set up the window frame
2727
JFrame frame = new JFrame("Drawing");

0 commit comments

Comments
 (0)