-
Notifications
You must be signed in to change notification settings - Fork 69
/
conway-game-of-life-2D.html
180 lines (149 loc) · 4.11 KB
/
conway-game-of-life-2D.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
<!DOCTYPE html>
<html>
<head>
<title>Conway's Game of Life</title>
</head>
<body>
<center>
<h1>Conway's Game of Life</h1>
<canvas id="canvas" width=512 height=512></canvas>
</center>
<script>
// "fixed" mod function
Number.prototype.mod = function(n)
{
return ((this%n)+n)%n;
}
class Cell
{
constructor()
{
this.prevState = null; // used to detect change (need to draw) when rendering?
this.currentState = 0; // 0:dead, 1:alive
this.nextState = null; // used to avoid creating a second board
this.count = 0; // # of live neighbors
}
}
let canvas = document.getElementById("canvas");
let context = canvas.getContext("2d");
let canvasSize = 512;
let boardSize = 128;
let cellSize = canvasSize / boardSize;
let DEAD = 0;
let ALIVE = 1;
let cellColors = { 0: "#DDDDDD", 1: "#0088FF" };
// used to identify cell neighbors
let offsets = [ {dx:1, dy:0}, {dx:1, dy:1}, {dx:0, dy:1}, {dx:-1, dy:1},
{dx:-1, dy:0}, {dx:-1, dy:-1}, {dx:0, dy:-1}, {dx:1, dy:-1} ];
// board: 2D array of cells
let board = null;
function initialize()
{
// initialize board
board = [];
for (let rowNumber = 0; rowNumber < boardSize; rowNumber++)
{
let row = [];
for (let columnNumber = 0; columnNumber < boardSize; columnNumber++)
{
let cell = new Cell();
if ( Math.random() < 0.30 )
cell.currentState = 1;
else
cell.currentState = 0;
row.push( cell );
}
board.push(row);
}
// initialize canvas
context.fillStyle = "#DDDDDD";
context.fillRect(0,0, 512,512);
context.strokeStyle = "#BBBBBB";
context.lineWidth = 1;
// draw lines across rows
for (let rowNumber = 0; rowNumber < boardSize; rowNumber++)
{
context.beginPath();
context.moveTo(0, rowNumber * cellSize + 0.5);
context.lineTo(canvasSize, rowNumber * cellSize + 0.5);
context.closePath();
context.stroke();
}
// draw lines down columns
for (let columnNumber = 0; columnNumber < boardSize; columnNumber++)
{
context.beginPath();
context.moveTo(columnNumber * cellSize + 0.5, 0);
context.lineTo(columnNumber * cellSize + 0.5, canvasSize);
context.closePath();
context.stroke();
}
}
function update()
{
// count adjacent neighbors currentState
for (let rowNumber = 0; rowNumber < boardSize; rowNumber++)
{
for (let columnNumber = 0; columnNumber < boardSize; columnNumber++)
{
let cell = board[rowNumber][columnNumber];
// count neighbors whose current state is ALIVE
cell.count = 0;
for (let i=0; i<8; i++)
{
if (board[ (rowNumber + offsets[i].dy).mod(boardSize) ][ (columnNumber + offsets[i].dx).mod(boardSize) ].currentState == ALIVE)
cell.count += 1;
}
if (cell.count < 2) // death by underpopulation
cell.nextState = 0;
else if (cell.count == 2) // stable
cell.nextState = cell.currentState;
else if (cell.count == 3) // birth
cell.nextState = 1;
else // (cell.count > 3) // death by overpopulation
cell.nextState = 0
}
}
// update current states to next states
for (let rowNumber = 0; rowNumber < boardSize; rowNumber++)
{
for (let columnNumber = 0; columnNumber < boardSize; columnNumber++)
{
let cell = board[rowNumber][columnNumber];
cell.prevState = cell.currentState;
cell.currentState = cell.nextState;
cell.nextState = null;
}
}
}
function render()
{
// console.log("Rendering...");
for (let rowNumber = 0; rowNumber < boardSize; rowNumber++)
{
for (let columnNumber = 0; columnNumber < boardSize; columnNumber++)
{
let cell = board[rowNumber][columnNumber];
let canvasY = rowNumber * cellSize;
let canvasX = columnNumber * cellSize;
// only redraw if cell has changed state
if (cell.currentState != cell.prevState)
{
context.fillStyle = cellColors[ cell.currentState ];
context.fillRect(canvasX+1, canvasY+1, cellSize-1, cellSize-1);
}
}
}
}
function gameloop()
{
update();
render();
setTimeout( gameloop, 50 );
}
// run the code
initialize();
gameloop();
</script>
</body>
</html>