Skip to content

Commit 3462ba8

Browse files
committed
Ant
1 parent ace73d6 commit 3462ba8

5 files changed

Lines changed: 302 additions & 0 deletions

File tree

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package chapter16sections;
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 chapter16sections;
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 Cell(int x, int y, int size) {
13+
this.x = x;
14+
this.y = y;
15+
this.size = size;
16+
this.state = 0;
17+
}
18+
19+
public static final Color[] COLORS = { Color.WHITE, Color.BLACK };
20+
21+
public void draw(Graphics g) {
22+
g.setColor(COLORS[state]);
23+
g.fillRect(x + 1, y + 1, size - 1, size - 1);
24+
g.setColor(Color.LIGHT_GRAY);
25+
g.drawRect(x, y, size, size);
26+
}
27+
28+
public boolean isOff() {
29+
return state == 0;
30+
}
31+
32+
public boolean isOn() {
33+
return state == 1;
34+
}
35+
36+
public void turnOff() {
37+
state = 0;
38+
}
39+
40+
public void turnOn() {
41+
state = 1;
42+
}
43+
44+
public static void main(String[] args) {
45+
int rows = 4;
46+
int cols = 3;
47+
int size = 20;
48+
Cell[][] array = new Cell[rows][cols];
49+
50+
for (int r = 0; r < rows; r++) {
51+
int y = r * size;
52+
for (int c = 0; c < cols; c++) {
53+
int x = c * size;
54+
array[r][c] = new Cell(x, y, size);
55+
}
56+
}
57+
}
58+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package chapter16sections;
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 chapter16sections;
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: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package chapter16sections;
2+
3+
import javax.swing.JFrame;
4+
5+
public class Langton {
6+
7+
private GridCanvas grid;
8+
private int xpos;
9+
private int ypos;
10+
private int head;
11+
12+
public Langton(int rows, int cols) {
13+
grid = new GridCanvas(rows, cols, 10);
14+
xpos = rows / 2;
15+
ypos = cols / 2;
16+
head = 0;
17+
}
18+
19+
private void flipCell() {
20+
Cell cell = grid.getCell(xpos, ypos);
21+
if (cell.isOff()) {
22+
head = (head + 1) % 4;
23+
cell.turnOn();
24+
} else {
25+
head = (head + 3) % 4;
26+
cell.turnOff();
27+
}
28+
}
29+
30+
private void moveAnt() {
31+
if (head == 0) {
32+
ypos -= 1;
33+
} else if (head == 1) {
34+
xpos += 1;
35+
} else if (head == 2) {
36+
ypos += 1;
37+
} else {
38+
xpos -= 1;
39+
}
40+
}
41+
42+
public void update() {
43+
flipCell();
44+
moveAnt();
45+
}
46+
47+
private void mainloop() {
48+
while (true) {
49+
50+
this.update();
51+
grid.repaint();
52+
53+
try {
54+
Thread.sleep(2);
55+
} catch (InterruptedException e) {
56+
}
57+
}
58+
}
59+
60+
public static void main(String[] args) {
61+
String title = "Langton's Ant";
62+
Langton game = new Langton(61, 61);
63+
JFrame frame = new JFrame(title);
64+
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
65+
frame.setResizable(false);
66+
frame.add(game.grid);
67+
frame.pack();
68+
frame.setVisible(true);
69+
game.mainloop();
70+
}
71+
72+
}

0 commit comments

Comments
 (0)