-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lifecycle.jack
185 lines (163 loc) · 5.91 KB
/
Lifecycle.jack
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
The lifecycle of a grid of cells.
The rules are:
- Any live cell with fewer than two live neighbours dies, as if by underpopulation.
- Any live cell with two or three live neighbours lives on to the next generation.
- Any live cell with more than three live neighbours dies, as if by overpopulation.
- Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
See https://en.wikipedia.org/wiki/Conway's_Game_of_Life for more details of the rules.
*/
class Lifecycle {
field Array iteration1, iteration2, savedState;
field boolean currentIsIteration1;
constructor Lifecycle new() {
let iteration1 = ArrayHelper.getEmptyGrid();
let iteration2 = ArrayHelper.getEmptyGrid();
let savedState = ArrayHelper.getEmptyGrid();
let currentIsIteration1 = true;
return this;
}
// gets a two dimensional array representing the current iteration's live and dead cells
method Array getCurrentIteration() {
if (currentIsIteration1) {
return iteration1;
} else {
return iteration2;
}
}
// gets a two dimensional array representing the previous iteration's live and dead cells
method Array getPreviousIteration() {
if (currentIsIteration1) {
return iteration2;
} else {
return iteration1;
}
}
// ticks over to the next iteration by applying the rules of the game of life, as set out above
method boolean tick() {
var Array current, next, currentRow, nextRow;
var int x, y, neighbours;
var boolean alive, changed;
let current = getCurrentIteration();
let next = getPreviousIteration();
let y = 0;
while (y < 32) {
let x = 0;
while (x < 32) {
let currentRow = current[y];
let nextRow = next[y];
let alive = currentRow[x];
let neighbours = numberOfNeighbours(x, y);
if ((neighbours < 2) & alive) {
// underpopulation
let alive = false;
let changed = true;
}
if ((neighbours = 3) & ~alive) {
// reproduction
let alive = true;
let changed = true;
}
if ((neighbours > 3) & alive) {
// overpopulation
let alive = false;
let changed = true;
}
let nextRow[x] = alive;
let x = x + 1;
}
let y = y + 1;
}
let currentIsIteration1 = ~currentIsIteration1;
return changed;
}
// gets the number of neighbours of the specified cell
method int numberOfNeighbours(int x, int y) {
var int xMinusOne, xPlusOne, yMinusOne, yPlusOne, count;
var Array iteration, row, rowAbove, rowBelow;
if (x = 0) {
let xMinusOne = 31;
} else {
let xMinusOne = x - 1;
}
if (y = 0) {
let yMinusOne = 31;
} else {
let yMinusOne = y - 1;
}
if (x = 31) {
let xPlusOne = 0;
} else {
let xPlusOne = x + 1;
}
if (y = 31) {
let yPlusOne = 0;
} else {
let yPlusOne = y + 1;
}
let iteration = getCurrentIteration();
let row = iteration[y];
let rowAbove = iteration[yMinusOne];
let rowBelow = iteration[yPlusOne];
let count = 0;
if (rowAbove[xMinusOne]) { let count = count + 1; }
if (rowAbove[x]) { let count = count + 1; }
if (rowAbove[xPlusOne]) { let count = count + 1; }
if (row[xMinusOne]) { let count = count + 1; }
if (row[xPlusOne]) { let count = count + 1; }
if (rowBelow[xMinusOne]) { let count = count + 1; }
if (rowBelow[x]) { let count = count + 1; }
if (rowBelow[xPlusOne]) { let count = count + 1; }
return count;
}
// clears the lifecycle to give a grid of entirely dead cells
method void clear() {
do ArrayHelper.clearGrid(iteration1);
do ArrayHelper.clearGrid(iteration2);
return;
}
// saves the state of the current iteration, so it can be restored using the "restore" method
method void save() {
var Array iteration;
let iteration = getCurrentIteration();
do ArrayHelper.copyGrid(iteration, savedState);
return;
}
// restores the current iteration from a previously saved state
method void restore() {
var Array iteration;
let iteration = getCurrentIteration();
do ArrayHelper.copyGrid(savedState, iteration);
return;
}
// sets cells starting at x,y and travelling horizontally using a string shorthand in "---x-x-xx" style,
// where "-" represents a dead cell and "x" represents a live one
method void setCells(int x, int y, String cells) {
var Array iteration;
let iteration = getCurrentIteration();
do ArrayHelper.setArrayCells(iteration, x, y, cells);
return;
}
// toggles the state of the cell at x,y between dead and alive or vice versa
method boolean toggleCell(int x, int y) {
var Array iteration, row;
let iteration = getCurrentIteration();
let row = iteration[y];
let row[x] = ~row[x];
return row[x];
}
// returns whether the cell at x,y is alive
method boolean isAlive(int x, int y) {
var Array iteration, row;
let iteration = getCurrentIteration();
let row = iteration[y];
return row[x];
}
method void dispose() {
do ArrayHelper.cleanUp(iteration1);
do ArrayHelper.cleanUp(iteration2);
do ArrayHelper.cleanUp(savedState);
do Memory.deAlloc(this);
return;
}
}