-
Notifications
You must be signed in to change notification settings - Fork 900
/
Copy pathsketch.js
172 lines (151 loc) · 3.96 KB
/
sketch.js
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
// Conway's Game of life
// RULES:
// 1. Any live cell with fewer than two live neighbours dies, as if by underpopulation.
// 2. Any live cell with two or three live neighbours lives on to the next generation.
// 3. Any live cell with more than three live neighbours dies, as if by overpopulation.
// 4. Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
let WIDTH;
let HEIGHT;
let COLS;
let ROWS;
const SIZE = 10;
const SPACE_KEY_CODE = 32;
const ENTER_KEY_CODE = 13;
var grid = []; // 2D array
function Cell(i, j) {
// each Cell contains a number of living neighbors and its state (alive or dead)
this.i = i;
this.j = j;
this.isDead = random(1) < 0.5 ? true : false;
this.neighbors;
this.show = function () {
noStroke();
if (this.isDead) color = 0;
else color = [0, 153, 51];
fill(color);
rect(this.i * SIZE, this.j * SIZE, SIZE);
};
this.getNeighbors = function (grid) {
// count living neighbors
this.neighbors = 0;
const pos = [-1, 0, 1];
for (var x of pos) {
for (var y of pos) {
if (x != 0 || y != 0) {
var i = (this.i + x + COLS) % COLS;
var j = (this.j + y + ROWS) % ROWS;
if (!grid[i][j].isDead) this.neighbors++;
}
}
}
};
this.update = function (grid) {
// follow the rules
var i = this.i;
var j = this.j;
if (this.isDead) {
// if it's dead and there are 3 living neighbors
if (grid[i][j].neighbors == 3) {
this.isDead = false; // revive it
}
} else {
if (grid[i][j].neighbors < 2 || grid[i][j].neighbors > 3) {
// if it's alive and there are less than 2 or
this.isDead = true; // greater than 3 living neighbors => kill it
}
}
};
this.clicked = function () {
// click on the cell with revive it
var centerX = this.i * SIZE + SIZE / 2;
var centerY = this.j * SIZE + SIZE / 2;
var d = dist(mouseX, mouseY, centerX, centerY);
if (d <= SIZE / 2) {
this.isDead = false;
}
};
}
function windowResized() {
// resize canvas
resizeCanvas(windowWidth, windowHeight);
WIDTH = windowWidth;
HEIGHT = windowHeight;
COLS = Math.floor(WIDTH / SIZE);
ROWS = Math.floor(HEIGHT / SIZE);
grid = randomGrid();
}
function setup() {
WIDTH = windowWidth;
HEIGHT = windowHeight;
createCanvas(WIDTH, HEIGHT);
COLS = Math.floor(WIDTH / SIZE);
ROWS = Math.floor(HEIGHT / SIZE);
grid = randomGrid();
}
function mousePressed() {
// get mouse press
loop();
for (var i = 0; i < COLS; i++) {
for (var j = 0; j < ROWS; j++) {
grid[i][j].clicked();
}
}
}
function draw() {
background(0);
for (var i = 0; i < COLS; i++) {
for (var j = 0; j < ROWS; j++) {
grid[i][j].getNeighbors(grid); // count all the living neighbors
}
}
for (var i = 0; i < COLS; i++) {
for (var j = 0; j < ROWS; j++) {
grid[i][j].show(color); // show
}
}
var oldGrid = copyGrid(grid); // copy the grid for the update
for (var i = 0; i < COLS; i++) {
for (var j = 0; j < ROWS; j++) {
grid[i][j].update(oldGrid); // update the cell
}
}
}
function copyGrid(grid) {
var currentGrid = new Array(COLS);
for (var i = 0; i < COLS; i++) {
currentGrid[i] = new Array(ROWS);
}
for (var i = 0; i < COLS; i++) {
for (var j = 0; j < ROWS; j++) {
currentGrid[i][j] = Object.assign({}, grid[i][j]);
}
}
return currentGrid;
}
function randomGrid() {
// initialize a random grid
var grid = new Array(COLS);
for (let i = 0; i < COLS; i++) {
grid[i] = new Array(ROWS);
}
for (var i = 0; i < COLS; i++) {
for (var j = 0; j < ROWS; j++) {
grid[i][j] = new Cell(i, j);
}
}
return grid;
}
// press SPACE to stop/continue the loop and ENTER to replay
function keyPressed() {
if (keyCode === SPACE_KEY_CODE) {
if (isLooping()) {
noLoop();
} else {
loop();
}
}
if (keyCode === ENTER_KEY_CODE) {
window.location.reload();
}
return false;
}