-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboard.go
121 lines (100 loc) · 1.78 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
package main
import (
"math/rand"
"time"
)
type Point struct {
X, Y int
}
type Board struct {
Height, Width int
curr [][]int
next [][]int
}
func NewBoard(height, width int) *Board {
curr := make([][]int, height)
next := make([][]int, height)
for i := range curr {
curr[i] = make([]int, width)
next[i] = make([]int, width)
}
return &Board{
Height: height,
Width: width,
curr: curr,
next: next,
}
}
func (b *Board) ApplySeed(seed []Point) {
for i := range seed {
p := seed[i]
b.curr[p.X][p.Y] = 1
b.next[p.X][p.Y] = 1
}
}
func (b *Board) RandomeSeed(chance int) {
rand.Seed(time.Now().Unix())
for i := 0; i < b.Height; i++ {
for j := 0; j < b.Width; j++ {
v := rand.Intn(100)
if v < chance {
b.curr[i][j] = 1
b.next[i][j] = 1
}
}
}
}
func (b *Board) Neighbours(p Point) int {
count := 0
x := p.X - 1
y := p.Y - 1
for i := 0; i < 3; i++ {
xo := b.offsetForEdge(x+i, b.Height)
for j := 0; j < 3; j++ {
yo := b.offsetForEdge(y+j, b.Width)
if i == 1 && j == 1 { // Skip self
continue
}
count += b.curr[xo][yo]
}
}
return count
}
func (b *Board) offsetForEdge(i, max int) int {
if i < 0 {
i = max - 1
} else if i == max {
i = 0
}
return i
}
func (b *Board) CellState(p Point) bool {
return b.curr[p.X][p.Y] == 1
}
func (b *Board) SetCell(p Point) {
b.next[p.X][p.Y] = 1
}
func (b *Board) ClearCell(p Point) {
b.next[p.X][p.Y] = 0
}
func (b *Board) SwapLayers() {
for i := 0; i < b.Height; i++ {
for j := 0; j < b.Width; j++ {
b.curr[i][j] = b.next[i][j]
}
}
}
func (b *Board) String() string {
str := ""
for i := 0; i < b.Height; i++ {
for j := 0; j < b.Width; j++ {
if b.curr[i][j] == 1 {
str += "X"
} else {
str += "."
}
}
str += "\n"
}
return str
}