-
Notifications
You must be signed in to change notification settings - Fork 0
/
life.js
87 lines (63 loc) · 1.81 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
var Life = function(n){
this.worldN = n;
this.world = [];
var that = this;
this._step = function (callBack){
var nextWorld = that._clear([]);
for(i = 0; i < nextWorld.length; i ++){
for(j = 0; j < nextWorld[i].length; j ++){
nextWorld[i][j] = that.isAlive(i, j);
}
}
that.world = nextWorld;
callBack(that.world, that.worldN);
}
this.isAlive = function(x, y){
var x_left = (that.worldN + x - 1) % that.worldN;
var x_right = (that.worldN + x + 1) % that.worldN;
var y_up = (that.worldN + y - 1) % that.worldN;
var y_down = (that.worldN + y + 1) % that.worldN;
var neighbours = 0;
if(that.world[x_left][y_up]) neighbours += 1;
if(that.world[x][y_up]) neighbours += 1;
if(that.world[x_right][y_up]) neighbours += 1;
if(that.world[x_left][y]) neighbours += 1;
if(that.world[x_right][y]) neighbours += 1;
if(that.world[x_left][y_down]) neighbours += 1;
if(that.world[x][y_down]) neighbours += 1;
if(that.world[x_right][y_down]) neighbours += 1;
var isAlive = (that.world[x][y] && (neighbours == 2 || neighbours == 3))
|| (neighbours == 3);
return isAlive;
}
this._setCell = function (x, y, state){
that.world[x][y] = state;
}
this._clear = function(aWorld){
if(aWorld == undefined){
aWorld = that.world;
}
for(i = 0; i < n; i ++ ){
aWorld[i] = [];
for(j = 0; j < n; j ++){
aWorld[i][j] = false;
}
}
return aWorld;
}
};
Life.prototype.setCell = function(x, y, state){
this._setCell(x, y, state);
}
Life.prototype.step = function(fnCallback){
this._step(fnCallback);
}
Life.prototype.clear = function(){
this._clear();
}
exports.Life = Life;
exports.init = function(n){
var newLife = new Life(n);
newLife.clear();
return newLife;
}