-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cell.java
67 lines (54 loc) · 1.47 KB
/
Cell.java
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
import processing.core.*;
import java.util.HashSet;
public class Cell {
final int CELL_SIZE = 30;
PApplet parent;
int colorTestColor;
int lastFlipped = 0;
boolean state = false;
boolean nextState;
float x, y, z;
HashSet<Cell> neighbors = new HashSet<Cell>();
public Cell(PApplet parent, float x, float y, float z, boolean state) {
this.parent = parent;
this.x = x;
this.y = y;
this.z = z;
this.state = state;
}
public String toString() {
return "Cell: " + x + "," + y + "," + z;
}
public void connect(Cell other) {
neighbors.add(other);
}
public void computeNextState() {
int count = 0;
for(Cell c : neighbors) {
count += c.state ? 1 : 0;
}
if(count == 3) nextState = true;
else if(count == 2 && state) nextState = true;
else nextState = false;
}
public void gotoNextState() {
state = nextState;
}
public void draw(int liveColor, int deadColor) {
parent.stroke(0xFFFFFFFF);
parent.fill(state ? liveColor : deadColor);
parent.pushMatrix();
parent.translate(x*CELL_SIZE, y*CELL_SIZE, z*CELL_SIZE);
parent.box(CELL_SIZE);
parent.popMatrix();
}
public void draw(int colorTestColor) {
this.colorTestColor = colorTestColor;
parent.noStroke();
parent.fill(colorTestColor);
parent.pushMatrix();
parent.translate(x*CELL_SIZE, y*CELL_SIZE, z*CELL_SIZE);
parent.box(CELL_SIZE);
parent.popMatrix();
}
}