Skip to content

Commit c621c0d

Browse files
committed
simulate bakers dozen from file
1 parent f351425 commit c621c0d

4 files changed

Lines changed: 203 additions & 50 deletions

File tree

ch16/Drawing.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import java.awt.Canvas;
2+
import java.awt.Graphics;
3+
4+
/**
5+
* Drawing of a grid.
6+
*/
7+
public class Drawing extends Canvas {
8+
9+
private Grid grid;
10+
11+
/**
12+
* Constructs a drawing for the grid.
13+
*
14+
* @param grid the initialized grid
15+
*/
16+
public Drawing(Grid grid) {
17+
this.grid = grid;
18+
setSize(grid.width(), grid.height());
19+
}
20+
21+
/**
22+
* Draws the grid, cell by cell.
23+
*
24+
* @param g graphics context
25+
*/
26+
@Override
27+
public void paint(Graphics g) {
28+
grid.draw(g);
29+
}
30+
31+
/**
32+
* Overriding this method helps the simulation run more smoothly. Normally
33+
* the Canvas is cleared before painting, but there is no need to clear it
34+
* since paint draws the entire grid.
35+
*
36+
* @param g graphics context
37+
*/
38+
@Override
39+
public void update(Graphics g) {
40+
grid.draw(g);
41+
}
42+
43+
}

ch16/Grid.java

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import java.awt.Canvas;
21
import java.awt.Graphics;
32

43
/**
5-
* Drawing of a 2D array of cells.
4+
* 2D array of cells that represent Conway's Game of Life.
65
*/
7-
public class Grid extends Canvas {
6+
public class Grid {
87

98
private final int rows;
109
private final int cols;
10+
private final int size;
1111

1212
/** Cells stored in row-major order. */
1313
private Cell[][] array;
@@ -21,18 +21,31 @@ public class Grid extends Canvas {
2121
*/
2222
public Grid(int rows, int cols, int size) {
2323

24-
// build 2D array of cells
24+
// store the configuration
2525
this.rows = rows;
2626
this.cols = cols;
27+
this.size = size;
28+
29+
// build 2D array of cells
2730
this.array = new Cell[rows][cols];
2831
for (int r = 0; r < rows; r++) {
2932
for (int c = 0; c < cols; c++) {
3033
this.array[r][c] = new Cell(c * size, r * size, size);
3134
}
3235
}
36+
}
3337

34-
// initialize the display
35-
setSize(cols * size, rows * size);
38+
/**
39+
* Paints the grid on the screen.
40+
*
41+
* @param g graphics context
42+
*/
43+
public void draw(Graphics g) {
44+
for (Cell[] row : array) {
45+
for (Cell cell : row) {
46+
cell.draw(g);
47+
}
48+
}
3649
}
3750

3851
/**
@@ -41,7 +54,7 @@ public Grid(int rows, int cols, int size) {
4154
* @param r row index
4255
* @param c column index
4356
*/
44-
public void toggle(int r, int c) {
57+
public void flip(int r, int c) {
4558
Cell cell = array[r][c];
4659
if (cell.isOff()) {
4760
cell.turnOn();
@@ -50,6 +63,20 @@ public void toggle(int r, int c) {
5063
}
5164
}
5265

66+
/**
67+
* @return total height of the grid
68+
*/
69+
public int height() {
70+
return rows * size;
71+
}
72+
73+
/**
74+
* @return total width of the grid
75+
*/
76+
public int width() {
77+
return cols * size;
78+
}
79+
5380
/**
5481
* Counts the number of live neighbors (without going out of bounds).
5582
*
@@ -130,7 +157,7 @@ public void updateCell(Cell cell, int count) {
130157
/**
131158
* Simulates one round of Conway's Game of Life.
132159
*/
133-
public void playGame() {
160+
public void update() {
134161

135162
// count neighbors before changing anything
136163
int[][] counts = new int[rows][cols];
@@ -148,30 +175,4 @@ public void playGame() {
148175
}
149176
}
150177

151-
/**
152-
* Draws the grid, cell by cell.
153-
*
154-
* @param g graphics context
155-
*/
156-
@Override
157-
public void paint(Graphics g) {
158-
for (Cell[] row : array) {
159-
for (Cell cell : row) {
160-
cell.draw(g);
161-
}
162-
}
163-
}
164-
165-
/**
166-
* Overriding this method helps the simulation run more smoothly. Normally
167-
* the Canvas is cleared before painting, but there is no need to clear it
168-
* since paint draws the entire grid.
169-
*
170-
* @param g graphics context
171-
*/
172-
@Override
173-
public void update(Graphics g) {
174-
paint(g);
175-
}
176-
177178
}

ch16/Main.java

Lines changed: 94 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,119 @@
1+
import java.io.File;
2+
import java.io.FileNotFoundException;
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.Scanner;
16
import javax.swing.JFrame;
27

38
/**
49
* Example simulation of Conway's Game of Life.
510
*/
611
public class Main {
712

13+
public static final int SIZE = 20;
14+
815
/**
9-
* Sets up the drawing, creates the frame, and plays the game.
16+
* Returns an example of a Blinker and a Glider.
17+
*
18+
* @return hard-coded grid
19+
*/
20+
public static Grid example() {
21+
Grid grid = new Grid(30, 25, SIZE);
22+
grid.flip(1, 2);
23+
grid.flip(2, 2);
24+
grid.flip(3, 2);
25+
grid.flip(6, 1);
26+
grid.flip(7, 2);
27+
grid.flip(7, 3);
28+
grid.flip(8, 1);
29+
grid.flip(8, 2);
30+
return grid;
31+
}
32+
33+
/**
34+
* Creates a grid based on a plain text file.
35+
* http://www.conwaylife.com/wiki/Plaintext
36+
*
37+
* @param path the path to the file
38+
* @return grid based on file contents
39+
*/
40+
public static Grid readFile(String path) {
41+
42+
// open the file at the given path
43+
Scanner scan = null;
44+
try {
45+
File file = new File(path);
46+
scan = new Scanner(file);
47+
} catch (FileNotFoundException e) {
48+
e.printStackTrace();
49+
System.exit(1);
50+
}
51+
52+
// read file contents into memory
53+
List<String> data = new ArrayList<String>();
54+
while (scan.hasNextLine()) {
55+
String line = scan.nextLine();
56+
// ignore blank lines and comments
57+
if (!line.isEmpty() && !line.startsWith("!")) {
58+
data.add(line);
59+
}
60+
}
61+
62+
// validate the file contents
63+
int rows = data.size();
64+
if (rows == 0) {
65+
throw new IllegalArgumentException("empty file");
66+
}
67+
int cols = data.get(0).length();
68+
for (String line : data) {
69+
if (line.length() != cols) {
70+
throw new IllegalArgumentException("invalid file");
71+
}
72+
}
73+
74+
// create the resulting grid
75+
Grid grid = new Grid(rows, cols, SIZE);
76+
for (int r = 0; r < rows; r++) {
77+
String line = data.get(r);
78+
for (int c = 0; c < cols; c++) {
79+
char x = line.charAt(c);
80+
if (x == 'O') {
81+
grid.flip(r, c);
82+
}
83+
}
84+
}
85+
return grid;
86+
}
87+
88+
/**
89+
* Sets up the grid, creates the drawing, and plays the game.
1090
*
1191
* @param args command-line arguments
1292
*/
1393
public static void main(String[] args) {
1494

15-
// set up the grid and initialize cells
16-
Grid grid = new Grid(30, 25, 20);
17-
grid.toggle(1, 2);
18-
grid.toggle(2, 2);
19-
grid.toggle(3, 2);
20-
grid.toggle(6, 1);
21-
grid.toggle(7, 2);
22-
grid.toggle(7, 3);
23-
grid.toggle(8, 1);
24-
grid.toggle(8, 2);
25-
26-
// set up the window frame
95+
// create the grid
96+
Grid grid;
97+
if (args.length != 1) {
98+
grid = example();
99+
} else {
100+
grid = readFile(args[0]);
101+
}
102+
103+
// set up the drawing
104+
Drawing drawing = new Drawing(grid);
27105
JFrame frame = new JFrame("Drawing");
28106
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
29-
frame.add(grid);
107+
frame.add(drawing);
30108
frame.pack();
31109
frame.setVisible(true);
32110

33111
// main simulation loop
34112
while (true) {
35113

36114
// update the drawing
37-
grid.playGame();
38-
grid.repaint();
115+
grid.update();
116+
drawing.repaint();
39117

40118
// delay the simulation
41119
try {

ch16/bakersdozen.cells

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
! bakersdozenreactions.cells
2+
! http://conwaylife.com/wiki/Baker%27s_dozen
3+
! http://conwaylife.com/patterns/bakersdozenreactions.cells
4+
.........................O.........
5+
........................OO.OO......
6+
........................OOOOO......
7+
........O.OO.OO.O.......OO.........
8+
........OO.O.O.OO........O.........
9+
...................................
10+
........OOO...OOO......O.O.........
11+
........O..O.O..O.......O..........
12+
..........OO.OO.............O......
13+
.....O.............O.......O.O..OO.
14+
OO..O.O...........O.O..OO..O..O.OO.
15+
OO.O..O..OO...OO..O..O.OO...OO.....
16+
....OO...OO...OO...OO..............
17+
...................................
18+
...............................O...
19+
...............................O...
20+
....OO...OO.....OO...OO.......OO.O.
21+
OO.O..O..OO.....OO..O..O.OO......OO
22+
OO..O.O.............O.O..OO...O....
23+
.....O...............O........OO...
24+
.............................OO....
25+
........OOO.......O..........OOO...
26+
........OOO.......O................
27+
......OO..OOO...O.OO...............
28+
......OO.......OO...O..............
29+
......OOO..........O.O.............
30+
........O.........O..O.............
31+
........O..........OO..............

0 commit comments

Comments
 (0)