-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlifeClass.js
More file actions
54 lines (46 loc) · 1.67 KB
/
lifeClass.js
File metadata and controls
54 lines (46 loc) · 1.67 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
function cloneArray(arr){
return arr.slice().map((row)=>row.slice());
}
module.exports.Life = class Life
{
constructor (seed){
this.seed = seed;
this.height = seed.length;
this.width = seed[0].length;
this.prevBoard = [];
this.board = cloneArray(seed);
}
next(){
this.prevBoard = cloneArray(this.board);
for(let i=0; i<this.height ;i++){
for(let j=0;j<this.width;j++){
let neighbors = this.aliveNeighbours(this.prevBoard,i,j);
let alive = !!this.board[i][j];
if(alive){
if(neighbors<2 || neighbors >3){
this.board[i][j] = 0;
}
}
else{
if(neighbors == 3){
this.board[i][j] = 1;
}
}
}
}
}
aliveNeighbours(array,x,y){
let sum = 0;
let prevRow = array[x-1] || [];
let nextRow = array[x+1] || [];
let neighbors = [
prevRow[y-1],prevRow[y],prevRow[y+1],
array[x][y-1],array[x][y+1],
nextRow[y-1],nextRow[y],nextRow[y+1]
].forEach((a)=>sum+=+!!a);
return sum;
}
toString(){
return this.board.map((row)=>row.join(' ')).join("\n");
}
}