-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
193 lines (166 loc) · 5.37 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
<html>
<head>
<title>Conway's life: Javascript edition</title>
<link rel="stylesheet" href="styles.css">
<link rel="stylesheet" href="hint.min.css">
</head>
<body>
<div class="container">
<h1>Conway's game of life: Javascript equality table edition</h1>
<p>What you see bellow is a standard implementation of mathematical game first devised by the British mathematician <a href="https://en.wikipedia.org/wiki/John_Horton_Conway" target="_blank">John Horton Conway</a> in 1970. The "game" is a zero-player game, meaning that its evolution is determined by its initial state, requiring no further input.</p>
<p>Now this particular one is seeded by <a href="https://dorey.github.io/JavaScript-Equality-Table/" target="_blank">equality table</a> of various Javascript values. The example uses loose comparison operator (==) which is notoriously known to produce rather counter-intuitive results.</p>
<p>Hover over a cell to see what is the value / comparison it represents. You can also toggle the cells by clicking on them and create your own patterns.</p>
<div class="game"></div>
<button id="tick">Tick</button>
<button id="start">START</button>
<p class="hidden">Surprisingly enough, Javascript doesn't anihilate itself. There appear to be stable elements to it's ecosystem, and potential to evolve past it's original boundries ;)</p>
<div class="footer">
Michal made this • <a href="http://twitter.com/emlison" target="_blank">@emlison</a> • <a href="http://github.com/mlison" target="_blank">Github</a>
</div>
</div>
<script type="text/javascript">
function makeCell(x, y) {
var cell = {
living: false,
neighbours: []
};
cell.neighbours.push({ x: x+1, y: y });
cell.neighbours.push({ x: x+1, y: y-1 });
cell.neighbours.push({ x: x+1, y: y+1 });
cell.neighbours.push({ x: x-1, y: y });
cell.neighbours.push({ x: x-1, y: y-1 });
cell.neighbours.push({ x: x-1, y: y+1 });
cell.neighbours.push({ x: x , y: y+1 });
cell.neighbours.push({ x: x , y: y-1 });
return cell;
}
function makeCells() {
var cells = [], cell, x, y;
for (y = 0; y < 50; y++) {
cells[y] = [];
for (x = 0; x < 50; x++) {
cells[y].push( makeCell(x,y) );
}
}
// these are the javascript values we want to compare with each other
var values = [
'true',
'false',
'1',
'0',
'-1',
'"true"',
'"false"',
'"1"',
'"0"',
'"-1"',
'""',
'null',
'undefined',
'Infinity',
'-Infinity',
'[]',
'new Object()',
'[[]]',
'[0]',
'[1]',
'NaN'
];
// make loose comparison between values and mark cells
values.forEach(function (valueY, y) {
values.forEach(function (valueX, x) {
// +15 to make it go in the middle
cells[y+15][x+15].living = (eval(valueY) == eval(valueX));
cells[y+15][x+15].title = valueY + ' == ' + valueX;
});
});
return cells;
}
// check if a cell exists at position
function isCell(pos) {
return cells[pos.y] && cells[pos.y][pos.x];
}
function isCellAlive(cell) {
return cell.living;
}
// get the cell at position
function getCell(pos) {
return cells[pos.y][pos.x];
}
// transform cell coordinates into object references, if one exists
function transformNeighbours(cells) {
return cells.map(function (row) {
return row.map(function (cell) {
cell.neighbours = cell.neighbours.filter(isCell).map(getCell);
return cell;
});
});
}
function draw(cells) {
var game = document.getElementsByClassName('game')[0];
game.innerHTML = '';
cells.forEach(function (row) {
row.forEach(function (cell) {
var div = document.createElement('div');
div.className = 'cell hint--top'
div.className += cell.living ? ' living' : '';
div.setAttribute('data-hint', cell.title || '');
div.addEventListener('click',function () {
cell.living = !cell.living;
draw(cells);
});
game.appendChild(div);
});
});
}
// One game tick will iterate over all cells
// and update whether they're alive or not
function tick() {
// actions to be executed after each tick
var actions = [];
// loop through the whole set and update life condition
cells.forEach(function (rows, y) {
rows.forEach(function (cell, x) {
var len = cell.neighbours.filter(isCellAlive).length,
pos = { x: x, y: y };
if (len < 2 || (cell.living && len > 3)) {
pos.living = false;
actions.push(pos);
} else if (len === 3) {
pos.living = true;
actions.push(pos);
}
});
});
// execute actions after the whole iteration is done
// (doing them mid-iteration would break the rules)
actions.forEach(function (action) {
cells[action.y][action.x].living = action.living;
});
// redraw the whole thing
draw(cells);
}
var interval, firstTime = true;
var cells = makeCells();
cells = transformNeighbours(cells);
window.onload = draw.bind(this, cells);
document.getElementById('tick').addEventListener('click', tick);
document.getElementById('start').addEventListener('click', function (e) {
if (!interval) {
interval = setInterval(tick, 350);
e.target.innerHTML = 'Stop';
} else {
clearInterval(interval);
e.target.innerHTML = 'START';
interval = null;
}
if (firstTime) {
firstTime = false;
setTimeout(function () {
document.getElementsByClassName('hidden')[0].className = '';
},8*1000);
}
});
</script>
</body>
</html>