-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
246 lines (219 loc) · 8.84 KB
/
index.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Conway's Game of Life</title>
<style type="text/css" >
body { font-family:calibri,helvetica; font-size:14px; }
button { width:80px;}
h3 { margin: 10px 0px 10px 0px; font-size:20px; }
#gdiv { margin: 5px 0px 0px 0px; }
#exLab { margin: 0px 10px 0px 10px; }
.row2 { margin:10px 0px 0px 0px;}
.block { display:inline-block; width:90px;}
.block2 { display:inline-block; width:82px; margin:0px 0px 0px 10px; }
.bc2 { margin:0px 0px 0px 10px; }
</style>
</head>
<body>
<h3>Conway's Game of Life</h3>
<fieldset>
<legend>Parameters</legend>
<div>
<label class="block">Grid Width</label>
<input id="gwidth" type="text" value="60"></input>
<label class="block2">Grid Height</label>
<input id="gheight" type="text" value="40"></input>
<label id="exLab">Examples</label>
<select id="examples">
<option></option>
</select>
<button class="bc2" onclick="redrawGrid()">Redraw</button>
</div>
<div class="row2">
<label class="block">Interval (ms)</label>
<input id="interval" type="text" value="500"></input>
<button id="bstrt" class="bc2" onclick="start()">Start</button>
<button id="bstop" onclick="stop()" disabled="true">Stop</button>
</div>
</fieldset>
<a href="https://en.wikipedia.org/wiki/Conway's_Game_of_Life">What is this?<a>
<div id="gdiv">
<svg id="grid" alt="SVG not supported by your browser" xmlns="http://www.w3.org/2000/svg">
<style type="text/css" >
<![CDATA[
rect.r_off { fill:#008d46; stroke-width:1; stroke:#3080d0; }
rect.r_on { fill:#3080d0; stroke-width:1; stroke:#3080d0; }
]]>
</style>
</svg>
</div>
<div>Generation: <span id="genCount">0</span></div>
</body>
<script>
var TILE_SIZE = 15;
var OFFSETS = [[-1, -1], [-1, 0], [-1, 1], [0, -1], [0, 1], [1, -1], [1, 0], [1, 1]];
function makeTile(row, col) {
var newElement = document.createElementNS("http://www.w3.org/2000/svg", 'rect');
newElement.setAttribute('width', TILE_SIZE);
newElement.setAttribute('height', TILE_SIZE);
newElement.setAttribute('x', col * TILE_SIZE);
newElement.setAttribute('y', row * TILE_SIZE);
newElement.setAttribute('class', 'r_off');
newElement.addEventListener('click', function () {
switchTile(this);
});
return newElement;
}
/**
* Draw the grid on screen and build the grid array.
*
* @param height the required number of tiles high
* @param width the required number of tiles wide
* @returns a 2x2 array with the {@code rect} elements created
*/
function buildGrid(height, width) {
var rv = new Array(height);
var svg = document.getElementById('grid');
svg.setAttribute('width', width * TILE_SIZE);
svg.setAttribute('height', height * TILE_SIZE);
for (var i = 0; i < height; i++) {
rv[i] = new Array(width);
for (var j = 0; j < width; j++) {
var newElement = makeTile(i, j);
svg.appendChild(newElement);
rv[i][j] = newElement;
}
}
return rv;
}
function isLive(element) {
return element.getAttribute('class') == 'r_on';
}
/**
* Counts the number of live tiles surrounding the requested tile.
*/
function liveNeighbours(grid, row, col) {
var gh = grid.length - 1;
var gw = grid[0].length - 1;
var ln = 0;
for (var i = 0; i < OFFSETS.length; i++) {
var offset = OFFSETS[i];
var checkRow = row + offset[0];
var checkCol = col + offset[1];
if (checkRow >= 0 && checkRow <= gh && checkCol >= 0 && checkCol <= gw) {
if (isLive(grid[checkRow][checkCol])) {
ln++;
}
}
}
return ln;
}
/**
* Applies the four GoL rules to each tile in the grid turn.
*
* @returns an array with all of the tiles needed to be switched value
*/
function applyRules(grid) {
var switchList = [];
for (i = 0; i < grid.length; i++) {
for (j = 0; j < grid[i].length; j++) {
var ln = liveNeighbours(grid, i, j);
var curEl = grid[i][j];
if (isLive(curEl)) {
if (ln < 2 || ln > 3) {
switchList.push(curEl);
}
} else if (ln == 3) {
switchList.push(curEl);
}
}
}
return switchList;
}
function switchTile(tile) {
if (isLive(tile)) {
tile.setAttribute('class', 'r_off');
} else {
tile.setAttribute('class', 'r_on');
}
}
/**
* Takes an array of tiles and toggles the live status of each.
*/
function switchTiles(switchList) {
var size = switchList.length;
for (i = 0; i < size; i++) {
switchTile(switchList[i]);
}
document.getElementById("genCount").textContent++;
}
function drawExample(tileList) {
for (var i = 0; i < tileList.length; i++) {
var tile = tileList[i];
switchTile(grid[tile[0]][tile[1]]);
}
}
function exportGrid() {
for (i = 0; i < grid.length; i++) {
for (j = 0; j < grid[i].length; j++) {
if (isLive(grid[i][j])) {
console.log('[' + i + ', ' + j + ']');
}
}
}
}
/**
* Loads a JSON file from the webserver.
*/
function loadJSON(callback) {
var xobj = new XMLHttpRequest();
xobj.overrideMimeType("application/json");
xobj.open('GET', 'examples.json', true);
xobj.onreadystatechange = function () {
if (xobj.readyState == 4 && xobj.status == "200") {
callback(xobj.responseText);
}
};
xobj.send(null);
}
var grid = null;
var token = null;
var examples = null;
loadJSON(function(response) {
examples = JSON.parse(response);
var dd = document.getElementById('examples');
for (var k in examples) {
dd.options[dd.options.length] = new Option(k, k);
}
redrawGrid();
});
function start() {
document.getElementById("bstrt").disabled = true;
document.getElementById("bstop").disabled = false;
var inter = document.getElementById('interval').value;
token = window.setInterval(function () {
switchTiles(applyRules(grid));
}, inter);
}
function stop() {
document.getElementById("bstrt").disabled = false;
document.getElementById("bstop").disabled = true;
if (token != null) {
window.clearInterval(token);
token = null;
}
}
function redrawGrid() {
stop();
document.getElementById("genCount").textContent = 0;
var gridHeight = document.getElementById('gheight').value;
var gridWidth = document.getElementById('gwidth').value;
grid = buildGrid(gridHeight, gridWidth);
var dd = document.getElementById('examples');
if (dd.selectedIndex > 0) {
drawExample(examples[dd.options[dd.selectedIndex].text]);
}
}
</script>
</html>