Skip to content

Commit 6a7b08b

Browse files
committed
initial code for ch16
1 parent 74e9a59 commit 6a7b08b

3 files changed

Lines changed: 286 additions & 0 deletions

File tree

ch16/Cell.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import java.awt.Color;
2+
import java.awt.Graphics;
3+
4+
/**
5+
* A square at a fixed location that changes color.
6+
*/
7+
public class Cell {
8+
9+
private final int x;
10+
private final int y;
11+
private final int size;
12+
private Color color;
13+
14+
/**
15+
* Constructs a new cell.
16+
*
17+
* @param x the X coordinate
18+
* @param y the Y coordinate
19+
* @param size number of pixels
20+
*/
21+
public Cell(int x, int y, int size) {
22+
this.x = x;
23+
this.y = y;
24+
this.size = size;
25+
this.color = Color.WHITE;
26+
}
27+
28+
/**
29+
* @return true if the cell is alive
30+
*/
31+
public boolean alive() {
32+
return this.color == Color.BLACK;
33+
}
34+
35+
/**
36+
* @return the cell's color
37+
*/
38+
public Color getColor() {
39+
return this.color;
40+
}
41+
42+
/**
43+
* @param color the new color
44+
*/
45+
public void setColor(Color color) {
46+
this.color = color;
47+
}
48+
49+
/**
50+
* Paints the cell on the screen.
51+
*
52+
* @param g graphics context
53+
*/
54+
public void draw(Graphics g) {
55+
g.setColor(this.color);
56+
g.fillRect(this.x + 1, this.y + 1, this.size - 1, this.size - 1);
57+
g.setColor(Color.LIGHT_GRAY);
58+
g.drawRect(this.x, this.y, this.size, this.size);
59+
}
60+
61+
}

ch16/Grid.java

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
import java.awt.Canvas;
2+
import java.awt.Color;
3+
import java.awt.Graphics;
4+
5+
/**
6+
* 2D array of cells.
7+
*/
8+
public class Grid extends Canvas {
9+
10+
private final int rows;
11+
private final int cols;
12+
13+
/** Cells stored in row-major order. */
14+
private Cell[][] array;
15+
16+
/**
17+
* Constructs a drawing of given size.
18+
*
19+
* @param rows number of rows
20+
* @param cols number of columns
21+
* @param size pixels per cell
22+
*/
23+
public Grid(int rows, int cols, int size) {
24+
25+
// build 2D array of cells
26+
this.rows = rows;
27+
this.cols = cols;
28+
this.array = new Cell[rows][cols];
29+
for (int r = 0; r < rows; r++) {
30+
for (int c = 0; c < cols; c++) {
31+
this.array[r][c] = new Cell(c * size, r * size, size);
32+
}
33+
}
34+
35+
// initialize the display
36+
setSize(cols * size, rows * size);
37+
}
38+
39+
/**
40+
* Toggles the cell color.
41+
*
42+
* @param r row index
43+
* @param c column index
44+
*/
45+
public void flip(int r, int c) {
46+
Cell cell = array[r][c];
47+
if (cell.alive()) {
48+
cell.setColor(Color.WHITE);
49+
} else {
50+
cell.setColor(Color.BLACK);
51+
}
52+
}
53+
54+
/**
55+
* Counts the number of live neighbors (without going out of bounds).
56+
*
57+
* @param r row index
58+
* @param c column index
59+
* @return number of live neighbors
60+
*/
61+
public int countLive(int r, int c) {
62+
int count = 0;
63+
64+
// previous row
65+
if (r > 0) {
66+
if (c > 0 && array[r - 1][c - 1].alive()) {
67+
count++;
68+
}
69+
if (array[r - 1][c].alive()) {
70+
count++;
71+
}
72+
if (c < cols - 1 && array[r - 1][c + 1].alive()) {
73+
count++;
74+
}
75+
}
76+
77+
// current row
78+
if (c > 0 && array[r][c - 1].alive()) {
79+
count++;
80+
}
81+
if (c < cols - 1 && array[r][c + 1].alive()) {
82+
count++;
83+
}
84+
85+
// next row
86+
if (r < rows - 1) {
87+
if (c > 0 && array[r + 1][c - 1].alive()) {
88+
count++;
89+
}
90+
if (array[r + 1][c].alive()) {
91+
count++;
92+
}
93+
if (c < cols - 1 && array[r + 1][c + 1].alive()) {
94+
count++;
95+
}
96+
}
97+
98+
return count;
99+
}
100+
101+
/**
102+
* Apply the rules of Conway's Game of Life.
103+
*
104+
* @param cell the cell to update
105+
* @param count number of live neighbors
106+
*/
107+
public void updateCell(Cell cell, int count) {
108+
if (cell.alive()) {
109+
if (count < 2) {
110+
// Any live cell with fewer than two live neighbors dies,
111+
// as if by underpopulation.
112+
cell.setColor(Color.WHITE);
113+
} else if (count > 3) {
114+
// Any live cell with more than three live neighbors dies,
115+
// as if by overpopulation.
116+
cell.setColor(Color.WHITE);
117+
} else {
118+
// Any live cell with two or three live neighbors lives on
119+
// to the next generation.
120+
cell.setColor(Color.BLACK);
121+
}
122+
} else {
123+
if (count == 3) {
124+
// Any dead cell with exactly three live neighbors
125+
// becomes a live cell, as if by reproduction.
126+
cell.setColor(Color.BLACK);
127+
}
128+
}
129+
}
130+
131+
/**
132+
* Simulates one round of Conway's Game of Life.
133+
*/
134+
public void playGame() {
135+
136+
// count neighbors
137+
int[][] counts = new int[rows][cols];
138+
for (int r = 0; r < rows; r++) {
139+
for (int c = 0; c < cols; c++) {
140+
counts[r][c] = countLive(r, c);
141+
}
142+
}
143+
144+
// update cells
145+
for (int r = 0; r < rows; r++) {
146+
for (int c = 0; c < cols; c++) {
147+
Cell cell = array[r][c];
148+
updateCell(cell, counts[r][c]);
149+
}
150+
}
151+
}
152+
153+
/**
154+
* Draws the grid, cell by cell.
155+
*
156+
* @param g graphics context
157+
*/
158+
public void paint(Graphics g) {
159+
for (Cell[] row : array) {
160+
for (Cell cell : row) {
161+
cell.draw(g);
162+
}
163+
}
164+
}
165+
166+
/**
167+
* Overriding this method makes the simulation more smooth, because the
168+
* Canvas does not need to be cleared before redrawing.
169+
*
170+
* @param g graphics context
171+
*/
172+
public void update(Graphics g) {
173+
paint(g);
174+
}
175+
176+
}

ch16/Main.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import javax.swing.JFrame;
2+
3+
/**
4+
* Example simulation of moving objects.
5+
*/
6+
public class Main {
7+
8+
/**
9+
* Sets up the drawing, creates the actors, and runs the simulation.
10+
*
11+
* @param args command-line arguments
12+
*/
13+
public static void main(String[] args) {
14+
15+
// set up the grid and initialize cells
16+
Grid grid = new Grid(30, 25, 20);
17+
grid.flip(1, 2);
18+
grid.flip(2, 2);
19+
grid.flip(3, 2);
20+
grid.flip(6, 1);
21+
grid.flip(7, 2);
22+
grid.flip(7, 3);
23+
grid.flip(8, 1);
24+
grid.flip(8, 2);
25+
26+
// set up the window frame
27+
JFrame frame = new JFrame("Drawing");
28+
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
29+
frame.add(grid);
30+
frame.pack();
31+
frame.setVisible(true);
32+
33+
// main simulation loop
34+
while (true) {
35+
36+
// update the drawing
37+
grid.playGame();
38+
grid.repaint();
39+
40+
// delay the simulation
41+
try {
42+
Thread.sleep(500);
43+
} catch (InterruptedException e) {
44+
// do nothing
45+
}
46+
}
47+
}
48+
49+
}

0 commit comments

Comments
 (0)