forked from fainapahanko/Conway-s-Game-Of-Life
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameOfLife.js
124 lines (100 loc) · 3.89 KB
/
GameOfLife.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
class GameOfLife {
/*
functions
1 - create 2 2d arrays with zeros (active/inactive) - done!
2 - fill active array randomly with ones and zeros - done!
3 - set color for cells - done!
4 - count neigbours
5 - update generation
6 - clear canvas
*/
constructor() {
this.cell_size = 5;
this.dead_color = `#181818`;
this.alive_color = `#FF756B`;
this.cells_in_column = Math.floor(canvas.width / this.cell_size);
this.cells_in_rows = Math.floor(canvas.height / this.cell_size);
this.active_array = [];
this.inactive_array = [];
this.arrayInitialization = () => {
for (let i = 0; i < this.cells_in_rows; i++) {
this.active_array[i] = [];
for (let j = 0; j < this.cells_in_column; j++) {
this.active_array[i][j] = 0;
}
}
this.inactive_array = this.active_array;
};
this.arrayRandomize = () => {
for (let i = 0; i < this.cells_in_rows; i++) {
for (let j = 0; j < this.cells_in_column; j++) {
this.active_array[i][j] = (Math.random() > 0.5) ? 1 : 0;
}
}
};
this.fillArray = () => {
for (let i = 0; i < this.cells_in_rows; i++) {
for (let j = 0; j < this.cells_in_column; j++) {
let color;
if (this.active_array[i][j] == 1)
color = this.alive_color;
else
color = this.dead_color;
ctx.fillStyle = color;
ctx.fillRect(j * this.cell_size, i * this.cell_size, this.cell_size, this.cell_size);
}
}
};
this.setCellValueHelper = (row, col) => {
try {
return this.active_array[row][col];
}
catch {
return 0;
}
};
this.countNeighbours = (row, col) => {
let total_neighbours = 0;
total_neighbours += this.setCellValueHelper(row - 1, col - 1);
total_neighbours += this.setCellValueHelper(row - 1, col);
total_neighbours += this.setCellValueHelper(row - 1, col + 1);
total_neighbours += this.setCellValueHelper(row, col - 1);
total_neighbours += this.setCellValueHelper(row, col + 1);
total_neighbours += this.setCellValueHelper(row + 1, col - 1);
total_neighbours += this.setCellValueHelper(row + 1, col);
total_neighbours += this.setCellValueHelper(row + 1, col + 1);
return total_neighbours;
};
this.updateCellValue = (row, col) => {
const total = this.countNeighbours(row, col);
// cell with more than 4 or less then 3 neighbours dies. 1 => 0; 0 => 0
if (total > 4 || total < 3) {
return 0;
}
// dead cell with 3 neighbours becomes alive. 0 => 1
else if (this.active_array[row][col] === 0 && total === 3) {
return 1;
}
// or returning its status back. 0 => 0; 1 => 1
else {
return this.active_array[row][col];
}
};
this.updateLifeCycle = () => {
for (let i = 0; i < this.cells_in_rows; i++) {
for (let j = 0; j < this.cells_in_column; j++) {
let new_state = this.updateCellValue(i, j);
this.inactive_array[i][j] = new_state;
}
}
this.active_array = this.inactive_array
};
this.gameSetUp = () => {
this.arrayInitialization();
};
this.runGame = () => {
this.updateLifeCycle();
this.fillArray();
};
}
}