Skip to content

Commit be6bb54

Browse files
committed
ch15 revisions
1 parent ce25adb commit be6bb54

6 files changed

Lines changed: 157 additions & 99 deletions

File tree

ch15/Cell.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,5 @@ public void turnOff() {
6666
public void turnOn() {
6767
state = 1;
6868
}
69+
6970
}

ch15/Conway.java

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
import javax.swing.JFrame;
2+
3+
/**
4+
* Conway's Game of Life.
5+
*/
6+
public class Conway {
7+
8+
private GridCanvas grid;
9+
10+
/**
11+
* Creates a grid with two Blinkers.
12+
*/
13+
public Conway() {
14+
grid = new GridCanvas(5, 10, 20);
15+
grid.turnOn(2, 1);
16+
grid.turnOn(2, 2);
17+
grid.turnOn(2, 3);
18+
grid.turnOn(1, 7);
19+
grid.turnOn(2, 7);
20+
grid.turnOn(3, 7);
21+
}
22+
23+
/**
24+
* Counts the number of live neighbors around a cell.
25+
*
26+
* @param r row index
27+
* @param c column index
28+
* @return number of live neighbors
29+
*/
30+
private int countAlive(int r, int c) {
31+
int count = 0;
32+
count += grid.test(r - 1, c - 1);
33+
count += grid.test(r - 1, c);
34+
count += grid.test(r - 1, c + 1);
35+
count += grid.test(r, c - 1);
36+
count += grid.test(r, c + 1);
37+
count += grid.test(r + 1, c - 1);
38+
count += grid.test(r + 1, c);
39+
count += grid.test(r + 1, c + 1);
40+
return count;
41+
}
42+
43+
/**
44+
* Apply the update rules of Conway's Game of Life.
45+
*
46+
* @param cell the cell to update
47+
* @param count number of live neighbors
48+
*/
49+
private static void updateCell(Cell cell, int count) {
50+
if (cell.isOn()) {
51+
if (count < 2 || count > 3) {
52+
// Any live cell with fewer than two live neighbors dies,
53+
// as if by underpopulation.
54+
// Any live cell with more than three live neighbors dies,
55+
// as if by overpopulation.
56+
cell.turnOff();
57+
}
58+
} else {
59+
if (count == 3) {
60+
// Any dead cell with exactly three live neighbors
61+
// becomes a live cell, as if by reproduction.
62+
cell.turnOn();
63+
}
64+
}
65+
}
66+
67+
/**
68+
* Counts the neighbors before changing anything.
69+
*
70+
* @return number of neighbors for each cell
71+
*/
72+
private int[][] countNeighbors() {
73+
int rows = grid.numRows();
74+
int cols = grid.numCols();
75+
76+
int[][] counts = new int[rows][cols];
77+
for (int r = 0; r < rows; r++) {
78+
for (int c = 0; c < cols; c++) {
79+
counts[r][c] = countAlive(r, c);
80+
}
81+
}
82+
return counts;
83+
}
84+
85+
/**
86+
* Updates each cell based on neighbor counts.
87+
*
88+
* @param counts number of neighbors for each cell
89+
*/
90+
private void updateGrid(int[][] counts) {
91+
int rows = grid.numRows();
92+
int cols = grid.numCols();
93+
94+
for (int r = 0; r < rows; r++) {
95+
for (int c = 0; c < cols; c++) {
96+
Cell cell = grid.getCell(r, c);
97+
updateCell(cell, counts[r][c]);
98+
}
99+
}
100+
}
101+
102+
/**
103+
* Simulates one round of Conway's Game of Life.
104+
*/
105+
public void update() {
106+
int[][] counts = countNeighbors();
107+
updateGrid(counts);
108+
}
109+
110+
/**
111+
* The simulation loop.
112+
*
113+
* @param rate frames per second
114+
*/
115+
private void mainloop() {
116+
while (true) {
117+
118+
// update the drawing
119+
this.update();
120+
grid.repaint();
121+
122+
// delay the simulation
123+
try {
124+
Thread.sleep(500);
125+
} catch (InterruptedException e) {
126+
// do nothing
127+
}
128+
}
129+
}
130+
131+
/**
132+
* Creates and runs the simulation.
133+
*
134+
* @param args command-line arguments
135+
*/
136+
public static void main(String[] args) {
137+
String title = "Conway's Game of Life";
138+
Conway game = new Conway();
139+
JFrame frame = new JFrame(title);
140+
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
141+
frame.setResizable(false);
142+
frame.add(game.grid);
143+
frame.pack();
144+
frame.setVisible(true);
145+
game.mainloop();
146+
}
147+
148+
}

ch15/empty.cells

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

ch15/pulsar.cells

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

ch16/Automaton.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
*/
77
public abstract class Automaton {
88

9-
public static final int SIZE = 20;
10-
119
protected GridCanvas grid;
1210

1311
/**

ch16/Conway.java

Lines changed: 8 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,19 @@
1-
import java.io.File;
2-
import java.io.FileNotFoundException;
3-
import java.util.ArrayList;
4-
import java.util.Scanner;
5-
61
/**
72
* Conway's Game of Life.
83
*/
94
public class Conway extends Automaton {
105

116
/**
12-
* Creates a grid with a Blinker and a Glider.
7+
* Creates a grid with two Blinkers.
138
*/
149
public Conway() {
15-
grid = new GridCanvas(30, 25, SIZE);
16-
grid.turnOn(1, 2);
10+
grid = new GridCanvas(5, 10, 20);
11+
grid.turnOn(2, 1);
1712
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);
24-
}
25-
26-
/**
27-
* Creates a grid based on a plain text file.
28-
* http://www.conwaylife.com/wiki/Plaintext
29-
*
30-
* @param path the path to the file
31-
* @param margin how many cells to add
32-
*/
33-
public Conway(String path, int margin) {
34-
35-
// open the file at the given path
36-
Scanner scan = null;
37-
try {
38-
File file = new File(path);
39-
scan = new Scanner(file);
40-
} catch (FileNotFoundException e) {
41-
e.printStackTrace();
42-
System.exit(1);
43-
}
44-
45-
// read file contents into memory
46-
ArrayList<String> data = new ArrayList<String>();
47-
while (scan.hasNextLine()) {
48-
String line = scan.nextLine();
49-
// only add non-comment lines
50-
if (!line.startsWith("!")) {
51-
data.add(line);
52-
}
53-
}
54-
55-
// determine number of rows and columns in the pattern
56-
int rows = data.size();
57-
int cols = 0;
58-
for (String line : data) {
59-
if (cols < line.length()) {
60-
cols = line.length();
61-
}
62-
}
63-
if (rows == 0 || cols == 0) {
64-
throw new IllegalArgumentException("no cells found");
65-
}
66-
67-
// create the resulting grid with margin of extra cells
68-
grid = new GridCanvas(rows + 2 * margin, cols + 2 * margin, SIZE);
69-
for (int r = 0; r < rows; r++) {
70-
String line = data.get(r);
71-
for (int c = 0; c < line.length(); c++) {
72-
char x = line.charAt(c);
73-
if (x == 'O') {
74-
grid.turnOn(r + margin, c + margin);
75-
}
76-
}
77-
}
13+
grid.turnOn(2, 3);
14+
grid.turnOn(1, 7);
15+
grid.turnOn(2, 7);
16+
grid.turnOn(3, 7);
7817
}
7918

8019
/**
@@ -171,7 +110,7 @@ public void update() {
171110
*/
172111
public static void main(String[] args) {
173112
String title = "Conway's Game of Life";
174-
Conway game = new Conway("pulsar.cells", 2);
113+
Conway game = new Conway();
175114
game.run(title, 2);
176115
}
177116

0 commit comments

Comments
 (0)