-
Notifications
You must be signed in to change notification settings - Fork 0
/
board.go
92 lines (83 loc) · 1.62 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
package main
import (
"math/rand"
)
type Cell bool
type Board [][]Cell
func (b Board) NextGen() Board {
newBoard := make(Board, len(b))
for y := 0; y < len(b); y++ {
for x := 0; x < len(b[y]); x++ {
newBoard[y] = append(newBoard[y], b.NextGenCell(x, y))
}
}
return newBoard
}
func (b Board) TextBoard() [][]string {
newBoard := make([][]string, len(b))
for y := 0; y < len(b); y++ {
for x := 0; x < len(b[y]); x++ {
c := b[y][x]
if c {
newBoard[y] = append(newBoard[y], "x")
} else {
newBoard[y] = append(newBoard[y], " ")
}
}
}
return newBoard
}
var neighbourOffsets = [8][2]int{
{+1, +1},
{+1, 0},
{+1, -1},
{0, -1},
{-1, -1},
{-1, 0},
{-1, +1},
{0, +1},
}
func (b Board) NextGenCell(x, y int) Cell {
neighbours := 0
currentCell := b[y][x]
for _, o := range neighbourOffsets {
if Neighbour(b, x, y, o[0], o[1]) {
neighbours++
}
}
if !currentCell && neighbours == 3 {
return true
}
if currentCell && (neighbours == 2 || neighbours == 3) {
return true
}
return false
}
func IsReachableNeighbour(board Board, x, y, ox, oy int) bool {
if y+oy > len(board)-1 || y+oy < 0 {
return false
}
if x+ox > len(board[y])-1 || x+ox < 0 {
return false
}
return true
}
func Neighbour(board Board, x, y, ox, oy int) Cell {
if !IsReachableNeighbour(board, x, y, ox, oy) {
return Cell(false)
}
return board[y+oy][x+ox]
}
func RandomBoard(boardX int, boardY int) Board {
cells := []Cell{
Cell(true),
Cell(false),
}
b := make(Board, boardY)
for y := 0; y < boardY; y++ {
for x := 0; x < boardX; x++ {
b[y] = append(b[y], cells[rand.Intn(len(cells))])
}
}
return b
}