/
app.js
191 lines (153 loc) · 4.05 KB
/
app.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
var canvas = document.getElementById('gameCanvas');
var ctx = canvas.getContext('2d');
//VARIABLES
var grid = [];
var FPS = 10;
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
var canvasWidth = canvas.width;
var canvasHeight = canvas.height;
var rows = parseInt(canvasHeight / CELL_RATIO);
var cols = parseInt(canvasWidth / CELL_RATIO);
var CELL_COUNT = 5000;
var CELL_RATIO = 4;
var CELL_COLOR = '';
var CANVAS_COLOR = '';
function setConfig() {
CELL_COUNT = document.getElementById('cellCount').value || (rows * cols) / 4;
CELL_RATIO = document.getElementById('cellRatio').value || 4;
rows = parseInt(canvasHeight / CELL_RATIO);
cols = parseInt(canvasWidth / CELL_RATIO);
CELL_COLOR = document.getElementById('cellColor').value || 'black';
CANVAS_COLOR = document.getElementById('backgroundColor').value || 'white';
canvas.style.backgroundColor = CANVAS_COLOR;
var configForm = document.getElementById('configForm').style.visibility = 'hidden';
canvas.style.visibility = 'visible';
init();
start();
}
init();
function init() {
rows = parseInt(canvasHeight / CELL_RATIO);
cols = parseInt(canvasWidth / CELL_RATIO);
grid = generateGrid(rows, cols);
grid = seedGrid(grid, CELL_COUNT);
}
function tick() {
clearCanvas();
grid = nextGrid(grid);
drawGrid(grid);
}
var timer;
function render() {
drawGrid(grid);
timer = setTimeout(function() {
requestAnimationFrame(render);
tick();
}, 1000 / FPS);
}
function start() {
if (!grid.length) init();
render();
}
function stop() {
clearTimeout(timer);
}
function reset() {
stop();
clearCanvas();
grid = [];
}
function draw() {
init();
drawGrid(grid);
printGrid(grid);
}
function printTwoDimArray(arr) {
var rows = "";
for(var i = 0; i < arr.length; i++) {
for (var j = 0; j < arr[0].length; j++) {
rows += arr[i][j] + " ";
}
rows += "\n";
}
console.log(rows)
}
//return random int between min and max
function randomInt(min, max) {
return Math.floor(Math.random() * (max)) + min;
}
function generateGrid(rows, cols) {
var grid = [];
for(var i = 0; i < rows; i++) {
grid[i] = [];
for (var j = 0; j < cols; j++) {
grid[i][j] = 0;
}
}
return grid;
}
function seedGrid(grid, cellsCount) {
var rows = grid.length;
var cols = grid[0].length;
if(cellsCount > (rows * cols)) {
cellsCount = rows * cols;
console.log("To many cells");
}
for (var i = 0; i < cellsCount; i++) {
var r = randomInt(0, rows);
var c = randomInt(0, cols);
while (grid[r][c] == 1) {
r = randomInt(0, rows);
c = randomInt(0, cols);
}
grid[r][c] = 1;
};
return grid;
}
function printGrid(grid) {
printTwoDimArray(grid);
}
// args: cell - cell's value; n - neighbours alived
function nextCell(cell, n) {
if (n >= 0 && n <= 8) {
//Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
if (n === 3 && cell === 0) return 1
else
//Any live cell with fewer than two live neighbours dies, as if caused by under-population.
//Any live cell with more than three live neighbours dies, as if by over-population.
//Any live cell with two or three live neighbours lives on to the next generation.
if (cell === 1 && (n === 2 || n === 3)) return 1
else return 0;
} else return null;
}
function nextGrid(oldGrid) {
var rows = oldGrid.length;
var cols = oldGrid[0].length;
var grid = generateGrid(rows, cols);
var i = 1, j = 1, n = 0;
for (i = 1; i < rows - 1; i++) {
for (j = 1; j < cols - 1; j++) {
n = oldGrid[i-1][j-1] + oldGrid[i-1][j] + oldGrid[i-1][j+1] + oldGrid[i][j-1] + oldGrid[i][j+1] + oldGrid[i+1][j-1] + oldGrid[i+1][j] + oldGrid[i+1][j+1];
grid[i][j] = nextCell(oldGrid[i][j], n);
n = 0;
};
};
return grid;
}
function drawGrid(grid) {
for (var i = 0; i < rows; i++) {
for (var j = 0; j < cols; j++) {
if(grid[i][j] == 1)
drawCell(i, j);
};
};
}
//Canvas functions
function drawCell(x, y) {
ctx.fillStyle = CELL_COLOR || 'black';
ctx.fillRect(y * CELL_RATIO, x * CELL_RATIO, CELL_RATIO, CELL_RATIO);
}
function clearCanvas() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
}