-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode.js
129 lines (110 loc) · 3.23 KB
/
node.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
export default class NODE {
static height = 25;
static width = 25;
constructor(row, col, ele, state = 0){
this.state = state;
this.neighbor_count = 0;
this.neighbors = [];
this.row = row;
this.col = col;
this.element = ele;
// visually reflect that node is alive
if(this.state === 1){
this.element.classList.add('alive');
}
// create and bind an event listener to these nodes
ele.addEventListener('click', this.clicked.bind(this));
ele.addEventListener('pointerenter', this.drag.bind(this));
}
clicked(){
// toggle the cell from alive to dead and dead to alive
if(this.isAlive){
this.#die();
}else{
this.#born();
}
}
drag(e){
if(e.buttons === 1){
// toggle the cell from alive to dead and dead to alive
if(this.isAlive){
this.#die();
}else{
this.#born();
}
}
}
addNeighbor(cell){
// this looks simple but look in conway.js,
// how many neighbors a simple cell might have
this.neighbors.push(cell);
if(cell.isAlive){
this.neighbor_count += 1;
}
}
get isAlive(){
return this.state === 1;
}
#let_live(){
this.live = 1;
}
#let_die(){
this.live = 0;
}
get willLive(){
return this.live === 1;
}
#born(){
if(!this.isAlive){
// signifies an alive cell
this.state = 1;
// reflect change in state with element becoming green in color
if(!this.element.classList.contains('add')){
this.element.classList.add('alive');
}
// update each of the neighbors that their neighbor is alive
// they may quarrel over pety things in life
this.neighbors.forEach(neighbor => {
neighbor.neighbor_count += 1;
});
}
}
#die(){
if(this.isAlive){
// signifies a dead cell
this.state = 0;
// reflect change in state by element becoming colorless
if(this.element.classList.contains('alive')){
this.element.classList.remove('alive');
}
// update each of the neighbors that their neighbor is dead
// they may mourn the passing of their fellow cell for upto 4 step counts
this.neighbors.forEach(neighbor => {
neighbor.neighbor_count -= 1;
});
}
}
check(){
if(this.state === 1){
if(this.neighbor_count <= 1 || this.neighbor_count >= 4){
// cell will die of loneliness OR cell die of overpopulation
this.#let_die();
}else{
// cell lives for another generation
this.#let_live();
}
}else{
if(this.neighbor_count === 3){
// the miracle of life
this.#let_live();
}
}
}
step(){
if(this.willLive){
this.#born();
}else{
this.#die();
}
}
}