-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboard.go
155 lines (138 loc) · 2.93 KB
/
board.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
package main
import (
"bytes"
"errors"
)
type Board struct {
board [][]Cell
w, h int
}
func NewBoard(w, h int) Board {
board := make([][]Cell, h)
for i := range board {
board[i] = make([]Cell, w)
}
return Board{w: w, h: h, board: board}
}
// return whether a cell is currently alive
func (b *Board) Alive(x, y int) bool {
// protect from out of bounds errors
if y >= len(b.board) || x >= len(b.board[y]) {
return false
}
return b.board[y][x].alive
}
// return whether a cell will be alive on the next iteration
// w & h are the width and height of the new board
func (b *Board) NextCell(cell *Cell, x, y, w, h int) error {
neighbors := b.Neighbors(x, y, w, h)
count := len(neighbors)
// currently alive cell
c, err := b.CellAt(x, y)
if err != nil {
return err
}
*cell = *c
if c.alive {
cell.alive = count >= 2 && count <= 3
} else {
// reproduce
if count == 3 || count == 6 {
cell.alive = true
cell.SetNextShape(neighbors)
}
}
return nil
}
func (b *Board) CellAt(x, y int) (*Cell, error) {
// protect from out of bounds errors
if y >= len(b.board) || x >= len(b.board[y]) {
var c Cell
return &c, errors.New("out of bounds")
}
return &b.board[y][x], nil
}
// returns the number living neighbors a cell has
// w & h are the width and height of the NEXT board
func (b *Board) Neighbors(x, y, w, h int) []*Cell {
neighbors := make([]*Cell, 0, 8)
lpos := saneModInt((x - 1), w) // cell to the left
rpos := saneModInt((x + 1), w) // cell to the right
apos := saneModInt((y - 1), h) // cell above
bpos := saneModInt((y + 1), h) // cell below
// above left
if b.Alive(lpos, apos) {
c, err := b.CellAt(lpos, apos)
if err == nil {
neighbors = append(neighbors, c)
}
}
// above
if b.Alive(x, apos) {
c, err := b.CellAt(x, apos)
if err == nil {
neighbors = append(neighbors, c)
}
}
// above right
if b.Alive(rpos, apos) {
c, err := b.CellAt(rpos, apos)
if err == nil {
neighbors = append(neighbors, c)
}
}
// left
if b.Alive(lpos, y) {
c, err := b.CellAt(lpos, y)
if err == nil {
neighbors = append(neighbors, c)
}
}
// right
if b.Alive(rpos, y) {
c, err := b.CellAt(rpos, y)
if err == nil {
neighbors = append(neighbors, c)
}
}
// below left
if b.Alive(lpos, bpos) {
c, err := b.CellAt(lpos, bpos)
if err == nil {
neighbors = append(neighbors, c)
}
}
// below
if b.Alive(x, bpos) {
c, err := b.CellAt(x, bpos)
if err == nil {
neighbors = append(neighbors, c)
}
}
// below right
if b.Alive(rpos, bpos) {
c, err := b.CellAt(rpos, bpos)
if err == nil {
neighbors = append(neighbors, c)
}
}
return neighbors
}
func (b *Board) Random() {
for y := range b.board {
for x := range b.board[y] {
b.board[y][x].Random()
}
}
}
func (b *Board) String() string {
var buf bytes.Buffer
for y := range b.board {
for x := range b.board[y] {
c, _ := b.CellAt(x, y)
buf.WriteString(c.String())
}
buf.WriteByte('\n')
}
return buf.String()
}