-
Notifications
You must be signed in to change notification settings - Fork 0
/
gameoflife.html
executable file
·196 lines (152 loc) · 4.85 KB
/
gameoflife.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
<html>
<head>
<title>Conway's Game of Life</title>
</head>
<body style="margin:0;padding:0;">
<canvas id="game_of_life_canvas" style="border:.1px solid #000000;margin:0;padding:0;"></canvas>
<script>
var Cell = function(x, y, alive){
this.x = x;
this.y = y;
this.alive = alive;
this.aliveLastGen = alive;
}
function drawLifeBoard(canvas, numSquares){
var ctx = canvas.getContext("2d");
var width = canvas.width;
var height = canvas.height;
var widthEachRect = width/numSquares;
var heightEachRect = height/numSquares;
var board = [];
var widthEachRect = width/numSquares;
var heightEachRect = height/numSquares;
ctx.lineWidth=".1";
for(var i=0; i < numSquares; i++){
board[i] = [];
for(var j=0; j < numSquares; j++){
var x = i * widthEachRect;
var y = j * heightEachRect;
var alive = false;
//if((i <= numSquares/2) && (j <= numSquares/2)){
alive = (Math.floor((Math.random()*10000)+1) % 7) == 0;
//}
board[i][j] = new Cell(i, j, alive);
ctx.rect(x, y, widthEachRect, heightEachRect);
}
}
ctx.stroke();
return board;
}
function fillCell(ctx, numSquares, cell){
var width = canvas.width;
var height = canvas.height;
var widthEachRect = width/numSquares;
var heightEachRect = height/numSquares;
var x = cell.x * widthEachRect;
var y = cell.y * heightEachRect;
ctx.fillStyle = cell.alive ? "#000000" : "#FFFFFF";
cell.aliveLastGen = cell.alive;
ctx.fillRect(x, y, widthEachRect, heightEachRect);
}
function drawGeneration(canvas, numSqaures, board){
var ctx = canvas.getContext("2d");
for(var i=0; i < numSquares; i++){
for(var j=0; j < numSquares; j++){
fillCell(ctx, numSquares, board[i][j]);
}
}
ctx.stroke();
}
function processRulesOfLife(board){
for(var i=0; i < board.length; i++){
for(var j=0; j < board[i].length; j++){
var neighbors = 0, x = 0, y = 0;
x = (i == 0) ? (board.length - 1) : i - 1;
y = (j == 0) ? (board[x].length - 1) : j - 1;
if (board[x][y].aliveLastGen){
neighbors += 1;
}
y = j;
if (board[x][y].aliveLastGen){
neighbors += 1;
}
y = ((j + 1) == board[x].length) ? 0 : j + 1;
if (board[x][y].aliveLastGen){
neighbors += 1;
}
x = i;
y = (j == 0) ? (board[x].length - 1) : j - 1;
if (board[x][y].aliveLastGen){
neighbors += 1;
}
y = ((j + 1) == board[x].length) ? 0 : j + 1;
if (board[x][y].aliveLastGen){
neighbors += 1;
}
x = ((i + 1) == board.length) ? 0 : i + 1;
y = (j == 0) ? (board[x].length - 1) : j - 1;
if (board[x][y].aliveLastGen){
neighbors += 1;
}
y = j;
if (board[x][y].aliveLastGen){
neighbors += 1;
}
y = ((j + 1) == board[x].length) ? 0 : j + 1;
if (board[x][y].aliveLastGen){
neighbors += 1;
}
cell = board[i][j];
var before = cell.aliveLastGen;
if(before){
if((neighbors == 2) || (neighbors == 3)){
//Any live cell with two or three live neighbours lives on to the next generation.
cell.alive = true;
}
else{
//Any live cell with fewer than two live neighbours dies, as if caused by under-population.
//Any live cell with more than three live neighbours dies, as if by overcrowding.
cell.alive = false;
}
}
else{
if (neighbors == 3){
//Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
cell.alive = true;
}
}
//console.log("[" + i + "][" + j + "]: " + before + " --> " + cell.alive);
}
}
}
function randomBirth(board, cells){
randomX = 0;
randomY = 0;
for(var i=0; i < cells;){
randomX = Math.floor((Math.random()*board.length));
randomY = Math.floor((Math.random()*board[randomX].length));
if(!board[randomX][randomY].alive){
board[randomX][randomY].alive = true;
i++;
}
}
}
var canvas = document.getElementById("game_of_life_canvas");
var numSquares = 150;
var dimension = Math.min(window.innerWidth, window.innerHeight);
canvas.height = canvas.width = dimension;
var board = drawLifeBoard(canvas, numSquares);
var interval = 50;
//randomBirth(board, 997);
drawGeneration(canvas, numSquares, board);
setInterval(function(){
processRulesOfLife(board);
if(interval-- <= 0) {
randomBirth(board, 50);
interval = 50;
}
drawGeneration(canvas, numSquares, board);
}, 100);
</script>
</body>
</html>