-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
executable file
·216 lines (181 loc) · 5.66 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
<html>
<head>
<title>Game of Life</title>
</head>
<body style="font-family:helvetica">
<h1>Conway's Game of life</h1><hr/>
<p style="font-size:16px;">This is a demonstration of <a href="http://en.wikipedia.org/wiki/Conway's_Game_of_Life">Conway's Game of life.</a> If you dont know about it, please follow the wikipedia link, read it and then come back here.</p>
<br/>
<div style="position:relative;height:500px;text-align:center">
<div style="float:left;width:500px">
<canvas id="myCanvas" width="400" height="400" style="border:1px solid #c3c3c3;">You don't seem to have canvas support :(</canvas>
</div>
<div style="float:left;width:350px">
<table>
<tr>
<td>Oscillator (beacon)</td>
<td><input type="button" onclick="generateRadomPattern(1)" value="Pattern 1" /></td>
</tr>
<tr>
<td>Oscillator (blinker)</td>
<td><input type="button" onclick="generateRadomPattern(2)" value="Pattern 2" /></td>
</tr>
<tr>
<td>Oscillator (toad)</td>
<td><input type="button" onclick="generateRadomPattern(3)" value="Pattern 3" /></td>
</tr>
<tr>
<td>Spaceship (glider)</td>
<td><input type="button" onclick="generateRadomPattern(4)" value="Pattern 4" /></td>
</tr>
<tr>
<td>Random</td>
<td><input type="button" onclick="generateRadomPattern(0)" value="Random" /></td>
</tr>
<tr>
<td>(you will love this the most, trust me!) </td>
<td></td>
</tr>
</table>
</div>
</div>
<div style="text-align:center;font-size:12px;">
You need a Html 5 supported browser to see this. I have tested this on Firefox and Chrome.<br/>
If you find any bugs, please email me at singh.roshan08 AT gmail.com
</div>
<script type="text/javascript">
var canvasX = 400;
var canvasY = 400;
var dotSize = 10;
var containerX = canvasX/dotSize;
var containerY = canvasY/dotSize;
var c = document.getElementById("myCanvas");
var cxt = c.getContext("2d");
cxt.fillStyle="#EEE";
cxt.fillRect(0, 0, containerX, containerY);
cxt.fillStyle="#FF0000";
function generateTemplate() {
var template = Array(containerX);
for (var i=0; i<containerX; i++) {
template[i] = Array(containerY);
for (var j=0; j<containerY; j++) {
template[i][j] = 0;
}
}
return template;
}
/*
var containerArray = Array(containerX);
for (var i=0; i<containerX; i++) {
containerArray[i] = Array(containerY);
for (var j=0; j<containerY; j++) {
containerArray[i][j] = 0;
}
}*/
//one time random generation of array 5,5 with 10 filled dots
function generateRadomPattern(type) {
containerArray = generateTemplate();
// two sq
if(type == 1) {
containerArray[4][5] = 1;
containerArray[5][5] = 1;
containerArray[4][6] = 1;
containerArray[5][6] = 1;
containerArray[6][7] = 1;
containerArray[6][8] = 1;
containerArray[7][7] = 1;
containerArray[7][8] = 1;
}
//straight line (blinker)
else if (type == 2) {
containerArray[4][5] = 1;
containerArray[4][6] = 1;
containerArray[4][7] = 1;
}
//oscillator (toad)
else if (type == 3) {
containerArray[6][4] = 1;
containerArray[7][4] = 1;
containerArray[8][4] = 1;
containerArray[5][5] = 1;
containerArray[6][5] = 1;
containerArray[7][5] = 1;
}
//glider
else if (type == 4) {
containerArray[3][7] = 1;
containerArray[4][8] = 1;
containerArray[5][6] = 1;
containerArray[5][7] = 1;
containerArray[5][8] = 1;
}
else {
for (var i=0; i<50; i++) {
var x = Math.floor(Math.random()*(containerX - dotSize));
var y = Math.floor(Math.random()*(containerY - dotSize));
containerArray[x][y] = 1;
}
}
replot();
}
function calculateNeighbours(x,y) {
var countNow = 0;
for (var i=x-1; i<=x+1 && i<containerX; i++) {
//corner cases
if (i<0)
continue;
for (var j=y-1; j<=y+1 && j<containerY; j++) {
if (j<0)
continue;
if (!(x==i && y==j) && containerArray[i][j] == 1) {
countNow++;
}
}
}
return countNow;
}
function replot() {
cxt.fillStyle="#EEE";
cxt.fillRect(0, 0, canvasX, canvasY);
cxt.fillStyle="#FF0000";
for (var i=0; i<containerX; i++) {
for (var j=0; j<containerY; j++) {
if (containerArray[i][j] == 1) {
cxt.fillRect(i*dotSize, j*dotSize, dotSize, dotSize);
}
}
}
}
generateRadomPattern(0);
var moveInterval = setInterval(moveNow, 300);
function moveNow() {
var temp = generateTemplate();
for (var i=0; i<containerX; i++) {
for (var j=0; j<containerY; j++) {
var count = calculateNeighbours(i,j);
//if live cell
if (containerArray[i][j] == 1) {
//if neighbour count is less than two or greater than three, then die
if (count < 2 || count > 3) {
temp[i][j] = 0;
}
//if neighbour count is two or three, then survive
if (count == 2 || count == 3) {
temp[i][j] = 1;
}
}
//if dead cell
if (containerArray[i][j] == 0) {
//if neighbour count is three, then become live
if (count == 3) {
temp[i][j] = 1;
}
}
}
}
containerArray = temp;
replot();
}
</script>
</body>
</html>