Skip to content

Commit c32030b

Browse files
committed
minor changes based on narrative
1 parent 543b43a commit c32030b

4 files changed

Lines changed: 28 additions & 22 deletions

File tree

ch15/Cell.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public Cell(int x, int y, int size) {
3434
* @param g graphics context
3535
*/
3636
public void draw(Graphics g) {
37-
g.setColor(color);
37+
g.setColor(this.color);
3838
g.fillRect(x + 1, y + 1, size - 1, size - 1);
3939
g.setColor(Color.LIGHT_GRAY);
4040
g.drawRect(x, y, size, size);

ch15/GridCanvas.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public int test(int r, int c) {
7878
if (array[r][c].isOn()) {
7979
return 1;
8080
}
81-
} catch (IndexOutOfBoundsException e) {
81+
} catch (ArrayIndexOutOfBoundsException e) {
8282
// cell doesn't exist
8383
}
8484
return 0;

ch15/Langton.java

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,30 +26,25 @@ public Langton(int rows, int cols) {
2626
public void update() {
2727

2828
Cell cell = grid.cellAt(xpos, ypos);
29-
if (cell.isOn()) {
30-
// at a black square; flip color and turn left
31-
cell.turnOff();
32-
head = (head + 3) % 4;
33-
} else {
34-
// at a white square; flip color and turn right
35-
cell.turnOn();
29+
if (cell.isOff()) {
30+
// at a white square; turn right and flip color
3631
head = (head + 1) % 4;
32+
cell.turnOn();
33+
} else {
34+
// at a black square; turn left and flip color
35+
head = (head + 3) % 4;
36+
cell.turnOff();
3737
}
3838

3939
// move forward one unit
40-
switch (head) {
41-
case 0:
42-
ypos -= 1;
43-
break;
44-
case 1:
45-
xpos += 1;
46-
break;
47-
case 2:
48-
ypos += 1;
49-
break;
50-
case 3:
51-
xpos -= 1;
52-
break;
40+
if (head == 0) {
41+
ypos -= 1;
42+
} else if (head == 1) {
43+
xpos += 1;
44+
} else if (head == 2) {
45+
ypos += 1;
46+
} else {
47+
xpos -= 1;
5348
}
5449

5550
// TODO: draw a triangle to show the ant

ch15/empty.cells

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
!An empty 10x10 grid
2+
..........
3+
..........
4+
..........
5+
..........
6+
..........
7+
..........
8+
..........
9+
..........
10+
..........
11+
..........

0 commit comments

Comments
 (0)