-
Notifications
You must be signed in to change notification settings - Fork 0
/
conway-game-of-life.js
76 lines (57 loc) · 1.71 KB
/
conway-game-of-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
var gol = function() {
var context = null,
cellSize = 10;
offsets = [-1, 0, 1],
tickDuration = 500;
var bigBang = initialWorld => {
var world = initialWorld;
drawWorld(initialWorld);
setInterval(function() {
world = nextWorld(world);
drawWorld(world);
}, tickDuration);
}
var nextWorld = world => {
var newWorld = [];
world.forEach((col, x) => {
newWorld.push([]);
col.forEach((cell, y) => {
var neighbours = 0,
isAlive;
offsets.forEach(h => offsets.forEach(v => {
if(world[x + h] && (h !== 0 || v !== 0)) world[x + h][y + v] === 1 && neighbours++;
}));
isAlive = world[x][y] === 1 && [2, 3].indexOf(neighbours) !== -1 || world[x][y] === 0 && neighbours === 3;
newWorld[x].push(isAlive ? 1 : 0);
})
});
return newWorld;
};
var drawWorld = world => {
// clear canvas
context.clearRect(0, 0, context.canvas.width, context.canvas.height);
//draw world
world.forEach((col, x) => col.forEach((cell, y) => cell === 1 && drawCell(x, y))) && console.log(world);
}
var drawCell = (x, y) => context.fillRect(x * cellSize, y * cellSize, cellSize, cellSize);
var worldFromCells = (cells, visibleWorldWidth, visibleWorldHeight) => {
var i, j, world = [];
for(i = 0; i < visibleWorldWidth; i++) {
world.push([]);
for(j = 0; j < visibleWorldHeight; j++) {
world[i].push(cells.some(c => c.x === i && c.y === j) ? 1 : 0);
}
}
return world;
};
return {
init: (canvas, width, height, initialCells) => {
// set the size of the visible world
canvas.width = width * cellSize;
canvas.height = height * cellSize;
// get context
context = canvas.getContext('2d');
bigBang(worldFromCells(initialCells, width, height));
}
};
};