-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.js
271 lines (232 loc) · 7.73 KB
/
code.js
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
var rows = 50;
var cols = 128;
var playing = false;
var currentStateArray;
var nextStateArray;
var timer;
var reproductionTime = 1000;
// initialize
function initialize() {
createTable();
setupControlButtons();
currentStateArray = createStateArray();
nextStateArray = createStateArray();
resetStateArrays();
}
function createStateArray() {
var stateArray = new Array(rows);
for (var i = 0; i < rows; i++) {
stateArray[i] = new Array(cols);
}
return stateArray;
}
function resetStateArrays() {
for (var row = 0; row < rows; row++) {
for (var cell = 0; cell < cols; cell++) {
currentStateArray[row][cell] = 0;
nextStateArray[row][cell] = 0;
}
}
}
function copyAndResetGrid() {
for (var i = 0; i < rows; i++) {
for (var j = 0; j < cols; j++) {
currentStateArray[i][j] = nextStateArray[i][j];
nextStateArray[i][j] = 0;
}
}
}
function computeNextGeneration() {
for (var row = 0; row < rows; row++) {
for (var col = 0; col < cols; col++) {
var numberOfNeighbors = getNumberOfNeighbors(row, col);
nextStateArray[row][col] = getNextStateForCell(currentStateArray[row][col], numberOfNeighbors);
}
}
// copy nextGrid to grid, and reset nextGrid
// copyAndResetGrid();
var temp = currentStateArray;
currentStateArray = nextStateArray;
nextStateArray = temp;
// copy all 1 values to "live" in the table
updateView();
timer = setTimeout(play, reproductionTime);
}
function getNextStateForCell(currentState, numberOfNeighbors) {
if (currentState === 1) {
// currently live cell
if (numberOfNeighbors < 2 || numberOfNeighbors > 3) {
// this cell dies
return 0;
} else {
// this cell has either 2 or 3 live neighbors, so it survives
return 1;
}
} else {
// currently dead cell
if (numberOfNeighbors === 3) {
// this cell revives, as if by reproduction
return 1;
} else {
// the dead cell remains as is
return 0;
}
}
}
function getNumberOfNeighbors(row, col) {
if (isCellOnBorder(row, col)) {
return getNumberOfNeighborsForBorderCell(row, col);
} else {
return getNumberOfNeighborsForInteriorCell(row, col);
}
}
function getNumberOfNeighborsForInteriorCell(row, col) {
return (currentStateArray[row][col - 1]
+ currentStateArray[row - 1][col - 1] + currentStateArray[row - 1][col] + currentStateArray[row - 1][col + 1]
+ currentStateArray[row][col + 1]
+ currentStateArray[row + 1][col - 1] + currentStateArray[row + 1][col] + currentStateArray[row + 1][col + 1]);
}
function isCellOnBorder(row, col) {
return (row === 0 || row === rows - 1 || col === 0 || col === cols - 1);
}
function getNumberOfNeighborsForBorderCell(row, col) {
if (isCornerCell(row, col)) {
return getCornerCellNumberOfNeighbors(row, col);
} else {
return getEdgeCellNumberOfNeighbors(row, col);
}
}
function isCornerCell(row, col) {
return ((row === 0 && col === 0) || (row === 0 && col === cols - 1) || (row === rows - 1 && col === 0) || (row === rows - 1 && col === cols - 1));
}
function getCornerCellNumberOfNeighbors(row, col) {
if (row === 0 && col === 0) {
// top left corner
return (currentStateArray[row][col + 1] + currentStateArray[row + 1][col] + currentStateArray[row + 1][col + 1]);
} else if (row === 0 && col === cols - 1) {
// top right corner
return currentStateArray[row][col - 1] + currentStateArray[row + 1][col] + currentStateArray[row + 1][col - 1];
} else if (row === rows - 1 && col === 0) {
// bottom left corner
return (currentStateArray[row][col + 1] + currentStateArray[row - 1][col] + currentStateArray[row - 1][col + 1]);
} else if (row === rows - 1 && col === cols - 1) {
// bottom right corner
return (currentStateArray[row][col - 1] + currentStateArray[row - 1][col] + currentStateArray[row - 1][col - 1]);
}
}
function getEdgeCellNumberOfNeighbors(row, col) {
if (row === 0) {
// north border
return (currentStateArray[row][col - 1] + currentStateArray[row + 1][col - 1] + currentStateArray[row + 1][col] + currentStateArray[row + 1][col + 1] + currentStateArray[row][col + 1]);
} else if (row === rows - 1) {
// south border
return (currentStateArray[row][col - 1] + currentStateArray[row - 1][col - 1] + currentStateArray[row - 1][col] + currentStateArray[row - 1][col + 1] + currentStateArray[row][col + 1]);
} else if (col === 0) {
// west border
return (currentStateArray[row - 1][col] + currentStateArray[row - 1][col + 1] + currentStateArray[row][col + 1] + currentStateArray[row + 1][col + 1] + currentStateArray[row + 1][col]);
} else if (col === cols - 1) {
// east border
return (currentStateArray[row - 1][col] + currentStateArray[row - 1][col - 1] + currentStateArray[row][col - 1] + currentStateArray[row + 1][col - 1] + currentStateArray[row + 1][col]);
}
}
// lay out the board
function createTable() {
var gridContainer = document.getElementById("gridContainer");
if (!gridContainer) {
// throw error
console.error("Problem: no div for the grid table!");
}
var table = document.createElement("table");
for (var i = 0; i < rows; i++) {
var tr = document.createElement("tr");
for (var j = 0; j < cols; j++) {
var cell = document.createElement("td");
cell.setAttribute("id", i + "_" + j);
cell.setAttribute("class", "dead");
cell.onclick = cellClickHandler;
tr.appendChild(cell);
}
table.appendChild(tr);
}
gridContainer.appendChild(table);
}
function cellClickHandler() {
var classes = this.getAttribute("class");
var rowcol = this.id.split("_");
var row = rowcol[0];
var col = rowcol[1];
if (classes.indexOf("live") > -1) {
this.setAttribute("class", "dead");
currentStateArray[row][col] = 0;
} else {
this.setAttribute("class", "live");
currentStateArray[row][col] = 1;
}
}
function updateView() {
for (var i = 0; i < rows; i++) {
for (var j = 0; j < cols; j++) {
var cell = document.getElementById(i + "_" + j);
if (currentStateArray[i][j] == 0) {
cell.setAttribute("class", "dead");
} else {
cell.setAttribute("class", "live");
}
}
}
}
function setupControlButtons() {
// button to start
var startButton = document.getElementById("start");
startButton.onclick = startButtonHandler;
// button to clear
var clearButton = document.getElementById("clear");
clearButton.onclick = clearButtonHandler;
// button for random cells
var randomButton = document.getElementById("random");
randomButton.onclick = randomButtonHandler;
}
function startButtonHandler() {
if (playing === false) {
console.log("Continue the game");
document.getElementById("random").disabled = true;
playing = true;
this.innerHTML = "pause";
play();
} else {
clearTimeout(timer);
console.log("Pause the game");
document.getElementById("random").disabled = false;
playing = false;
this.innerHTML = "continue";
}
}
function clearButtonHandler() {
console.log("Clear the game: stop playing, clear the grid");
if (playing) {
playing = false;
clearTimeout(timer);
}
var startButton = document.getElementById("start");
startButton.innerHTML = "start";
resetStateArrays();
updateView();
document.getElementById("random").disabled = false;
}
function randomButtonHandler() {
clearButtonHandler();
for (var i = 0; i < rows; i++) {
for (var j = 0; j < cols; j++) {
nextState = Math.round(Math.random());
currentStateArray[i][j] = nextState;
}
}
updateView();
}
// run the life game
function play() {
console.log("Play the game");
computeNextGeneration();
}
// start everything
window.onload = initialize;