-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
131 lines (112 loc) · 4.05 KB
/
main.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
//- JavaScript source code
//- game-of-life.js ~~
// ~~ (c) SRW, 18 Feb 2011
// ~~ last updated 13 Aug 2013
function main(argv) {
'use strict';
// Pragmas
/*jslint browser: true, indent: 4, maxlen: 80 */
// Declarations
var args, canvas, ctx, init, show, update;
// Definitions
args = {
// These simulation parameters can be configured via `location.search`.
rows: (typeof argv.rows === 'number') ? argv.rows : 50,
cols: (typeof argv.cols === 'number') ? argv.cols : 50,
pxls: (typeof argv.pxls === 'number') ? argv.pxls : 10,
freq: (typeof argv.freq === 'number') ? argv.freq : 20,
wrap: (typeof argv.wrap === 'boolean') ? argv.wrap : true
};
canvas = document.createElement('canvas');
canvas.width = args.cols * args.pxls;
canvas.height = args.rows * args.pxls;
document.body.appendChild(canvas);
ctx = canvas.getContext('2d');
init = function (p) {
// This function populates a cell grid randomly.
var grid, i, j;
grid = [];
for (i = 0; i < args.rows; i += 1) {
grid[i] = [];
for (j = 0; j < args.cols; j += 1) {
grid[i][j] = (Math.random() < p) ? 1 : 0;
}
}
return grid;
};
show = function (grid) {
// This function paints the next generation to the canvas.
var i, j, size;
size = args.pxls;
for (i = 0; i < args.rows; i += 1) {
for (j = 0; j < args.cols; j += 1) {
if (grid[i][j] === 1) {
ctx.fillRect(size * j, size * i, size, size);
} else {
ctx.clearRect(size * j, size * i, size, size);
}
}
}
return;
};
update = function (curr) {
// This function computes the next generation from the current one.
var i, j, neighbors, next;
neighbors = function (i, j) {
// This function computes the number of living neighbors for a cell
// at position (i, j).
var coords, count, index, row, col;
coords = [
[i - 1, j - 1], [i - 1, j], [i - 1, j + 1],
[i, j - 1], /*itself*/ [i, j + 1],
[i + 1, j - 1], [i + 1, j], [i + 1, j + 1]
];
count = 0;
for (index = 0; index < coords.length; index += 1) {
row = coords[index][0];
col = coords[index][1];
if (args.wrap === true) {
// Implement wraparound boundary conditions.
if ((row < 0) || (row === args.rows)) {
row = (row < 0) ? args.rows - 1 : 0;
}
if ((col < 0) || (col === args.cols)) {
col = (col < 0) ? args.cols - 1 : 0;
}
count += curr[row][col];
} else {
if (0 <= row && row < args.rows) {
if (0 <= col && col < args.cols) {
count += curr[row][col];
}
}
}
}
return count;
};
next = init(0); //- creates a "zeros" array :-)
for (i = 0; i < args.rows; i += 1) {
for (j = 0; j < args.cols; j += 1) {
switch (neighbors(i, j)) {
case 2:
next[i][j] = (curr[i][j] === 1) ? 1 : 0;
break;
case 3:
next[i][j] = 1;
break;
default:
next[i][j] = 0;
}
}
}
curr = next;
show(curr);
setTimeout(update, (1000 / args.freq), curr);
return;
};
// Invocations
update(init(0.6));
// That's all, folks!
return;
}
//- vim:set syntax=javascript: