/
demo.html
56 lines (51 loc) · 1.56 KB
/
demo.html
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
<!doctype html>
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="lib/yourlib.js"></script>
<script type="text/javascript">
function createGrid(size){
var grid = []
for(var x = 0; x < size; x++){
var row = []
for(var y = 0; y < size; y++){
row.push(Math.random() * 100 > 90?'x':'.')
}
grid.push(row)
}
return grid
}
$(function(){
var canvas = document.getElementById('gameoflife');
var ctx = canvas.getContext('2d');
var grid = createGrid(150)
var game = createNewGame(grid)
draw(grid, ctx);
setInterval(function(){
console.log('evolving')
game.evolve(function(cell){
if(cell.state == '.'){
ctx.fillStyle = 'rgb(0,0,0)'
}else if(cell.state == 'x'){
ctx.fillStyle = 'rgb(255,255,255)'
}
ctx.fillRect(cell.location[0], cell.location[1], 1,1)
})
}, 100)
// draw grid, initialize game
})
function draw(grid, ctx){
grid.forEach(function(row, x){
row.forEach(function(cell, y){
if(cell == 'x')
ctx.fillRect(x,y,1,1)
})
})
}
</script>
</head>
<body>
<canvas id="gameoflife" width="150" height="150">
<input type="button" value="start">
</body>
</html>