forked from geekhub-js-2014/life-homework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
85 lines (74 loc) · 2.18 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
(function() {
var hasAce = !!window.ace,
map,
iteration,
running,
showDiv = document.querySelector('#output'),
editor = hasAce ? ace.edit("editor") : document.querySelector('#editor');
hasAce && editor.getSession().setMode("ace/mode/javascript");
function initMap() {
var i, j, size = 100;
//var x, y = 5,
// plane = [
// [true, true, true],
// [false, false, true],
// [false, true, false]
//],
//plane1 = [];
map = [];
iteration = 0;
for (i = 0; i < size; i++) {
map[i] = [];
//if (!(i % y)) {
// plane1 = plane.map(function (val) {
// return val.slice(0, val.length);
// });
//}
//
//if (plane1.length) {
// x = plane1.pop();
//}
for (j = 0; j < size; j++) {
//map[i][j] = x.length ? x.pop() : false;
map[i][j] = Math.random() > 0.5;
}
}
show();
}
function show() {
showDiv.innerHTML = 'Iteration ' + iteration + '\n<br><div>' + map.reduce(function(memo, row) {
return memo + '<div class="row">' + row.map(function(val) {
return '<div class="block' + (val ? ' alive' : ' ') + '"></div>';
}).join('') + '</div>\n';
}, '') + '</div>';
}
function step() {
map = Life.step(map, iteration++);
show();
}
function run() {
running = true;
step();
setTimeout(function() {
running && run();
}, 50);
}
function stop() {
running = false;
}
window.step = step;
window.show = show;
window.run = run;
window.stop = stop;
document.querySelector('#run').addEventListener('click', function(e) {
e.preventDefault();
initMap();
eval(hasAce ? editor.getValue() : editor.textContent);
run();
});
document.querySelector('#stop').addEventListener('click', function(e) {
e.preventDefault();
stop();
});
initMap();
})();