forked from dp50mm/chaos-theory-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
game_of_life.html
187 lines (169 loc) · 6.38 KB
/
game_of_life.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
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
<!DOCTYPE html>
<html>
<head>
<title>Conways game of life</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js" charset="utf-8"></script>
<style>
/* Center the svg on the web page */
.svg-container {
width:500px;
height:500px;
margin-top:100px;
margin-left:auto;
margin-right:auto;
}
/* Add hover effect to game tiles to make the interaction more intuitive*/
.game-tile:hover {
cursor:pointer;
stroke-width:2px;
}
/* Center the controls on the web page */
.controls {
width:500px;
margin-left:auto;
margin-right:auto;
}
</style>
<script>
$(document).ready(function() {
console.log('document ready');
var h_tiles = 20;
var v_tiles = 20;
var game_grid = [];
var width = 500;
var height = 500;
var playing = false;
for (var i = 0; i < v_tiles; i++) {
var row = [];
for (var j = 0; j < h_tiles; j++) {
row.push({
alive:false,
next:false
});
}
game_grid.push(row);
}
var rules = {
'rule_one':2,
'rule_three':3,
'rule_four':4
};
var svg = d3.select('.svg-container').append('svg')
.attr('width',width)
.attr('height',height);
function draw_grid(svg) {
svg.selectAll('rect').remove();
for (var i = 0; i < v_tiles; i++) {
for (var j = 0; j < h_tiles; j++) {
svg.append('rect')
.attr('x',i*(width/h_tiles))
.attr('y',j*(height/v_tiles))
.attr('width',width/h_tiles)
.attr('height',height/v_tiles)
.attr('fill',function() {
if(game_grid[i][j].alive) {
return 'rgba(0,0,0,0.8)';
} else {
return 'transparent';
}
})
.attr('stroke','black')
.attr('class','game-tile')
.attr('i',i)
.attr('j',j);
}
}
/**
* If playing is true
* Apply rules to the grid and redraw on timer
*/
if(playing) {
// Apply rules on the grid, setting the state for the next turn
var living_neighbours = 0;
for(var i = 1; i < v_tiles-1; i++) {
for (var j = 1; j < h_tiles-1; j++) {
// Count neighbouring cells
living_neighbours = 0;
// top
if(game_grid[i-1][j].alive) { living_neighbours += 1; }
// left top
if(game_grid[i-1][j-1].alive) { living_neighbours += 1; }
// left
if(game_grid[i][j-1].alive) { living_neighbours += 1; }
// left bottom
if(game_grid[i+1][j-1].alive) { living_neighbours += 1; }
// bottom
if(game_grid[i+1][j].alive) { living_neighbours += 1; }
// bottom right
if(game_grid[i+1][j+1].alive) { living_neighbours += 1; }
// right
if(game_grid[i][j+1].alive) { living_neighbours += 1; }
// right top
if(game_grid[i-1][j+1].alive) { living_neighbours += 1; }
if(living_neighbours < 2) { // Any live cell with fewer than two live neighbours dies, as if caused by under-population.
game_grid[i][j].next = false;
} else if(living_neighbours > 3) { // Any live cell with more than three live neighbours dies, as if by over-population.
game_grid[i][j].next = false;
} else { // Any live cell with two or three live neighbours lives on to the next generation.
game_grid[i][j].next = game_grid[i][j].alive;
}
if(living_neighbours == 3 && game_grid[i][j].alive == false) {
game_grid[i][j].next = true;
}
console.log('neighbours for '+i+' '+j+' : '+living_neighbours);
}
}
// Loop though the playing field to set next turn state to current state
for (var i = 0; i < v_tiles; i++) {
for (var j = 0; j < h_tiles; j++) {
game_grid[i][j].alive = game_grid[i][j].next;
game_grid[i][j].next = false;
}
}
setTimeout(function () {
draw_grid(svg);
}, 100);
} else {
/**
* Else if the game is not playing yet
* Add an event listener to the squares to set squares to alive
*/
$('.game-tile').click(function() {
console.log('tile clicked');
console.log($(this).attr('i'));
game_grid[$(this).attr('i')][$(this).attr('j')].alive = true;
draw_grid(svg);
});
}
}
draw_grid(svg);
/**
* Start game on click of the button
*/
$('#start').click(function() {
console.log('start game!');
if(playing == false) {
playing = true;
draw_grid(svg);
}
});
});
</script>
</head>
<body>
<div class='svg-container'>
</div>
<div class='controls'>
<button id='start'>
Start
</button>
</div>
<div class='controls'>
<h2>Manual</h2>
<p>step 1: click on some squares to turn them alive</p>
<p>step 2: click on [start] to start the game</p>
<p>optional step 3: <b>refresh the page to start over</b></p>
</div>
</body>
</html>