Skip to content

Commit fb040b1

Browse files
committed
removed Main; refactored ch15
1 parent 192f1e7 commit fb040b1

7 files changed

Lines changed: 95 additions & 117 deletions

File tree

ch15/Automaton.java

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import javax.swing.JFrame;
2+
13
/**
24
* A cellular automaton consists of a grid of cells and follows a set of rules
35
* that update the grid over time.
@@ -9,15 +11,39 @@ public abstract class Automaton {
911
protected GridCanvas grid;
1012

1113
/**
12-
* @return the grid
14+
* Applies rules to update the grid.
1315
*/
14-
public GridCanvas getGrid() {
15-
return grid;
16-
}
16+
public abstract void update();
1717

1818
/**
19-
* Updates the automaton.
19+
* Creates a JFrame and runs the automaton.
20+
*
21+
* @param title the frame title
22+
* @param rate frames per second
2023
*/
21-
public abstract void update();
24+
public void run(String title, int rate) {
25+
// set up the window frame
26+
JFrame frame = new JFrame(title);
27+
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
28+
frame.setResizable(false);
29+
frame.add(this.grid);
30+
frame.pack();
31+
frame.setVisible(true);
32+
33+
// main simulation loop
34+
while (true) {
35+
36+
// update the drawing
37+
this.update();
38+
grid.repaint();
39+
40+
// delay the simulation
41+
try {
42+
Thread.sleep(1000 / rate);
43+
} catch (InterruptedException e) {
44+
// do nothing
45+
}
46+
}
47+
}
2248

2349
}

ch15/Cell.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,4 @@ public void turnOn() {
6868
color = ON;
6969
}
7070

71-
public void flip() {
72-
if (isOn()) {
73-
turnOff();
74-
} else {
75-
turnOn();
76-
}
77-
}
78-
7971
}

ch15/Conway.java

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

2626
/**
@@ -71,8 +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-
Cell cell = grid.getCell(r + margin, c + margin);
75-
cell.turnOn();
74+
grid.init(r + margin, c + margin);
7675
}
7776
}
7877
}
@@ -87,13 +86,14 @@ public Conway(String path, int margin) {
8786
*/
8887
private int countAlive(int r, int c) {
8988
int count = 0;
90-
for (int i=-1; i<=1; i++) {
91-
for (int j=-1; j<=1; j++) {
92-
if (i!=0 || j!=0) {
93-
count += grid.test(r+i, c+j);
94-
}
95-
}
96-
}
89+
count += grid.test(r - 1, c - 1);
90+
count += grid.test(r - 1, c);
91+
count += grid.test(r - 1, c + 1);
92+
count += grid.test(r, c - 1);
93+
count += grid.test(r, c + 1);
94+
count += grid.test(r + 1, c - 1);
95+
count += grid.test(r + 1, c);
96+
count += grid.test(r + 1, c + 1);
9797
return count;
9898
}
9999

@@ -108,7 +108,7 @@ private static void updateCell(Cell cell, int count) {
108108
if (count < 2 || count > 3) {
109109
// Any live cell with fewer than two live neighbors dies,
110110
// as if by underpopulation.
111-
// Any live cell with more than three live neighbors dies,
111+
// Any live cell with more than three live neighbors dies,
112112
// as if by overpopulation.
113113
cell.turnOff();
114114
}
@@ -139,13 +139,21 @@ public void update() {
139139
// update each cell based on neighbor counts
140140
for (int r = 0; r < rows; r++) {
141141
for (int c = 0; c < cols; c++) {
142-
final Cell cell = grid.getCell(r, c);
143-
updateCell(cell, counts[r][c]);
142+
Cell cell = grid.cellAt(r, c);
143+
updateCell(cell, counts[r][c]);
144144
}
145145
}
146+
}
146147

147-
// update the display
148-
grid.repaint();
148+
/**
149+
* Creates and runs the simulation.
150+
*
151+
* @param args command-line arguments
152+
*/
153+
public static void main(String[] args) {
154+
String title = "Conway's Game of Life";
155+
Conway game = new Conway("pulsar.cells", 2);
156+
game.run(title, 2);
149157
}
150158

151159
}

ch15/GridCanvas.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ 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;
24+
int y = r * size;
2525
for (int c = 0; c < cols; c++) {
26-
int x = c * size;
26+
int x = c * size;
2727
array[r][c] = new Cell(x, y, size);
2828
}
2929
}
@@ -51,10 +51,20 @@ public int numCols() {
5151
* @param c column index
5252
* @return the cell
5353
*/
54-
public Cell getCell(int r, int c) {
54+
public Cell cellAt(int r, int c) {
5555
return array[r][c];
5656
}
5757

58+
/**
59+
* Convenience method that turns on the cell at (r, c).
60+
*
61+
* @param r row index
62+
* @param c column index
63+
*/
64+
public void init(int r, int c) {
65+
array[r][c].turnOn();
66+
}
67+
5868
/**
5969
* Returns 1 if the cell at (r, c) exists and is on. Returns 0 if the cell
6070
* doesn't exist or is off.
@@ -106,4 +116,5 @@ public void paint(Graphics g) {
106116
public void update(Graphics g) {
107117
draw(g);
108118
}
119+
109120
}

ch15/Langton.java

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@ public class Langton extends Automaton {
99

1010
/**
1111
* Creates a grid with the ant in the center.
12+
*
13+
* @param rows number of rows
14+
* @param cols number of columns
1215
*/
13-
public Langton(int nrows, int ncols) {
14-
grid = new GridCanvas(nrows, ncols, 10);
15-
xpos = nrows / 2;
16-
ypos = ncols / 2;
16+
public Langton(int rows, int cols) {
17+
grid = new GridCanvas(rows, cols, 10);
18+
xpos = rows / 2;
19+
ypos = cols / 2;
1720
head = 0;
1821
}
1922

@@ -22,7 +25,7 @@ public Langton(int nrows, int ncols) {
2225
*/
2326
public void update() {
2427

25-
Cell cell = grid.getCell(xpos, ypos);
28+
Cell cell = grid.cellAt(xpos, ypos);
2629
if (cell.isOn()) {
2730
// at a black square; flip color and turn left
2831
cell.turnOff();
@@ -49,10 +52,18 @@ public void update() {
4952
break;
5053
}
5154

52-
// update the display
53-
grid.repaint();
54-
5555
// TODO: draw a triangle to show the ant
5656
}
5757

58+
/**
59+
* Creates and runs the simulation.
60+
*
61+
* @param args command-line arguments
62+
*/
63+
public static void main(String[] args) {
64+
String title = "Langton's Ant";
65+
Langton game = new Langton(61, 61);
66+
game.run(title, 750);
67+
}
68+
5869
}

ch15/Main.java

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

ch15/Main2.java

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

0 commit comments

Comments
 (0)