-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathcell.go
160 lines (132 loc) · 3.15 KB
/
cell.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
package main
import (
"log"
"math/rand"
"github.com/go-gl/gl/v4.1-core/gl"
)
var (
squarePoints = []float32{
// Bottom left right-angle triangle
-1, 1, 0,
1, -1, 0,
-1, -1, 0,
// Top right right-angle triangle
-1, 1, 0,
1, 1, 0,
1, -1, 0,
}
squarePointCount = int32(len(squarePoints) / 3)
)
type cell struct {
drawable uint32
x int
y int
alive bool
nextState bool
}
// checkState determines the state of the cell for the next tick of the game.
func (c *cell) checkState(cells [][]*cell) {
c.alive = c.nextState
c.nextState = c.alive
liveCount := c.liveNeighbors(cells)
if c.alive {
// 1. Any live cell with fewer than two live neighbours dies, as if caused by underpopulation.
if liveCount < 2 {
c.nextState = false
}
// 2. Any live cell with two or three live neighbours lives on to the next generation.
if liveCount == 2 || liveCount == 3 {
c.nextState = true
}
// 3. Any live cell with more than three live neighbours dies, as if by overpopulation.
if liveCount > 3 {
c.nextState = false
}
} else {
// 4. Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
if liveCount == 3 {
c.nextState = true
}
}
}
// liveNeighbors returns the number of live neighbors for a cell.
func (c *cell) liveNeighbors(cells [][]*cell) int {
var liveCount int
add := func(x, y int) {
// If we're at an edge, check the other side of the board.
if x == len(cells) {
x = 0
} else if x == -1 {
x = len(cells) - 1
}
if y == len(cells[x]) {
y = 0
} else if y == -1 {
y = len(cells[x]) - 1
}
if cells[x][y].alive {
liveCount++
}
}
add(c.x-1, c.y) // To the left
add(c.x+1, c.y) // To the right
add(c.x, c.y+1) // up
add(c.x, c.y-1) // down
add(c.x-1, c.y+1) // top-left
add(c.x+1, c.y+1) // top-right
add(c.x-1, c.y-1) // bottom-left
add(c.x+1, c.y-1) // bottom-right
return liveCount
}
// draw draws the cell if it is alive.
func (c *cell) draw() {
if !c.alive {
return
}
gl.BindVertexArray(c.drawable)
gl.DrawArrays(gl.TRIANGLES, 0, squarePointCount)
}
// newCell initializes and returns a cell with the given x/y coordinates.
func newCell(x, y int) *cell {
points := make([]float32, len(squarePoints), len(squarePoints))
copy(points, squarePoints)
for i := 0; i < len(points); i++ {
var factor float32
var size float32
switch i % 3 {
case 0:
size = 1.0 / float32(columns)
factor = float32(x) * size
case 1:
size = 1.0 / float32(rows)
factor = float32(y) * size
default:
continue
}
if points[i] < 0 {
points[i] = (factor * 2) - 1
} else {
points[i] = ((factor + size) * 2) - 1
}
}
return &cell{
drawable: makeVao(points),
x: x,
y: y,
}
}
// makeCells creates the cell matrix and sets the initial state of the game.
func makeCells(seed int64, threshold float64) [][]*cell {
log.Printf("Using seed=%v, threshold=%v", seed, threshold)
rand.Seed(seed)
cells := make([][]*cell, rows, rows)
for x := 0; x < rows; x++ {
for y := 0; y < columns; y++ {
c := newCell(x, y)
c.alive = rand.Float64() < threshold
c.nextState = c.alive
cells[x] = append(cells[x], c)
}
}
return cells
}