Skip to content

Commit bb437e3

Browse files
committed
reviewed ch15 code
1 parent 445db0e commit bb437e3

6 files changed

Lines changed: 38 additions & 52 deletions

File tree

ch15/Cell.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,14 @@ public void draw(Graphics g) {
4141
}
4242

4343
/**
44-
* @return true if the cell is OFF
44+
* @return true if the color is OFF
4545
*/
4646
public boolean isOff() {
4747
return this.color == OFF;
4848
}
4949

5050
/**
51-
* @return true if the cell is ON
51+
* @return true if the color is ON
5252
*/
5353
public boolean isOn() {
5454
return this.color == ON;

ch15/Conway.java

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import java.io.File;
22
import java.io.FileNotFoundException;
33
import java.util.ArrayList;
4-
import java.util.List;
54
import java.util.Scanner;
65

76
/**
@@ -29,8 +28,9 @@ public Conway() {
2928
* http://www.conwaylife.com/wiki/Plaintext
3029
*
3130
* @param path the path to the file
31+
* @param margin how many cells to add
3232
*/
33-
public Conway(String path) {
33+
public Conway(String path, int margin) {
3434

3535
// open the file at the given path
3636
Scanner scan = null;
@@ -43,35 +43,35 @@ public Conway(String path) {
4343
}
4444

4545
// read file contents into memory
46-
List<String> data = new ArrayList<String>();
46+
ArrayList<String> data = new ArrayList<String>();
4747
while (scan.hasNextLine()) {
4848
String line = scan.nextLine();
49-
// ignore blank lines and comments
50-
if (!line.isEmpty() && !line.startsWith("!")) {
49+
// only add non-comment lines
50+
if (!line.startsWith("!")) {
5151
data.add(line);
5252
}
5353
}
5454

55-
// validate the file contents
55+
// determine number of rows and columns in the pattern
5656
int rows = data.size();
57-
if (rows == 0) {
58-
throw new IllegalArgumentException("empty file");
59-
}
60-
int cols = data.get(0).length();
57+
int cols = 0;
6158
for (String line : data) {
62-
if (line.length() != cols) {
63-
throw new IllegalArgumentException("invalid file");
59+
if (cols < line.length()) {
60+
cols = line.length();
6461
}
6562
}
63+
if (rows == 0 || cols == 0) {
64+
throw new IllegalArgumentException("no cells found");
65+
}
6666

67-
// create the resulting grid
68-
grid = new GridCanvas(rows, cols, SIZE);
67+
// create the resulting grid with margin of extra cells
68+
grid = new GridCanvas(rows + 2 * margin, cols + 2 * margin, SIZE);
6969
for (int r = 0; r < rows; r++) {
7070
String line = data.get(r);
71-
for (int c = 0; c < cols; c++) {
71+
for (int c = 0; c < line.length(); c++) {
7272
char x = line.charAt(c);
7373
if (x == 'O') {
74-
grid.flip(r, c);
74+
grid.flip(r + margin, c + margin);
7575
}
7676
}
7777
}

ch15/GridCanvas.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import java.awt.Graphics;
33

44
/**
5-
* 2D array of cells representing a game board.
5+
* 2D array of cells representing a rectangular grid.
66
*/
77
public class GridCanvas extends Canvas {
88

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
/**
55
* Application that simulates a cellular automaton.
66
*/
7-
public class Simulation {
7+
public class Main {
88

99
/**
1010
* Sets up the grid, creates the frame, and plays the game.
@@ -14,7 +14,7 @@ public class Simulation {
1414
public static void main(String[] args) {
1515

1616
String title = "Conway's Game of Life";
17-
Automaton game = new Conway("bakersdozen.cells");
17+
Automaton game = new Conway("pulsar.cells", 2);
1818

1919
// String title = "Langton's Ant";
2020
// Automaton game = new Langton();

ch15/bakersdozen.cells

Lines changed: 0 additions & 31 deletions
This file was deleted.

ch15/pulsar.cells

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
!Name: Pulsar
2+
!Author: John Conway
3+
!Despite its size, this is the fourth most common oscillator (and by far the most common of period greater than 2).
4+
!www.conwaylife.com/wiki/index.php?title=Pulsar
5+
..OOO...OOO
6+
7+
O....O.O....O
8+
O....O.O....O
9+
O....O.O....O
10+
..OOO...OOO
11+
12+
..OOO...OOO
13+
O....O.O....O
14+
O....O.O....O
15+
O....O.O....O
16+
17+
..OOO...OOO

0 commit comments

Comments
 (0)