/
game.js
55 lines (43 loc) · 1.31 KB
/
game.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
var startButton = document.createElement('button')
startButton.id = 'start'
startButton.innerHTML = 'Generate'
document.body.appendChild(startButton)
var stopButton = document.createElement('button')
stopButton.id = 'stop'
stopButton.innerHTML = 'Freeze'
stopButton.disabled = true
document.body.appendChild(stopButton)
/* THE GAME OF LIFE
**
** 1st rule
** Any live cell with fewer than two live neighbours dies, as if caused by under-population.
**
** 2nd rule
** Any live cell with two or three live neighbours lives on to the next generation.
**
** 3rd rule
** Any live cell with more than three live neighbours dies, as if by overcrowding.
**
** 4th rule
** Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
**
*/
var world = new World
world.create()
world.spawn('glider', 3, 3)
world.spawn('glider', 15, 20)
world.spawn('glider', 30, 4)
var intervalId = 0;
startButton.addEventListener('click', function(){
this.disabled = true
stopButton.disabled = false
intervalId = setInterval(function() {
world.generate()
world.renderNextGeneration()
}, world.speed)
}, false)
stopButton.addEventListener('click', function(){
this.disabled = true
startButton.disabled = false
clearInterval(intervalId)
}, false)