Skip to content

Commit 096ebdd

Browse files
committed
Simplify Langton.update
1 parent 3032b6f commit 096ebdd

4 files changed

Lines changed: 20 additions & 31 deletions

File tree

ch15/Conway.java

Lines changed: 10 additions & 9 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.flip(1, 2);
17-
grid.flip(2, 2);
18-
grid.flip(3, 2);
19-
grid.flip(6, 1);
20-
grid.flip(7, 2);
21-
grid.flip(7, 3);
22-
grid.flip(8, 1);
23-
grid.flip(8, 2);
16+
grid.getCell(1, 2).turnOn();
17+
grid.getCell(2, 2).turnOn();
18+
grid.getCell(3, 2).turnOn();
19+
grid.getCell(6, 1).turnOn();
20+
grid.getCell(7, 2).turnOn();
21+
grid.getCell(7, 3).turnOn();
22+
grid.getCell(8, 1).turnOn();
23+
grid.getCell(8, 2).turnOn();
2424
}
2525

2626
/**
@@ -71,7 +71,8 @@ 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.flip(r + margin, c + margin);
74+
Cell cell = grid.getCell(r + margin, c + margin);
75+
cell.turnOn();
7576
}
7677
}
7778
}

ch15/GridCanvas.java

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@ public GridCanvas(int rows, int cols, int size) {
2121
// build 2D array of cells
2222
array = new Cell[rows][cols];
2323
for (int r = 0; r < rows; r++) {
24+
int y = r * size;
2425
for (int c = 0; c < cols; c++) {
25-
array[r][c] = new Cell(c * size, r * size, size);
26+
int x = c * size;
27+
array[r][c] = new Cell(x, y, size);
2628
}
2729
}
2830

29-
// set up the canvas drawing
31+
// set the canvas size
3032
setSize(cols * size, rows * size);
3133
}
3234

@@ -53,17 +55,6 @@ public Cell getCell(int r, int c) {
5355
return array[r][c];
5456
}
5557

56-
/**
57-
* Toggles the cell on/off.
58-
*
59-
* @param r row index
60-
* @param c column index
61-
*/
62-
public void flip(int r, int c) {
63-
Cell cell = array[r][c];
64-
cell.flip();
65-
}
66-
6758
/**
6859
* Returns 1 if the cell at (r, c) exists and is on. Returns 0 if the cell
6960
* doesn't exist or is off.

ch15/Langton.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,11 @@ public void update() {
2626
if (cell.isOn()) {
2727
// at a black square; flip color and turn left
2828
cell.turnOff();
29-
head -= 1;
30-
if (head < 0) {
31-
head = 3;
32-
}
29+
head = (head + 3) % 4;
3330
} else {
3431
// at a white square; flip color and turn right
3532
cell.turnOn();
36-
head += 1;
37-
if (head > 3) {
38-
head = 0;
39-
}
33+
head = (head + 1) % 4;
4034
}
4135

4236
// move forward one unit
@@ -57,6 +51,8 @@ public void update() {
5751

5852
// update the display
5953
grid.repaint();
54+
55+
// TODO: draw a triangle to show the ant
6056
}
6157

6258
}

ch15/Main.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ public class Main {
1313
*/
1414
public static void main(String[] args) {
1515
String title = "Conway's Game of Life";
16-
Automaton game = new Conway("pulsar.cells", 2);
16+
Automaton game = new Conway();
17+
// Automaton game = new Conway("pulsar.cells", 2);
1718
runSimulation(title, game, 500);
1819
}
1920

0 commit comments

Comments
 (0)