-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlife.js
321 lines (284 loc) · 9.01 KB
/
life.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
$(document).ready(function() {
$(".reset").on("click", start);
$(".next_stage").on("click", nextStage);
var LENGTH = 23;
/* Initializes the array and view */
function start() {
$("#board").html("");
initBoard(parseInt(LENGTH));
/* Generates a grid */
/* Allows the user to designate cells as dead or alive */
for (index = 0; index < LENGTH; index += 1) {
$("#board").append("<tr id=\"" + index + "\">");
for (index2 = 0; index2 < LENGTH; index2 += 1) {
$("#" + index).append("<td class=\" dead " + index2 +"\"></td>");
$("#" + index + " ." + index2).on("click", function() {
if($(this).hasClass("dead")) {
$(this).removeClass("dead");
$(this).addClass("alive");
}else {
$(this).removeClass("alive");
$(this).addClass("dead");
}
});
}
$("#board").append("</tr>");
}; /* End view generation */
} /* End start */
/* Sets up an array to model the board for the game
* Takes the board length as a parameter and returns an array
* A 2 element denotes a cells current state and its next state
* The first element is its current state and the second is its
* next state. "0" for dead and "1" for live */
function initBoard(LENGTH) {
var index = 0;
var board = [];
while(index < LENGTH) {
var index2 = 0;
board[index] = [];
while(index2 < LENGTH) {
/* Initialize all cells to be dead */
board[index][index2] = ["0", "0"];
index2 += 1;
}
index += 1;
}
return board;
} /* End initBoard function */
/* Reads the html and syncs the array with it */
/* Sig: Takes in a length and returns an array */
function setArrayFromHTML(LENGTH) {
var board = initBoard(LENGTH);
var index = 0;
while(index < board.length) {
var index2 = 0;
while(index2 < board.length) {
if($("#" + index + " ." + index2).hasClass("alive")) {
board[index][index2] = ["1", "0"];
}else {
board[index][index2] = ["0", "0"];
}
index2 += 1;
}
index += 1;
}
return board;
} /* End set_array_from_html function */
/* Progresses to the next life stage
* Sets the array from the current html state, then calculates
* the status of cells for the next stage. Then it changes the
* cells current status to the next stage and updates the UI */
function nextStage() {
var board = setArrayFromHTML(LENGTH);
board = calculateAndSetNextStage(board);
board = markBoardAndSetHTML(board);
} /* End nextStage function */
/* Iterates through the board and calls count_living_neighbors to mark cells
* for death or life */
function calculateAndSetNextStage(board) {
var index = 0;
while(index < board.length) {
var index2 = 0;
while(index2 < board.length) {
var living_neighbors = count_living_neighbors([index, index2], board)
if(board[index][index2][0] === "1" && (living_neighbors < 2 || living_neighbors > 3)) {
board[index][index2][1] = "0";
}else if(board[index][index2][0] === "1" && (living_neighbors === 2 || living_neighbors === 3)) {
board[index][index2][1] = "1";
}else if(board[index][index2][0] === "0" && living_neighbors === 3) {
board[index][index2][1] = "1";
}
index2 += 1;
}
index += 1;
}
return board
} /* End calculateAndSetNextStage function */
/* Iterates through the board and calls count_living_neighbors to mark cells
* for death or life */
function markBoardAndSetHTML(board) {
var index = 0;
while(index < board.length) {
var index2 = 0;
while(index2 < board.length) {
if(board[index][index2][1] === "0") {
board[index][index2][0] = "0";
$("#" + index + " ." + index2).removeClass("alive").addClass("dead");
}else {
board[index][index2][0] = "1";
$("#" + index + " ." + index2).removeClass("dead").addClass("alive");
}
index2 += 1;
}
index += 1;
}
return board
} /* End mark_board function */
/* Counts the number of living neighbors for a cell.
* Takes in a 2 element array (integer strings)
* and a board (array)
* Returns the number of living neighbors */
function count_living_neighbors(coord, board) {
var count = 0;
/* Set x and y to coordinates */
var x = coord[0];
var y = coord[1];
/* If it is not an edge cell */
if (!(x === 0 ||
y === 0 ||
x === (board.length - 1) ||
y === (board.length - 1))) {
/* Count living neighbors clockwise from upper left cell */
if(board[x - 1][y - 1][0] === "1" ) {
count += 1;
}
if(board[x - 1][y][0] === "1") {
count += 1;
}
if(board[x - 1][y + 1][0] === "1") {
count += 1;
}
if(board[x][y + 1][0] === "1") {
count += 1;
}
if(board[x + 1][y + 1][0] === "1" ) {
count += 1;
}
if(board[x + 1][y][0] === "1") {
count += 1;
}
if(board[x + 1][y - 1][0] === "1") {
count += 1;
}
if(board[x][y - 1][0] === "1") {
count += 1;
}
/* Otherwise the cell is on the edge of the board */
} else {
/* Upper left corner */
if(x === 0 && y === 0) {
/* Count clockwise from the right cell */
if(board[x][y + 1][0] === "1") {
count += 1;
}
if(board[x + 1][y + 1][0] === "1" ) {
count += 1;
}
if(board[x + 1][y][0] === "1") {
count += 1;
}
/* Upper right corner */
}else if(x === 0 && y == (board.length - 1)) {
/* Count clockwise from lower cell */
if(board[x + 1][y][0] === "1") {
count += 1;
}
if(board[x + 1][y - 1][0] === "1") {
count += 1;
}
if(board[x][y - 1][0] === "1") {
count += 1;
}
/* Lower right corner */
}else if(x === (board.length - 1) && y === (board.length - 1)) {
/* Count clockwise from left cell */
if(board[x][y - 1][0] === "1") {
count += 1;
}
if(board[x - 1][y - 1][0] === "1" ) {
count += 1;
}
if(board[x - 1][y][0] === "1") {
count += 1;
}
/* Lower left corner cell */
}else if(x === (board.length - 1) && y === 0) {
/* Count clockwise from right */
if(board[x][y + 1][0] === "1") {
count += 1;
}
if(board[x - 1][y][0] === "1") {
count += 1;
}
if(board[x - 1][y + 1][0] === "1") {
count += 1;
}
/* Left side */
}else if( y === 0) {
/* Count clockwise from upper cell */
if(board[x - 1][y][0] === "1") {
count += 1;
}
if(board[x - 1][y + 1][0] === "1") {
count += 1;
}
if(board[x][y + 1][0] === "1") {
count += 1;
}
if(board[x + 1][y + 1][0] === "1" ) {
count += 1;
}
if(board[x + 1][y][0] === "1") {
count += 1;
}
/* Upper side */
}else if(x === 0) {
/* Count clockwise from right cell */
if(board[x][y + 1][0] === "1") {
count += 1;
}
if(board[x + 1][y + 1][0] === "1" ) {
count += 1;
}
if(board[x + 1][y][0] === "1") {
count += 1;
}
if(board[x + 1][y - 1][0] === "1") {
count += 1;
}
if(board[x][y - 1][0] === "1") {
count += 1;
}
/* Right side */
}else if(y === (board.length - 1)) {
/* Count clockwise from lower cell */
if(board[x + 1][y][0] === "1") {
count += 1;
}
if(board[x + 1][y - 1][0] === "1") {
count += 1;
}
if(board[x][y - 1][0] === "1") {
count += 1;
}
if(board[x - 1][y - 1][0] === "1" ) {
count += 1;
}
if(board[x - 1][y][0] === "1") {
count += 1;
}
/* Otherwise lower side */
}else {
/* Count clockwise from left side */
if(board[x][y - 1][0] === "1") {
count += 1;
}
if(board[x - 1][y - 1][0] === "1" ) {
count += 1;
}
if(board[x - 1][y][0] === "1") {
count += 1;
}
if(board[x - 1][y + 1][0] === "1") {
count += 1;
}
if(board[x][y + 1][0] === "1") {
count += 1;
}
}
}
return count
}; /* End count_living_neighbors function */
/* Initializes board on the display */
start(LENGTH);
}); /* End $(document).ready */