-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
278 lines (260 loc) · 8.7 KB
/
Copy pathmain.js
File metadata and controls
278 lines (260 loc) · 8.7 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
let GameOfLife = function (p) {
let grid
let rows, cols
let canvas
let mouseMotion = false
let mousedown = 0
let running = true
let generation = 0
let size = 600
let scale = 10
let generationElem = document.getElementById('generation')
let clearButton = document.getElementById('clearButton')
let stopButton = document.getElementById('stopStartButton')
let stepButton = document.getElementById('stepButton')
let randomButton = document.getElementById('randomButton')
let buttonRow = document.getElementsByClassName('buttonRow')[0]
p.setup = () => {
if (p.windowWidth < size) {
size = Math.round(p.windowWidth / 10) * 10 - 100
scale = 10
}
canvas = p.createCanvas(size, size)
canvas.mouseReleased(() => {
mouseMotion = false
})
canvas.mousePressed(() => {
mouseMotion = true
fillClickedGrid(true)
})
canvas.mouseMoved(() => {
if (mouseMotion) {
fillClickedGrid(false)
}
})
cols = p.width / scale
rows = p.height / scale
grid = makeRandomGrid(rows, cols)
clearButton.addEventListener('click', () => {
grid = makeEmptyGrid(rows, cols)
generation = 0
})
stopButton.addEventListener('click', () => {
if (running) {
stopButton.innerText = 'start'
stopButton.classList.remove('red')
stopButton.classList.add('green')
M.toast({html: 'click cell to select', classes: 'rounded'})
} else {
stopButton.innerText = 'stop'
stopButton.classList.remove('green')
stopButton.classList.add('red')
M.Toast.dismissAll()
}
running = !running
})
stepButton.addEventListener('click', () => {
updateGrid()
})
randomButton.addEventListener('click', () => {
grid = makeRandomGrid(rows, cols)
generation = 0
})
// prevent double-click highlight
makeUnselectable(clearButton)
makeUnselectable(stopButton)
makeUnselectable(stepButton)
makeUnselectable(randomButton)
// makes buttons look pretty on mobile
if (p.windowWidth < 750) {
buttonRow.classList.remove('row')
}
}
p.draw = () => {
p.background('#455a64')
generationElem.innerText = "Generation: " + generation
drawGrid(grid)
if (running) {
updateGrid()
}
}
function fillClickedGrid(toggleOn) {
let rowClicked
let colClicked
if (p.mouseX >= 0 && p.mouseX <= p.width && p.mouseY >= 0 && p.mouseY <= p.height) {
// round down to nearest 10 & divide by scale
rowClicked = Math.floor((Math.floor(p.mouseY / 10) * 10) / scale)
colClicked = Math.floor((Math.floor(p.mouseX / 10) * 10) / scale)
// toggle grid location
let state = grid[rowClicked][colClicked]
if (toggleOn) {
grid[rowClicked][colClicked] = state ? 0 : 1
} else {
grid[rowClicked][colClicked] = 1
}
drawGrid(grid)
}
}
// creates 2D array filled with 1's & 0's
function makeRandomGrid(rows, cols) {
let arr = new Array()
for (let i = 0; i < rows; i++) {
arr.push(new Array())
for (let j = 0; j < cols; j++) {
arr[i].push(Math.floor(p.random(2)))
}
}
return arr
}
// creates 2D array filled with 1's & 0's
function makeEmptyGrid(rows, cols) {
let arr = new Array()
for (let i = 0; i < rows; i++) {
arr.push(new Array())
for (let j = 0; j < cols; j++) {
arr[i].push(0)
}
}
return arr
}
function drawGrid(grid) {
let x, y
for (let i = 0; i < grid.length; i++) {
for (let j = 0; j < grid[i].length; j++) {
y = i * scale
x = j * scale
if (grid[i][j] === 1) {
p.fill('#2196f3')
p.rect(x, y, scale - 1, scale - 1)
} else {
p.fill('#455a64')
p.rect(x, y, scale - 1, scale - 1)
}
}
}
}
function updateGrid() {
let oldGrid = new Array()
// copy grid into oldGrid
for (let i = 0; i < grid.length; i++) {
oldGrid.push(new Array())
for (let j = 0; j < grid[i].length; j++) {
oldGrid[i].push(grid[i][j])
}
}
let n // number of neighbors
let state // live or dead
for (let i = 0; i < grid.length; i++) {
for (let j = 0; j < grid[i].length; j++) {
n = countNeighbors(i, j, oldGrid)
state = oldGrid[i][j]
/* Rule #1
* Any live cell with fewer than two live neighbors dies,
* as if by under population.
*/
if (state == 1 && n < 2) {
grid[i][j] = 0
}
/* Rule #2
* Any live cell with two or three live neighbors lives on
* to the next generation.
*/
else if (state === 1 && (n === 2 || n === 3)) {
grid[i][j] = 1
}
/* Rule #3
* Any live cell with more than three live neighbors dies,
* as if by overpopulation.
*/
else if (state === 1 && n > 3) {
grid[i][j] = 0
}
/* Rule #4
* Any dead cell with exactly three live neighbors becomes
* a live cell, as if by reproduction.
*/
else if (state === 0 && n === 3) {
grid[i][j] = 1
}
}
}
generation++
}
function countNeighbors(row, col, g) {
let sum = 0
// top row
if (row === 0) {
// down
sum += g[1][col]
if (col != 0) {
// left
sum += g[0][col - 1]
// left diagonal
sum += g[1][col - 1]
}
if (col != g[0].length - 1) {
// right
sum += g[0][col + 1]
// right diagonal
sum += g[1][col + 1]
}
}
// bottom row
else if (row === g.length - 1) {
// up
sum += g[row - 1][col]
if (col != 0) {
// left
sum += g[row][col - 1]
// left diagonal
sum += g[row - 1][col - 1]
}
if (col != g[row].length - 1) {
// right
sum += g[row][col + 1]
// right diagonal
sum += g[row - 1][col + 1]
}
}
// left column
else if (col === 0) {
for (let i = -1; i < 2; i++) {
for (let j = 0; j < 2; j++) {
sum += g[row + i][col + j]
}
}
sum -= g[row][col]
}
// right column
else if (col === g[row].length - 1) {
for (let i = -1; i < 2; i++) {
for (let j = -1; j < 1; j++) {
sum += g[row + i][col + j]
}
}
sum -= g[row][col]
}
else {
for (let i = -1; i < 2; i++) {
for (let j = -1; j < 2; j++) {
sum += g[row + i][col + j]
}
}
sum -= g[row][col]
}
return sum
}
// Stack Overflow solution
//https://stackoverflow.com/questions/880512/prevent-text-selection-after-double-click
function makeUnselectable(elem) {
if (typeof (elem) == 'string')
elem = document.getElementById(elem);
if (elem) {
elem.onselectstart = function () { return false; };
elem.style.MozUserSelect = "none";
elem.style.KhtmlUserSelect = "none";
elem.unselectable = "on";
}
}
}
var game = new p5(GameOfLife, 'sketch')