-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
155 lines (128 loc) · 3.55 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
var gridSize = 50
var arraySize = (gridSize*gridSize)
var chanceOfLife = 0.25
//the "isAlive" array stores true/false for each of our cells telling us which ones are alive:
var isAlive = []
var nextTurnIsAlive = []
function setup() {
createCanvas(windowWidth, windowWidth);
//to start, we randomly assign each cell as living or dead (boolean true or false)
for(var i = 0;i<arraySize;i++){
var alive = (Math.random() < chanceOfLife)
isAlive.push(alive)
}
frameRate(10)
}
function draw() {
background(220);
//each frame we show the cells that are alive
showAliveCells()
//then we determine which ones should be alive next turn and update our array
decideWhichCellShouldBeAliveNextTurn()
}
function showAliveCells(){
//we loop through our entire array and draw a black square if it is alive
var cellWidth = width/gridSize;
var x = 0;
var y = 0;
for(var i = 0;i<arraySize;i++){
if(isAlive[i]){
fill("black")
} else {
fill("white")
}
rect(x, y, cellWidth, cellWidth);
x += cellWidth
if((i+1) % gridSize == 0){
x = 0
y += cellWidth
}
}
}
function decideWhichCellShouldBeAliveNextTurn(){
//Here we need to loop through our cells and decide whether or not
//they should be alive next turn.
for(var i = 0;i<arraySize;i++){
var numberOfLivingNeighbors = howManyNeighborsAreAlive(i)
nextTurnIsAlive[i] = isAlive[i]
//wikipedia.org/wiki/Conway%27s_Game_of_Life
//rule 1
if(isAlive[i] && numberOfLivingNeighbors < 2){
nextTurnIsAlive[i] = false
}
//rule 3
if(isAlive[i] && numberOfLivingNeighbors > 3){
nextTurnIsAlive[i] = false
}
//rules 4
if(!isAlive[i] && numberOfLivingNeighbors == 3){
nextTurnIsAlive[i] = true
}
//hint: you actually don't need to do anything for rule #2, so just worry about #3 and #4.
//HOMEWORK END
}
for(var i = 0; i<arraySize; i++){
isAlive[i] = nextTurnIsAlive[i]
}
}
//This big function is used to calculate how many neighboring cells are alive.
//The only reason it looks so complicated is because it has to account for situations
//where a cell is on the very left/right/top/bottom of the area and therefore doesn't
//have neighboring cells in that direction.
//Don't worry about understanding this function.
//Just understand that all it's doing is counting how many
//cells next to a particular cell are alive.
function howManyNeighborsAreAlive(index){
var neighborsAlive = 0
var isVeryTop = (index < gridSize)
var isVeryBottom = (index >= (gridSize*gridSize-gridSize-1))
var isVeryLeft = (index%gridSize == 0)
var isVeryRight = (index%gridSize == (gridSize-1))
if(!isVeryTop){
//count all neighbors above
var topIndex = index-gridSize
if(isAlive[topIndex]){
neighborsAlive++
}
if(!isVeryLeft){
if(isAlive[topIndex-1]){
neighborsAlive++
}
}
if(!isVeryRight){
if(isAlive[topIndex+1]){
neighborsAlive++
}
}
}
if(!isVeryBottom){
//count all neighbors above
var bottomIndex = index+gridSize
if(isAlive[bottomIndex]){
neighborsAlive++
}
if(!isVeryLeft){
if(isAlive[bottomIndex-1]){
neighborsAlive++
}
}
if(!isVeryRight){
if(isAlive[bottomIndex+1]){
neighborsAlive++
}
}
}
if(!isVeryLeft){
var leftIndex = index-1
if(isAlive[leftIndex]){
neighborsAlive++
}
}
if(!isVeryRight){
var rightIndex = index+1
if(isAlive[rightIndex]){
neighborsAlive++
}
}
return neighborsAlive
}