-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.txt
116 lines (103 loc) · 2.81 KB
/
test.txt
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
prepare rows as 64
prepare cols as 64
prepare dbg as true
brush Cell has { x, y, live }
sketch seed {
prepare cells as []
loop x through (0, rows) {
loop y through (0, cols) {
prepare live as false
prepare chance as random(0, 100)
if (chance < 10) {
prepare live as true
}
cells.add(prep Cell(x: x, y: y, live: live))
}
}
finished cells
}
prepare cells as seed()
sketch getNeighbors needs (cells, index) {
~ Get neighbors around a cell
prepare neighbors as []
~ Populate neighbors
if (index - rows - 1 > 0) {
neighbors.add(cells[index - rows - 1])
}
if (index - rows > 0) {
neighbors.add(cells[index - rows])
}
if (index - rows + 1 > 0) {
neighbors.add(cells[index - rows + 1])
}
if (index > 0) {
neighbors.add(cells[index - 1])
}
if (index < cells.length - 1) {
neighbors.add(cells[index + 1])
}
if (index + rows - 1 < cells.length - 1) {
neighbors.add(cells[index + rows - 1])
}
if (index + rows < cells.length - 1) {
neighbors.add(cells[index + rows])
}
if (index + rows + 1 < cells.length - 1) {
neighbors.add(cells[index + rows + 1])
}
prepare alive as []
loop i through (0, neighbors.length) {
if (neighbors[i].live) {
alive.add(neighbors[i])
}
}
finished alive
}
if (dbg) {
ink(getNeighbors(cells, rows * cols / 2))
}
sketch painting {
~ This loop runs every iteration and must be in every program
loop i through (0, cells.length) {
prepare cell as cells[i]
prepare neighbors as getNeighbors(cells, i)
if (cell.live) {
if (neighbors.length < 2 || neighbors.length > 3) {
~ Any live cell with fewer than two neighbors dies, as if by underpopulation
~ Any live cell with more than three live neighbors dies, as if by overpopulation
prepare cell.live as false
} elif (!cell.live && neighbors.length == 3) {
~ Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction
prepare cell.live as true
}
} else {
if (neighbors.length == 3) {
~ Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction
prepare cell.live as true
}
}
if (cell.live) {
~ Now draw the cell if it's alive!
prepare color as prep Color(r: 255, g: 0, b: 0)
Canvas.fill(cell.x, cell.y, color)
} else {
~ If it's dead, turn the cell off
Canvas.erase(cell.x, cell.y)
}
}
}
if (dbg) {
sketch includes needs (array, value) {
loop i through (0, array.length) {
if (array[i] == value) {
finished true
}
}
finished false
}
prepare test as ["hello", "world", "fox"]
ink(test)
if (!(false && includes(test, "foxes"))) {
ink("NOT false AND includes foxes in test evaluates to true")
}
}