-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconway.js
More file actions
297 lines (255 loc) · 8.03 KB
/
conway.js
File metadata and controls
297 lines (255 loc) · 8.03 KB
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
291
292
293
294
295
296
297
// abbreviate "Math.floor" to "floor":
const floor = Math.floor;
/**
* State and behavior of an instance of Conway's Game of Life.
*
* @class Game game
*/
class Game {
/**
* Constructs a Game object.
*
* @param {object} context The JS canvas context
* @param {number} width The game's width
* @param {number} height The game's height
*/
constructor(context, width = 0, height = 0) {
// initialize graphic constants:
this.ctx = context;
this.width = width;
this.height = height;
this.cellSize = 0.2 * (width ** 0.55) || 1;
this.nRows = floor(height / this.cellSize);
this.nColumns = this.nRows + floor((width - height) / this.cellSize);
this.xCenteringOffset = (width - this.nColumns * this.cellSize) / 2;
this.yCenteringOffset = (height - this.nRows * this.cellSize) / 2;
this.cellPadding = 0.01 * this.cellSize;
this.animation = null;
// make sure all cells are set to zero:
this.reset();
}
/**
* Resets the state of the game to all dead cells.
*/
reset() {
this.cells = new Array(this.nRows);
for (let row = 0; row < this.cells.length; row++) {
this.cells[row] = new Array(this.nColumns);
for (let col = 0; col < this.cells[row].length; col++) {
this.cells[row][col] = 0;
}
}
}
/**
* Draws all the game, based on its current state.
*/
draw() {
// erase everything on the canvas:
this.ctx.fillStyle = rgb(30, 30, 30);
this.ctx.fillRect(0, 0, this.width, this.height);
// draw each cell:
for (const row in this.cells)
for (const col in this.cells[row])
this.drawCell(row, col);
}
/**
* Draws the cell at a specified (row, column) position. The color of
* the cell is determined by the value of its corresponding cell.
*
* @param {number} row The cell's row
* @param {number} col The cell's column
*/
drawCell(row, col) {
// declare some useful geometric constants:
const xOffset = this.xCenteringOffset + this.cellPadding;
const yOffset = this.yCenteringOffset + this.cellPadding;
const cellInnerSize = this.cellSize - this.cellPadding * 2;
// calculate the coordinates of the cell's enclosing rectangle:
const y = row * this.cellSize + yOffset;
const x = col * this.cellSize + xOffset;
// choose a color for the cell:
const color = this.cells[row][col];
this.ctx.fillStyle = rgb(0, 200 * color, 140 * color);
// draw the cell:
smoothSquare(this.ctx, x, y, cellInnerSize);
this.ctx.fill();
}
/**
* Sets a specified cell to a certain value.
*
* @param {number} row The cell's row
* @param {number} col The cell's column
* @param {number} [value=1] The new value
*/
setCell(row, col, value = 1) {
if (row >= 0 && row < this.nRows && col >= 0 && col < this.nColumns)
this.cells[row][col] = value;
}
/**
* Returns the theoretical neighbors of a cell at position (row, col),
* regardless of whether the cell is at the border or not.
*
* @param {number} row The cell's row
* @param {number} col The cell's column
* @return {Array} Theoretical neighbors of the cell.
*/
potentialNeihgbors(row, col) {
return [
{x: row - 1, y: col - 1},
{x: row - 1, y: col },
{x: row - 1, y: col + 1},
{x: row, y: col - 1},
{x: row, y: col + 1},
{x: row + 1, y: col - 1},
{x: row + 1, y: col },
{x: row + 1, y: col + 1}
];
}
/**
* Returns all the neighbors of a specified cell.
*
* @param {number} row The cell's row
* @param {number} col The cell's col
* @return {Array} The cell's neighbors.
*/
getCellNeighbors(row, col) {
// make sure that row and column are numbers:
row = Number(row);
col = Number(col);
// calculate the cell's neighbors, disregarding the borders:
const candidates = this.potentialNeihgbors(row, col);
// only consider the neighbors that are inside the borders:
let neighbors = [];
for (const cell of candidates) {
const x = cell.x;
const y = cell.y;
if (x >= 0 && x < this.nRows && y >= 0 && y < this.nColumns)
neighbors.push(this.cells[x][y]);
}
// return the list of neighbors:
return neighbors;
}
/**
* Updates the state of the game, based on the rules of Conway's Game of
* Life.
*/
tick() {
// create a deep copy of the game's current state:
const newState = new Array(this.nRows);
for (const row in this.cells)
newState[row] = this.cells[row].slice();
// calculate the next state of each cell:
for (const row in newState) {
for (const col in newState[row]) {
const neighbors = this.getCellNeighbors(row, col);
let life = 0;
for (const cell of neighbors)
life += cell;
if (life == 3 && this.cells[row][col] < 1)
newState[row][col] = 1;
if (life > 3 && this.cells[row][col] > 0)
newState[row][col] = 0;
if (life < 2 && this.cells[row][col] > 0)
newState[row][col] = 0;
}
}
// update the state of the game and draw the whole game:
this.cells = newState;
this.draw();
}
/**
* Starts to animate the game, optionally resetting its state to a random
* value.
*
* @param {boolean} [setState=false] Whether to reset the state
*/
start(setState = false) {
// if specified, initialize the new game with a random state:
if (setState) this.setRandomState();
// draw the game:
this.draw();
// set the game's state to be updated every second:
this.resume();
}
/**
* Pauses the game if it is not paused.
*/
pause() {
if (this.animation !== null) {
window.clearInterval(this.animation);
this.animation = null;
}
}
/**
* Resumes the game if it was paused.
*/
resume() {
if (this.animation === null) {
this.tick();
this.animation = window.setInterval(this.tick.bind(this), 1000);
}
}
/**
* Resets the state of the game to a random state where `nClusters` number
* of clusters (neighboring cells) will be randomly set to 1 or 0.
*
* @param {number} [nClusters=15] The number of clusters
*/
setRandomState(nClusters = 15) {
// erase all cells.
this.reset();
// randomly turn some cells on.
for (let i = 0; i < nClusters; i++) {
const row = floor(Math.random() * this.nRows);
const col = floor(Math.random() * this.nColumns);
const neighbors_1 = this.potentialNeihgbors(row + 1, col);
const neighbors_2 = this.potentialNeihgbors(row, col + 1);
for (const cell of [...neighbors_1, ...neighbors_2]) {
const randomState = floor(Math.random() >= 0.5);
this.setCell(cell.x, cell.y, randomState);
}
}
}
}
/**
* Returns a string that represents a valid CSS color, based on the absolute
* amount of red, green, and blue provided as integers in [0, 256).
*
* @param {string} red The absolute amount of red
* @param {string} green The absolute amount of green
* @param {string} blue The absolute amount of blue
* @return {string} The string representation of a valid CSS color.
*/
function rgb(red, green, blue) {
return 'rgb(' + red + ', ' + green + ', ' + blue + ')';
}
/**
* Draws the path of a square with smooth vertices, specified by the
* position of its left edge (x), the position of its upper edge (y), and
* its side (side). Note that you are responsible for filling or stroking
* the path after having called `smoothSquare`.
*
* @param {object} ctx The JS canvas context
* @param {number} x The position of the left edge
* @param {number} y The position of the upper edge
* @param {number} side The square's side
*/
function smoothSquare(ctx, x, y, side) {
// set the border radius:
const radius = 3;
// calculate the right and bottom edges:
const r = x + side;
const b = y + side;
// draw a line path:
ctx.beginPath();
ctx.moveTo(x + radius, y);
ctx.lineTo(r - radius, y);
ctx.quadraticCurveTo(r, y, r, y + radius);
ctx.lineTo(r, b - radius);
ctx.quadraticCurveTo(r, b, r - radius, b);
ctx.lineTo(x + radius, b);
ctx.quadraticCurveTo(x, b, x, b - radius);
ctx.lineTo(x, y + radius);
ctx.quadraticCurveTo(x, y, x + radius, y);
}
module.exports = Game;