-
Notifications
You must be signed in to change notification settings - Fork 0
/
Langton.java
87 lines (79 loc) · 1.68 KB
/
Langton.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
* Chapter 16.1
*/
import javax.swing.JFrame;
public class Langton
{
//BOOK:grid is a GridCanvas object, which represents the state of the cells
private GridCanvas grid;
private int xpos;
private int ypos;
private int head;
//head is which direction the ant is facing, "its heading"....
//0=North(top of the screen), 1=East, 2=South, 3=West
public Langton(int rows, int cols)
{
grid = new GridCanvas(rows, cols, 10);
xpos = rows / 2;
ypos = cols / 2;
head = 0;
}
public void update()
{
flipCell();
moveAnt();
}
//!!!!!!!!!NOTICE HOW % ALLOWS WRAP-AROUND!!!!!!!!!!!!!!!!!!!!!!!!!
private void flipCell()
{
Cell cell = grid.getCell(xpos, ypos);
if(cell.isOff())
{
head = (head + 1) % 4; //%4 seems pointless???
cell.turnOn(); //(3+1)%4=0 so it allows a wrap around
} else {
head = (head + 3) % 4; //not (head-1) bc (-1)%4=-1
cell.turnOff(); //!!+3 acts as 3 right turns!!
}
}
private void moveAnt()
{
if(head == 0) {
ypos -= 1;
}
else if(head == 1) {
xpos += 1;
}
else if(head == 2) {
ypos += 1;
}
else {
xpos -= 1;
}
}
public static void main(String[] args)
{
String title = "Langton's Ant";
Langton game = new Langton(61, 61);
JFrame frame = new JFrame(title);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.add(game.grid);
frame.pack();
frame.setVisible(true);
game.mainloop();
}
private void mainloop()
{
while(true)
{
this.update();
grid.repaint();
try {
Thread.sleep(2);
} catch(InterruptedException e) {
//do nathing
}
}
}
}