-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.js
More file actions
51 lines (38 loc) · 1.21 KB
/
node.js
File metadata and controls
51 lines (38 loc) · 1.21 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
// requires neighborhood.js to be loaded
var ALIVE = true;
var DEAD = false;
function Node(startingState) {
var neighbourhood = new Neighbourhood();
var that = this;
this.living = startingState;
function shouldProduceDeadNode() {
var test = neighbourhood.underpopulated();
return (
(this.living && neighbourhood.underpopulated()) ||
(this.living && neighbourhood.overcrowded())
);
}
function shouldProduceLiveNode() {
return (
(this.living && neighbourhood.suitable()) ||
(!this.living && neighbourhood.energized())
);
}
function newNodeAs(state) {
var offSpring = new Node(state);
offSpring.addNeighbors(neighbourhood.getNeighbours());
return offSpring;
}
this.addNeighbors = function(nodes) {
neighbourhood.addNeighbors(nodes);
};
this.reproduce = function() {
if (shouldProduceDeadNode()) {
return newNodeAs(DEAD);
}
if (shouldProduceLiveNode()) {
return newNodeAs(ALIVE);
}
return newNodeAs(this.living); //produce an unchanged clone.
};
}