-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
290 lines (268 loc) · 9.29 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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
<html>
<head>
<title>Conway's Game of Life</title>
</head>
<body bgcolor="black">
<p><b>Conway's Game of Life</b></p>
<p>Instructions: Use the mouse to draw on the grid, and then hit play to start the simulation.
You can pause, adjust the speed, or draw while the game is running.</p>
<p>Conway's Game of Life is a "cellular automaton", a simulation that evolves using a simple set of rules.
<a color="white" href="https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life" target="_blank">
Read about it on Wikipedia.</a></p>
<canvas id="canvas"
onmousedown="canvasMouseDown(event);"
onmousemove="canvasMouseMove(event);"
onmouseup="canvasMouseUp(event);"></canvas>
<br><br>
<div style="display:inline-block;">
<button id="pause" type="button" class="coolButton" onclick="pauseClick();">Play</button>
<button id="step" type="button" class="coolButton" onclick="tick();">Next</button>
<button id="clear" type="button" class="coolButton" onclick="clearClick();">Clear</button>
<button id="showGrid" type="button" class="coolButton" onclick="showGridClick();">Show Grid</button>
</div>
<div style="display:inline-block; padding-left:100px;">
<img src="white-speed-256.png" alt="Game Speed">
<input id="speedSlider" type="range" min="1" max="25" value="4" onchange="adjustSpeed();">
</div>
<br><br> Abe Peterkin - 2016
</body>
<script>
var canvas = document.getElementById("canvas");
var clearButton = document.getElementById("clear");
var pauseButton = document.getElementById("pause");
var stepButton = document.getElementById("step");
var speedSlider = document.getElementById("speedSlider");
var showGridButton = document.getElementById("showGrid");
var canvasWidth = 1000;
var canvasHeight = 600;
canvas.height = canvasHeight;
canvas.width = canvasWidth;
var ctx=canvas.getContext("2d");
var cellSize = 10;
var rows = Math.ceil(canvasHeight / cellSize);
var columns = Math.ceil(canvasWidth / cellSize);
var grid = new Array(rows);
var timer;
var interval = 1000 / speedSlider.value;
var paused = true;
var drawingGridLines = false;
var drawing = false;
initialize(Math.ceil(canvasHeight / cellSize), Math.ceil(canvasWidth / cellSize));
/************************* EVENT HANDLERS **********************************/
// Handlers for drawing cells onto the canvas
function canvasMouseDown(event) {
drawing = true;
clickX = event.pageX - canvas.offsetLeft;
clickY = event.pageY - canvas.offsetTop;
alivenCell(Math.floor(clickY / cellSize), Math.floor(clickX / cellSize));
}
function canvasMouseMove(event) {
if (drawing) {
clickX = event.pageX - canvas.offsetLeft;
clickY = event.pageY - canvas.offsetTop;
alivenCell(Math.floor(clickY / cellSize), Math.floor(clickX / cellSize));
}
}
function canvasMouseUp(event) {
drawing = false;
}
// Handler for the 'pause' button
function pauseClick() {
paused = !paused;
if (paused) {
clearInterval(timer);
pauseButton.innerHTML = "Play";
} else {
timer = setInterval(tick, interval);
pauseButton.innerHTML = "Pause";
}
updateAndDraw();
}
// Handler for the 'clear' button
function clearClick() {
initialize(rows, columns);
}
// Handler for the 'show grid' button
function showGridClick() {
drawingGridLines = !drawingGridLines;
if (drawingGridLines) {
ctx.fillStyle = "white";
ctx.fillRect(0, 0, canvasWidth, canvasHeight);
drawGridLines();
updateAndDraw();
showGridButton.innerHTML = "Hide Grid";
} else {
ctx.fillStyle = "white";
ctx.fillRect(0, 0, canvasWidth, canvasHeight);
updateAndDraw();
showGridButton.innerHTML = "Show Grid";
}
}
// Handler for the 'speed' slider
function adjustSpeed() {
interval = 1000 / speedSlider.value;
if (!paused) {
clearInterval(timer);
timer = setInterval(tick, interval);
}
}
/************************* MISC FUNCTIONS ***************************************/
// Switches a cell from dead to alive, and draws a black square over it.
function alivenCell(r,c) {
grid[r][c].alive = true;
ctx.fillStyle = "black";
if (drawingGridLines) {
ctx.fillRect(c * cellSize, r * cellSize, cellSize - 1, cellSize - 1);
} else {
ctx.fillRect(c * cellSize, r * cellSize, cellSize, cellSize);
}
}
/* Initializes the array of cells.
* */
function initialize(rows, columns) {
for (var i = 0; i < rows; i++) {
var temp = new Array(columns);
for (var j = 0; j < columns; j++) {
temp[j] = {
alive: false,
change: 0
// 0 is no change, 1 is becoming alive, -1 is becoming dead.
}
}
grid[i] = temp;
}
updateAndDraw();
}
/* Updates the current state of the cells (based on the future state
* that's stored in the cells) and draws the living cells on the board. */
function updateAndDraw () {
for (var i = 0; i < rows; i++) {
for (var j = 0; j < columns; j++) {
if (grid[i][j].change === 1) {
grid[i][j].alive = true;
grid[i][j].change = 0;
} else if (grid[i][j].change === -1) {
grid[i][j].alive = false;
grid[i][j].change = 0;
}
if (grid[i][j].alive) {
ctx.fillStyle = "black";
} else {
ctx.fillStyle = "white";
}
if (drawingGridLines) {
ctx.fillRect(j * cellSize, i * cellSize, cellSize - 1, cellSize - 1);
} else {
ctx.fillRect(j * cellSize, i * cellSize, cellSize, cellSize);
}
}
}
}
// Draws lines on the grid that seperate the rows and columns.
function drawGridLines() {
ctx.beginPath();
for (var i = 0; i < rows; i++) {
ctx.moveTo(0, i *cellSize);
ctx.lineTo(canvasWidth, i * cellSize);
for (var j = 0; j < columns; j++) {
ctx.moveTo(j * cellSize, 0);
ctx.lineTo(j * cellSize, canvasHeight);
}
}
ctx.lineWidth=1;
ctx.strokeStyle = "gray";
ctx.stroke();
}
// Calculates what the state of the cells will be on the next step
function applyRules() {
for (var i = 0; i < rows; i++) {
for (var j = 0; j < columns; j++) {
liveNeighbors = countLiveNeighbors(i, j);
if (grid[i][j].alive && (liveNeighbors < 2 || liveNeighbors > 3)) {
grid[i][j].change = -1;
} else if (liveNeighbors === 3){
grid[i][j].change = 1;
}
}
}
}
/* Takes the row and column coordinates of a cell
* and returns the number of living neighbors that cell has */
function countLiveNeighbors(r, c) {
var count = 0;
/* Instead of using eight if statements,
* I use this array containing the eight coordinates that surround the cell. */
var coords = [r-1, c-1, r-1, c, r-1, c+1, r, c-1, r, c+1, r+1, c-1, r+1, c, r+1, c+1];
for (var i = 0; i < 16; i += 2) {
if (grid[coords[i]] === undefined) continue;
var cell = grid[coords[i]][coords[i + 1]];
if (cell !== undefined && cell.alive === true) {
count++;
}
}
return count;
}
// Applies the game rules and redraws the board.
function tick() {
applyRules();
updateAndDraw();
}
</script>
<style>
body {
color: white;
font-size: 150%;
text-align: center;
}
a:link {
color:white;
background-color:transparent;
text-decoration:underline
}
a:visited {
color:white;
background-color:transparent;
text-decoration:underline
}
/*credit to http://www.bestcssbuttongenerator.com/*/
.coolButton {
-moz-box-shadow: 0px 0px 0px 2px #000000;
-webkit-box-shadow: 0px 0px 0px 2px #000000;
box-shadow: 0px 0px 0px 2px #000000;
background:-webkit-gradient(linear, left top, left bottom, color-stop(0.05, #858585), color-stop(1, #292929));
background:-moz-linear-gradient(top, #858585 5%, #292929 100%);
background:-webkit-linear-gradient(top, #858585 5%, #292929 100%);
background:-o-linear-gradient(top, #858585 5%, #292929 100%);
background:-ms-linear-gradient(top, #858585 5%, #292929 100%);
background:linear-gradient(to bottom, #858585 5%, #292929 100%);
filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#858585', endColorstr='#292929',GradientType=0);
background-color:#858585;
-moz-border-radius:10px;
-webkit-border-radius:10px;
border-radius:10px;
border:1px solid #000000;
display:inline-block;
cursor:pointer;
color:#ffffff;
font-family:Arial;
font-size:19px;
padding:12px 37px;
text-decoration:none;
text-shadow:0px 1px 0px #000000;
}
.coolButton:hover {
background:-webkit-gradient(linear, left top, left bottom, color-stop(0.05, #292929), color-stop(1, #858585));
background:-moz-linear-gradient(top, #292929 5%, #858585 100%);
background:-webkit-linear-gradient(top, #292929 5%, #858585 100%);
background:-o-linear-gradient(top, #292929 5%, #858585 100%);
background:-ms-linear-gradient(top, #292929 5%, #858585 100%);
background:linear-gradient(to bottom, #292929 5%, #858585 100%);
filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#292929', endColorstr='#858585',GradientType=0);
background-color:#292929;
}
.coolButton:active {
position:relative;
top:1px;
}
</style>
</html>