-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworld.go
234 lines (210 loc) · 4.21 KB
/
world.go
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package main
import (
"log"
"math/rand"
"sort"
)
var heatMapClr map[int][4]byte = map[int][4]byte{
0: [4]byte{0xff, 0xff, 0xff, 0xff},
5: [4]byte{0xff, 0xf3, 0x3b, 0xff},
10: [4]byte{0xfd, 0xc7, 0x0c, 0xff},
15: [4]byte{0xf3, 0x90, 0x3f, 0xff},
20: [4]byte{0xed, 0x68, 0x3c, 0xff},
25: [4]byte{0xe9, 0x3e, 0x3a, 0xff},
}
type Cell struct {
alive bool
age int
}
type World struct {
width int
height int
mtx [][]Cell
}
func (w *World) heavyWeightSpaceship(x, y int) {
// Check out of bounds
if x+6 >= w.width || y+4 >= w.height || x < 0 || y < 0 {
log.Print("heavy weight spaceship index out of range")
return
}
hws := []point{
point{x: 2, y: 0, alive: true},
point{x: 3, y: 0, alive: true},
point{x: 0, y: 1, alive: true},
point{x: 5, y: 1, alive: true},
point{x: 6, y: 2, alive: true},
point{x: 0, y: 3, alive: true},
point{x: 6, y: 3, alive: true},
point{x: 1, y: 4, alive: true},
point{x: 2, y: 4, alive: true},
point{x: 3, y: 4, alive: true},
point{x: 4, y: 4, alive: true},
point{x: 5, y: 4, alive: true},
point{x: 6, y: 4, alive: true},
}
for _, v := range hws {
w.mtx[x+v.x][y+v.y].alive = v.alive
}
}
func (w *World) randPopulate(p int) {
for i := 0; i < p; i++ {
x := rand.Intn(w.width)
y := rand.Intn(w.height)
w.mtx[x][y].alive = true
w.mtx[x][y].age = 1
}
}
func (w *World) reset() {
for x := range w.mtx {
for y := range w.mtx[x] {
w.mtx[x][y].alive = false
w.mtx[x][y].age = 0
}
}
globalTick = 0
}
func (w *World) toPixel(p []byte) []byte {
for x := 0; x < w.width; x++ {
for y := 0; y < w.height; y++ {
tmp := [4]int{}
i := 4 * x
j := 4 * y * w.width
for n := range tmp {
tmp[n] = i + j + n
}
curr := w.mtx[x][y]
// needs to sort
keys := []int{}
for k := range heatMapClr {
keys = append(keys, k)
}
sort.Ints(keys)
// find interval
interClr := 0
for _, k := range keys {
if curr.age > k {
interClr = k
continue
}
break
}
for idx, pos := range tmp {
if w.mtx[x][y].alive {
p[pos] = heatMapClr[interClr][idx]
continue
}
p[pos] = 0
}
}
}
return p
}
type point struct {
x int
y int
alive bool
}
var direction map[string]point = map[string]point{
"UP": point{
x: 0,
y: -1,
},
"UP_RIGHT": point{
x: 1,
y: -1,
},
"RIGHT": point{
x: 1,
y: 0,
},
"DOWN_RIGHT": point{
x: 1,
y: 1,
},
"DOWN": point{
x: 0,
y: 1,
},
"DOWN_LEFT": point{
x: -1,
y: 1,
},
"LEFT": point{
x: -1,
y: 0,
},
"UP_LEFT": point{
x: -1,
y: -1,
},
}
func (w *World) update(worldBorder bool) (float64, float64) {
next := []point{}
currAlive := 0
for x := 0; x < w.width; x++ {
for y := 0; y < w.height; y++ {
// Check all direction
neigh := []point{}
for _, p := range direction {
curr := point{
x: x + p.x,
y: y + p.y,
}
// A world with or without borders
switch worldBorder {
case true:
if curr.x < 0 || curr.y < 0 || curr.x >= w.width || curr.y >= w.height {
continue
}
default:
if curr.x < 0 {
curr.x = w.width - 1
}
if curr.y < 0 {
curr.y = w.height - 1
}
if curr.x >= w.width {
curr.x = 0
}
if curr.y >= w.height {
curr.y = 0
}
}
curr.alive = w.mtx[curr.x][curr.y].alive
neigh = append(neigh, curr)
}
dead := 0
alive := 0
for _, v := range neigh {
if v.alive {
alive++
continue
}
dead++
}
// Any live cell with two or three live neighbours survives.
if w.mtx[x][y].alive == true && (alive == 2 || alive == 3) {
currAlive++
w.mtx[x][y].age++
continue
}
// Any dead cell with three live neighbours becomes a live cell.
if w.mtx[x][y].alive == false && alive == 3 {
currAlive++
w.mtx[x][y].age = 1
next = append(next, point{x: x, y: y, alive: true})
continue
}
// All other live cells die in the next generation. Similarly, all other dead cells stay dead.
if w.mtx[x][y].alive == true {
w.mtx[x][y].age = 0
next = append(next, point{x: x, y: y, alive: false})
}
}
}
for _, v := range next {
w.mtx[v.x][v.y].alive = v.alive
}
currDead := w.width*w.height - currAlive
return float64(currAlive), float64(currDead)
}