-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.js
235 lines (205 loc) · 4.75 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
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
const WIDTH = 400;
const ROWS = 20;
const COLS = 20;
const TILEWIDTH = WIDTH/ROWS;
const DELAY = 100;
let grid;
let savedGrid = null;
let mode = 'edit'; // simulate
let timestamp = 0;
let generation = 0;
const GLIDER = makeConstellation(0, 0, [0, 2], [1, 0], [1, 2], [2, 1], [2, 2]);
const SHIP = makeConstellation(0, 0, [0,0], [0,3], [1,4], [2,0], [2,4], [3,1], [3,2], [3,3], [3,4]);
const TOAD = makeConstellation(COLS/3, ROWS/4, [0,2], [1,0], [1,3], [2,0], [2,3], [3,1]);
// const CATHION = makeConstellation([[0,1], [1,0], [1,1], [1,2], []])
const DATA = [GLIDER, SHIP, TOAD];
function setup() {
createCanvas(WIDTH, WIDTH);
frameRate(20);
textSize(10);
textAlign(CENTER, CENTER);
grid = makeGrid(ROWS, COLS);
initGrid();
recordGrid();
}
function draw() {
background(0);
for (let row of grid) {
for (let cell of row) {
cell.draw();
}
}
if (mode == 'simulate') {
now = millis();
if (now - timestamp > DELAY) {
timestamp = now;
for (let row of grid) {
for (let cell of row) {
cell.changeState();
}
}
nextGeneration(grid);
}
describe('Simulating @Generation : ' + generation + ' [ press SPACE to stop ]', LABEL);
} else {
describe('Editing [ "SPACE"-run, "p"-replay, "R"-refresh, "M_1"-Add, "M_2"-Remove "1, 2, ..."-Demos ]', LABEL);
}
}
function keyPressed() {
if (key == ' ') {
if (mode == 'edit') {
mode = 'simulate';
recordGrid();
} else mode = 'edit';
generation = 0;
}
if (!(mode == 'edit')) return;
if (key == 'n')
nextGeneration(grid);
if (key == 'r') {
generation = 0;
randomizeGrid();
}
if (key == 'p')
loadGrid(savedGrid);
numKey = parseInt(key)
if (!(isNaN(numKey))) {
index = numKey - 1;
if (!(index < 0 || index >= DATA.length))
loadGrid(DATA[index]);
}
}
function mouseDragged() {
if (mode !== 'edit') return;
editGrid();
}
function mousePressed() {
if (mode !== 'edit') return;
editGrid();
}
function makeGrid(rows, cols) {
let arr = new Array(rows);
for (let i = 0; i < rows; i++) {
let row = new Array(cols);
arr[i] = row;
}
return arr;
}
function initGrid(){
for (let i = 0; i < ROWS; i++) {
for (let j = 0; j < COLS; j++) {
grid[i][j] = new Cell(i, j);
}
}
}
function editGrid() {
const row = floor(mouseY / TILEWIDTH);
const col = floor(mouseX / TILEWIDTH);
if (row < 0 || row >= ROWS || col < 0 || col >= COLS) return;
if (mouseButton == LEFT) {
grid[row][col].state = 1;
grid[row][col].gen = 0;
} else if (mouseButton == CENTER) {
grid[row][col].state = 0;
grid[row][col].gen = 0;
}
}
function recordGrid(){
savedGrid = makeGrid(ROWS, COLS);
for (let i = 0; i < ROWS; i++) {
for (let j = 0; j < COLS; j++) {
if (grid[i][j].state == 1) savedGrid[i][j] = 1;
else savedGrid[i][j] = 0;
}
}
print('Current grid is saved successfully.');
// console.table(savedGrid);
}
function randomizeGrid() {
for (let row of grid) {
for (let cell of row) {
cell.randomState();
cell.gen = 0;
}
}
}
function loadGrid(data) {
for (let i = 0; i < ROWS; i++) {
for (let j = 0; j < COLS; j++) {
grid[i][j].gen = 0;
if (data[i][j] === 1) grid[i][j].state = 1;
else grid[i][j].state = 0;
}
}
}
function makeConstellation(x, y, ...coors) {
let arr = makeGrid(ROWS, COLS);
for (let index of coors) {
let row = index[0] + Math.ceil(y);
let col = index[1] + Math.ceil(x);
arr[row][col] = 1;
}
return arr;
}
function totalNeighbors(row, col) {
let count = 0;
for (let i = -1; i < 2; i++) {
for (let j = -1; j < 2; j++) {
if (i == 0 && j == 0) continue;
if (row+i < 0 || row+i >= ROWS || col+j < 0 || col+j >= COLS) continue;
let state = grid[row+i][col+j].state;
if (state == 1 || state == 2) count++;
}
}
return count;
}
function nextGeneration() {
for (let row of grid) {
for (let cell of row) {
let neighbors = totalNeighbors(cell.row, cell.col);
cell.nextState(neighbors);
}
}
generation++;
}
class Cell {
constructor(row, col) {
this.row = row;
this.col = col;
this.state = 0;
this.gen = 0;
this.randomState();
}
draw() {
let color = [0, 0, 0];
if (this.state == 1) {
color = [10, 200, 30];
} else if (this.state == 2) {
color = [200, 20, 20];
} else if (this.state == 3) {
color = [0, 150, 200];
}
fill(...color);
rect(this.col*TILEWIDTH, this.row*TILEWIDTH, TILEWIDTH, TILEWIDTH, TILEWIDTH/4);
if (this.gen > 0) {
fill(255);
text(this.gen, this.col*TILEWIDTH+TILEWIDTH/2, this.row*TILEWIDTH+TILEWIDTH/2);
}
}
randomState() {
this.state = floor(random(2));
}
nextState(n) {
if (this.state == 1 && (n > 3 || n < 2)) {
this.state = 2;
} else if (this.state == 0 && n == 3) {
this.state = 3;
}
}
changeState() {
if (this.state === 1) this.gen ++;
else this.gen = 0;
if (this.state === 2) this.state = 0;
else if (this.state === 3) this.state = 1;
}
}