Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit de389a0

Browse filesBrowse files
committed
sync with ch15 revisions
1 parent 869bade commit de389a0
Copy full SHA for de389a0

File tree

Expand file treeCollapse file tree

4 files changed

+49
-22
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

4 files changed

+49
-22
lines changed
Open diff view settings
Collapse file

‎ch15/Cell.java‎

Copy file name to clipboardExpand all lines: ch15/Cell.java
+2-3Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77
public class Cell {
88

9-
public static final Color colors[] = {Color.WHITE, Color.BLACK};
9+
public static final Color[] COLORS = {Color.WHITE, Color.BLACK};
1010

1111
private final int x;
1212
private final int y;
@@ -33,8 +33,7 @@ public Cell(int x, int y, int size) {
3333
* @param g graphics context
3434
*/
3535
public void draw(Graphics g) {
36-
Color color = colors[this.state];
37-
g.setColor(color);
36+
g.setColor(COLORS[this.state]);
3837
g.fillRect(x + 1, y + 1, size - 1, size - 1);
3938
g.setColor(Color.LIGHT_GRAY);
4039
g.drawRect(x, y, size, size);
Collapse file

‎ch15/Conway.java‎

Copy file name to clipboardExpand all lines: ch15/Conway.java
+32-13Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ public class Conway extends Automaton {
1313
*/
1414
public Conway() {
1515
grid = new GridCanvas(30, 25, SIZE);
16-
grid.turnCellOn(1, 2);
17-
grid.turnCellOn(2, 2);
18-
grid.turnCellOn(3, 2);
19-
grid.turnCellOn(6, 1);
20-
grid.turnCellOn(7, 2);
21-
grid.turnCellOn(7, 3);
22-
grid.turnCellOn(8, 1);
23-
grid.turnCellOn(8, 2);
16+
grid.turnOn(1, 2);
17+
grid.turnOn(2, 2);
18+
grid.turnOn(3, 2);
19+
grid.turnOn(6, 1);
20+
grid.turnOn(7, 2);
21+
grid.turnOn(7, 3);
22+
grid.turnOn(8, 1);
23+
grid.turnOn(8, 2);
2424
}
2525

2626
/**
@@ -71,7 +71,7 @@ public Conway(String path, int margin) {
7171
for (int c = 0; c < line.length(); c++) {
7272
char x = line.charAt(c);
7373
if (x == 'O') {
74-
grid.getCell(r + margin, c + margin).turnOn();
74+
grid.turnOn(r + margin, c + margin);
7575
}
7676
}
7777
}
@@ -122,21 +122,32 @@ private static void updateCell(Cell cell, int count) {
122122
}
123123

124124
/**
125-
* Simulates one round of Conway's Game of Life.
125+
* Counts the neighbors before changing anything.
126+
*
127+
* @return number of neighbors for each cell
126128
*/
127-
public void update() {
129+
private int[][] countNeighbors() {
128130
int rows = grid.numRows();
129131
int cols = grid.numCols();
130132

131-
// count neighbors before changing anything
132133
int[][] counts = new int[rows][cols];
133134
for (int r = 0; r < rows; r++) {
134135
for (int c = 0; c < cols; c++) {
135136
counts[r][c] = countAlive(r, c);
136137
}
137138
}
139+
return counts;
140+
}
141+
142+
/**
143+
* Updates each cell based on neighbor counts.
144+
*
145+
* @param counts number of neighbors for each cell
146+
*/
147+
private void updateGrid(int[][] counts) {
148+
int rows = grid.numRows();
149+
int cols = grid.numCols();
138150

139-
// update each cell based on neighbor counts
140151
for (int r = 0; r < rows; r++) {
141152
for (int c = 0; c < cols; c++) {
142153
Cell cell = grid.getCell(r, c);
@@ -145,6 +156,14 @@ public void update() {
145156
}
146157
}
147158

159+
/**
160+
* Simulates one round of Conway's Game of Life.
161+
*/
162+
public void update() {
163+
int[][] counts = countNeighbors();
164+
updateGrid(counts);
165+
}
166+
148167
/**
149168
* Creates and runs the simulation.
150169
*
Collapse file

‎ch15/GridCanvas.java‎

Copy file name to clipboardExpand all lines: ch15/GridCanvas.java
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public Cell getCell(int r, int c) {
6161
* @param r row index
6262
* @param c column index
6363
*/
64-
public void turnCellOn(int r, int c) {
64+
public void turnOn(int r, int c) {
6565
array[r][c].turnOn();
6666
}
6767

Collapse file

‎ch15/Langton.java‎

Copy file name to clipboardExpand all lines: ch15/Langton.java
+14-5Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,9 @@ public Langton(int rows, int cols) {
2121
}
2222

2323
/**
24-
* Simulates one round of Langton's Ant.
24+
* Flip the color of the current cell.
2525
*/
26-
public void update() {
27-
26+
private void flipCell() {
2827
Cell cell = grid.getCell(xpos, ypos);
2928
if (cell.isOff()) {
3029
// at a white square; turn right and flip color
@@ -35,8 +34,12 @@ public void update() {
3534
head = (head + 3) % 4;
3635
cell.turnOff();
3736
}
37+
}
3838

39-
// move forward one unit
39+
/**
40+
* Move the ant forward one unit.
41+
*/
42+
private void moveAnt() {
4043
if (head == 0) {
4144
ypos -= 1;
4245
} else if (head == 1) {
@@ -46,8 +49,14 @@ public void update() {
4649
} else {
4750
xpos -= 1;
4851
}
52+
}
4953

50-
// TODO: draw a triangle to show the ant
54+
/**
55+
* Simulates one round of Langton's Ant.
56+
*/
57+
public void update() {
58+
flipCell();
59+
moveAnt();
5160
}
5261

5362
/**

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.