-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch_old.js
150 lines (116 loc) · 3.75 KB
/
sketch_old.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
'use strict';
const ROWS = 10;
const COLS = 10;
const CELL_SIZE = 40;
let grid;
class Cell {
constructor(x, y, size, state) {
this.x = x;
this.y = y;
this.size = size;
this.state = state;
this.color = (this.state == 0) ? color(0) : color(255);
this.neighborCount = undefined;
}
render() {
stroke(0);
fill(this.color);
rect(this.x, this.y, this.size - 1, this.size - 1);
}
}
class Grid {
constructor(cols, rows, size) {
this.rows = rows;
this.cols = cols;
this.size = size;
this.cells = create2DArray(this.cols, this.rows);
this.currentStates = create2DArray(this.cols, this.rows);
this.newStates = this.currentStates;
}
// create() {
// for (let i = 0; i < this.cols; i++) {
// this.cells[i] = new Array(this.rows);
// }
// }
intitalize() {
for (let i = 0; i < this.cols; i++) {
for (let j = 0; j < this.rows; j++) {
this.currentStates[i][j] = floor(random(2));
let cellX = i * CELL_SIZE;
let cellY = j * CELL_SIZE;
let cellState = this.currentStates[i][j]
this.cells[i][j] = new Cell(cellX, cellY, CELL_SIZE, cellState);
}
}
}
render() {
for (let i = 0; i < this.cols; i++) {
for (let j = 0; j < this.rows; j++) {
this.cells[i][j].render();
}
}
}
// countNeighbors() {
// }
nextGeneration() {
//this.nextCells = create2DArray(this.cols, this.rows);
for (let i = 0; i < this.cols; i++) {
for (let j = 0; j < this.rows; j++) {
let currentCell = this.cells[i][j];
//let futureCell = this.nextCells[i][j];
let liveNeighbors = 0;
for (let m = -1; m <= 1; m++) {
for (let n = -1; n <= 1; n++) {
let col = (currentCell.x + m + this.cols) % this.cols;
let row = (currentCell.y + m + this.rows) % this.rows;
let neighborCell = this.cells[col][row];
liveNeighbors += neighborCell.state;
}
}
liveNeighbors -= currentCell.state;
// if (currentCell.state == 1) {
// if (liveNeighbors < 2 || liveNeighbors > 3) {
// //futureCell = new Cell(currentCell.x, currentCell.y, currentCell.size, 1);
// this.newStates[i][j] = 0;
// }
// } else if (currentCell.state == 0) {
// if (liveNeighbors == 3) {
// //futureCell = new Cell(currentCell.x, currentCell.y, currentCell.size, 1);
// this.newStates[i][j] = 1;
// }
// } else {
// //futureCell = new Cell(currentCell.x, currentCell.y, currentCell.size, currentCell.state);
// this.newStates[i][j] = this.currentStates[i][j];
// }
let state = this.currentStates[i][j];
if (state == 1 && (liveNeighbors == 2) || (liveNeighbors == 3)) {
this.newStates[i][j] == 1;
} else if (state == 0 && liveNeighbors == 3) {
this.newStates[i][j] == 1;
} else {
this.newStates[i][j] == 0;
}
//this.nextCells.push(futureCell);
}
}
//this.cells = this.nextCells;
}
}
function create2DArray(rows, cols) {
let arr = new Array(cols);
for (let i = 0; i < cols; i++) {
arr[i] = new Array(rows);
}
return arr;
}
function setup() {
createCanvas(COLS * CELL_SIZE, ROWS * CELL_SIZE);
grid = new Grid(COLS, ROWS, CELL_SIZE);
//grid.create();
grid.intitalize();
}
function draw() {
background(251);
grid.nextGeneration();
grid.render();
}