-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbrowser.js
More file actions
100 lines (81 loc) · 2.16 KB
/
browser.js
File metadata and controls
100 lines (81 loc) · 2.16 KB
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
var PetriDish = require('./source/petriDish'),
SVGDisplay = require('./display/svg'),
SVGCell = SVGDisplay.SVGCell,
SVGFood = SVGDisplay.SVGFood,
Playable = require('./ai/playable'),
Random = require('./ai/random'),
Simple = require('./ai/simple'),
width = 500,
height = 225,
svgElement = document.getElementById('cells-dish'),
aiList, cellList, foodFactory,
petriDish,
intervalID;
function start() {
var randomCount = 1,
simpleCount = 1,
i, circles, circle;
$('button#btn-start').prop('disabled', true);
circles = document.getElementById('cells-dish').childNodes;
for (i = circles.length - 1; i >= 0; i--) {
circle = circles[i];
circle.remove();
}
aiList = Array.prototype.slice.call($('select')).map(function (select) {
return $(select).find('option:selected').val();
}).filter(function (value) {
return !!value;
}).map(function (value) {
switch (value) {
case 'Random':
return new Random('Random ' + randomCount++);
case 'Simple':
return new Simple('Simple ' + simpleCount++);
case 'Playable':
return new Playable('Player');
}
});
cellList = aiList.map(function (ai) {
var x = Math.random() * width,
y = Math.random() * height,
r, g, b;
if (ai instanceof Simple) {
r = 0;
g = 0;
b = 255;
} else if (ai instanceof Random) {
r = 255;
g = 0;
b = 0;
} else {
r = 0;
g = 255;
b = 0;
}
return new SVGCell(svgElement, x, y, 10, ai, r, g, b);
});
foodFactory = function(width, height) {
return new SVGFood(svgElement, width, height, 2);
};
petriDish = new PetriDish(width, height, 20, 12, 200, cellList, foodFactory);
intervalID = setInterval(function () {
petriDish.processTurn(function (err, results) {
if (results) {
clearInterval(intervalID);
intervalID = null;
alert(results.map(function (result, i) {
return '' + (i + 1) + '. ' + result.name;
}).join('\n'));
$('button#btn-start').prop('disabled', false);
}
});
}, 30);
function animate() {
petriDish.processDisplay();
if (intervalID) {
requestAnimationFrame(animate);
}
}
requestAnimationFrame(animate);
}
$('button#btn-start').on('click', start);