-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
201 lines (174 loc) · 3.65 KB
/
main.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
/* Classic Conway's Game of Life:
*
* To calculate each generational step:
* 1. While iterating through living cells:
* - Add +1 to n on each adjacent cell.
* - Add living cell and adjacent cell to
* set of known cells.
* 2. Iterate though set of known cells while
* applying Conway's set of rules.
*
* Rules:
* 1. Any live cell with fewer than 2 live
* neighbours die.
* 2. Any live cell with 2 or 3 live
* neighbours lives on.
* 3. Any cell with more then 3 live
* neighbours die.
* 4. Dead cells with 3 live neighbours
* becomes a live cell.
*/
package main
import (
"math"
"github.com/faiface/pixel"
"github.com/faiface/pixel/imdraw"
"github.com/faiface/pixel/pixelgl"
"github.com/fatih/set"
"golang.org/x/image/colornames"
)
const (
size float64 = 16
maxX float64 = 1024
maxY float64 = 768
)
type delta struct {
dx float64
dy float64
}
var transformations = []*delta{
&delta{dx: 0, dy: 1},
&delta{dx: 0, dy: -1},
&delta{dx: 1, dy: 0},
&delta{dx: -1, dy: 0},
&delta{dx: 1, dy: 1},
&delta{dx: -1, dy: -1},
&delta{dx: -1, dy: 1},
&delta{dx: 1, dy: -1},
}
// Tile is a position on the map
type Tile struct {
x float64
y float64
n int
alive bool
}
func createTile(x float64, y float64) *Tile {
return &Tile{
x: x,
y: y,
n: 0,
alive: false,
}
}
func (t *Tile) around() <-chan *Tile {
ch := make(chan *Tile, 8)
go func() {
defer close(ch)
for _, d := range transformations {
nx, ny := t.x-(d.dx*size), t.y-(d.dy*size)
if insideWindow(nx, ny) {
ch <- createTile(nx, ny)
}
}
}()
return ch
}
func drawTile(imd *imdraw.IMDraw, x float64, y float64) {
imd.Color = pixel.RGB(0.5, 0.5, 1)
imd.Push(pixel.V(x, y), pixel.V(x+size, y+size))
imd.Rectangle(0)
}
func round(i float64) float64 {
return math.Floor(i/size) * size
}
func insideWindow(x float64, y float64) bool {
return (((0.0 <= x) && (x < maxX)) && ((0.0 <= y) && (y < maxY)))
}
type key struct {
x float64
y float64
}
func step(cells set.Interface) {
// Calculate crowding.
// I'm tempted to make a data structure
// for this tile set.
tileMap := make(map[key]*Tile)
for cells.Size() > 0 {
e := cells.Pop()
tile := e.(*Tile)
k := key{x: tile.x, y: tile.y}
if t, b := tileMap[k]; b {
t.alive = true
} else {
tile.alive = true
tileMap[k] = tile
}
for t := range tile.around() {
k := key{x: t.x, y: t.y}
if _tile, b := tileMap[k]; b {
_tile.n++
} else {
t.n++
tileMap[k] = t
}
}
}
// Calculate survivors
for _, v := range tileMap {
if (2 <= v.n) && (v.n < 4) && (v.alive) {
v.n = 0
cells.Add(v)
} else if (v.n == 3) && (!v.alive) {
v.n = 0
cells.Add(v)
}
}
}
func run() {
// 64x48 tiles of 16x16 pixels
cfg := pixelgl.WindowConfig{
Title: "Gome-of-Life",
Bounds: pixel.R(0, 0, maxX, maxY),
VSync: true,
}
win, err := pixelgl.NewWindow(cfg)
if err != nil {
panic(err)
}
imd := imdraw.New(nil)
cells := set.New(set.NonThreadSafe)
for !win.Closed() {
win.UpdateInputWait(-1)
if win.JustPressed(pixelgl.MouseButtonLeft) {
pos := win.MousePosition()
x := round(pos.X)
y := round(pos.Y)
tile := createTile(x, y)
tile.alive = true
// Avoid adding tiles on top
// of each other.
// This is currently broken, I'll
// need to write a Tile specific set.
if !cells.Has(tile) {
cells.Add(tile)
}
}
// Single game step
if win.JustPressed(pixelgl.KeyEnter) {
imd.Clear()
step(cells)
}
// Draw calls
for _, e := range cells.List() {
tile := e.(*Tile)
drawTile(imd, tile.x, tile.y)
}
win.Clear(colornames.Aliceblue)
imd.Draw(win)
win.Update()
}
}
func main() {
pixelgl.Run(run)
}