Skip to content

Commit 57886d5

Browse files
committed
1 more chapter to go...
I was expecting this book to be just a "hey this is how you make things in Java so here you go" but it was quite interesting and it challenged me a few times. What started as me wanting to have something to go over when I have time has resulted in me finding programming more fun and rewarding.
1 parent 3462ba8 commit 57886d5

7 files changed

Lines changed: 359 additions & 20 deletions

File tree

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package chapter16exercises;
2+
3+
import javax.swing.JFrame;
4+
5+
public abstract class Automaton {
6+
7+
protected GridCanvas grid;
8+
9+
public abstract void update();
10+
11+
private void mainloop(int rate) {
12+
while (true) {
13+
14+
this.update();
15+
grid.repaint();
16+
17+
try {
18+
Thread.sleep(1000 / rate);
19+
} catch (InterruptedException e) {
20+
}
21+
}
22+
}
23+
24+
public void run(String title, int rate) {
25+
JFrame frame = new JFrame(title);
26+
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
27+
frame.setResizable(false);
28+
frame.add(this.grid);
29+
frame.pack();
30+
frame.setVisible(true);
31+
this.mainloop(rate);
32+
}
33+
34+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package chapter16exercises;
2+
3+
public class BriansBrain extends Automaton {
4+
5+
public BriansBrain(int rows, int cols, int cellSize) {
6+
grid = new GridCanvas(rows, cols, cellSize);
7+
}
8+
9+
@Override
10+
public void update() {
11+
int rows = grid.numRows();
12+
int cols = grid.numCols();
13+
int[][] newStates = new int[rows][cols];
14+
15+
for (int r = 0; r < rows; r++) {
16+
for (int c = 0; c < cols; c++) {
17+
Cell cell = grid.getCell(r, c);
18+
int neighbors = countAliveNeighbors(r, c);
19+
20+
if (cell.isOn()) {
21+
newStates[r][c] = 2;
22+
} else if (cell.isOff() && neighbors == 2) {
23+
newStates[r][c] = 1;
24+
} else if (cell.isDying()) {
25+
newStates[r][c] = 0;
26+
} else {
27+
newStates[r][c] = cell.getState();
28+
}
29+
}
30+
}
31+
32+
for (int r = 0; r < rows; r++) {
33+
for (int c = 0; c < cols; c++) {
34+
Cell cell = grid.getCell(r, c);
35+
cell.setState(newStates[r][c]);
36+
}
37+
}
38+
}
39+
40+
private int countAliveNeighbors(int r, int c) {
41+
int count = 0;
42+
count += grid.test(r - 1, c - 1);
43+
count += grid.test(r - 1, c);
44+
count += grid.test(r - 1, c + 1);
45+
count += grid.test(r, c - 1);
46+
count += grid.test(r, c + 1);
47+
count += grid.test(r + 1, c - 1);
48+
count += grid.test(r + 1, c);
49+
count += grid.test(r + 1, c + 1);
50+
return count;
51+
}
52+
53+
public static void main(String[] args) {
54+
String title = "Brian's Brain";
55+
BriansBrain game = new BriansBrain(50, 50, 10);
56+
game.run(title, 10);
57+
}
58+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package chapter16exercises;
2+
3+
import java.awt.Color;
4+
import java.awt.Graphics;
5+
6+
public class Cell {
7+
private final int x;
8+
private final int y;
9+
private final int size;
10+
private int state;
11+
12+
public static final int OFF = 0;
13+
public static final int ON = 1;
14+
public static final int DYING = 2;
15+
16+
public static final Color[] COLORS = { Color.WHITE, Color.BLACK, Color.GRAY };
17+
18+
public Cell(int x, int y, int size) {
19+
this.x = x;
20+
this.y = y;
21+
this.size = size;
22+
this.state = OFF;
23+
}
24+
25+
public void draw(Graphics g) {
26+
g.setColor(COLORS[state]);
27+
g.fillRect(x + 1, y + 1, size - 1, size - 1);
28+
g.setColor(Color.LIGHT_GRAY);
29+
g.drawRect(x, y, size, size);
30+
}
31+
32+
public boolean isOff() {
33+
return state == OFF;
34+
}
35+
36+
public boolean isOn() {
37+
return state == ON;
38+
}
39+
40+
public boolean isDying() {
41+
return state == DYING;
42+
}
43+
44+
public void turnOff() {
45+
state = OFF;
46+
}
47+
48+
public void turnOn() {
49+
state = ON;
50+
}
51+
52+
public void setState(int state) {
53+
this.state = state;
54+
}
55+
56+
public int getState() {
57+
return state;
58+
}
59+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package chapter16exercises;
2+
3+
public class Conway extends Automaton {
4+
5+
public Conway() {
6+
grid = new GridCanvas(5, 10, 20);
7+
grid.turnOn(2, 1);
8+
grid.turnOn(2, 2);
9+
grid.turnOn(2, 3);
10+
grid.turnOn(1, 7);
11+
grid.turnOn(2, 7);
12+
grid.turnOn(3, 7);
13+
}
14+
15+
private int countAlive(int r, int c) {
16+
int count = 0;
17+
count += grid.test(r - 1, c - 1);
18+
count += grid.test(r - 1, c);
19+
count += grid.test(r - 1, c + 1);
20+
count += grid.test(r, c - 1);
21+
count += grid.test(r, c + 1);
22+
count += grid.test(r + 1, c - 1);
23+
count += grid.test(r + 1, c);
24+
count += grid.test(r + 1, c + 1);
25+
return count;
26+
}
27+
28+
private static void updateCell(Cell cell, int count) {
29+
if (cell.isOn()) {
30+
if (count < 2 || count > 3) {
31+
cell.turnOff();
32+
}
33+
} else {
34+
if (count == 3) {
35+
cell.turnOn();
36+
}
37+
}
38+
}
39+
40+
private int[][] countNeighbors() {
41+
int rows = grid.numRows();
42+
int cols = grid.numCols();
43+
44+
int[][] counts = new int[rows][cols];
45+
for (int r = 0; r < rows; r++) {
46+
for (int c = 0; c < cols; c++) {
47+
counts[r][c] = countAlive(r, c);
48+
}
49+
}
50+
return counts;
51+
}
52+
53+
private void updateGrid(int[][] counts) {
54+
int rows = grid.numRows();
55+
int cols = grid.numCols();
56+
57+
for (int r = 0; r < rows; r++) {
58+
for (int c = 0; c < cols; c++) {
59+
Cell cell = grid.getCell(r, c);
60+
updateCell(cell, counts[r][c]);
61+
}
62+
}
63+
}
64+
65+
public void update() {
66+
int[][] counts = countNeighbors();
67+
updateGrid(counts);
68+
}
69+
70+
public static void main(String[] args) {
71+
String title = "Conway's Game of Life";
72+
Conway game = new Conway();
73+
game.run(title, 2);
74+
}
75+
76+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package chapter16exercises;
2+
3+
import java.awt.Canvas;
4+
import java.awt.Graphics;
5+
6+
public class GridCanvas extends Canvas {
7+
private Cell[][] array;
8+
9+
public GridCanvas(int rows, int cols, int size) {
10+
array = new Cell[rows][cols];
11+
for (int r = 0; r < rows; r++) {
12+
int y = r * size;
13+
for (int c = 0; c < cols; c++) {
14+
int x = c * size;
15+
array[r][c] = new Cell(x, y, size);
16+
}
17+
}
18+
setSize(cols * size, rows * size);
19+
}
20+
21+
public void draw(Graphics g) {
22+
for (Cell[] row : array) {
23+
for (Cell cell : row) {
24+
cell.draw(g);
25+
}
26+
}
27+
}
28+
29+
public void paint(Graphics g) {
30+
draw(g);
31+
}
32+
33+
public void update(Graphics g) {
34+
draw(g);
35+
}
36+
37+
public int numRows() {
38+
return array.length;
39+
}
40+
41+
public int numCols() {
42+
return array[0].length;
43+
}
44+
45+
public Cell getCell(int r, int c) {
46+
return array[r][c];
47+
}
48+
49+
public void turnOn(int r, int c) {
50+
array[r][c].turnOn();
51+
}
52+
53+
public int test(int r, int c) {
54+
try {
55+
if (array[r][c].isOn()) {
56+
return 1;
57+
}
58+
} catch (ArrayIndexOutOfBoundsException e) {
59+
}
60+
return 0;
61+
}
62+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package chapter16exercises;
2+
3+
public class Langton extends Automaton {
4+
private int xpos;
5+
private int ypos;
6+
private int head;
7+
8+
public Langton(int rows, int cols, int cellSize) {
9+
grid = new GridCanvas(rows, cols, cellSize);
10+
xpos = rows / 2;
11+
ypos = cols / 2;
12+
head = 0;
13+
}
14+
15+
private void flipCell() {
16+
Cell cell = grid.getCell(xpos, ypos);
17+
if (cell.isOff()) {
18+
head = (head + 1) % 4;
19+
cell.turnOn();
20+
} else {
21+
head = (head + 3) % 4;
22+
cell.turnOff();
23+
}
24+
}
25+
26+
private void moveAnt() {
27+
if (head == 0) {
28+
ypos -= 1;
29+
} else if (head == 1) {
30+
xpos += 1;
31+
} else if (head == 2) {
32+
ypos += 1;
33+
} else {
34+
xpos -= 1;
35+
}
36+
}
37+
38+
@Override
39+
public void update() {
40+
flipCell();
41+
moveAnt();
42+
}
43+
44+
public static void main(String[] args) {
45+
String title = "Langton's Ant";
46+
Langton game = new Langton(61, 61, 10);
47+
game.run(title, 500);
48+
}
49+
}

0 commit comments

Comments
 (0)