-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgame_of_life.go
150 lines (135 loc) · 2.43 KB
/
game_of_life.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
package main
import "fmt"
import "log"
import "bufio"
import "os"
import "strings"
var DEBUG = false
type Grid struct {
grid [][]bool
N int
B int
S1 int
S2 int
}
const ALIVE = '*'
const DEAD = '.'
func (g *Grid) Init(n, b, s1, s2 int) {
g.N = n
g.B = b
g.S1 = s1
g.S2 = s2
g.grid = initGrid(n)
}
func initGrid(n int) [][]bool {
grid := make([][]bool, n)
for i := range grid {
grid[i] = make([]bool, n)
}
return grid
}
func (g *Grid) String() string {
out := make([]string, 0, g.N*(g.N+1))
for _, line := range g.grid {
for _, cell := range line {
out = append(out, g.CellToString(cell))
}
out = append(out, "\n")
}
return strings.Join(out, "")
}
func (g *Grid) CellToString(cell bool) string {
if cell {
return string(ALIVE)
} else {
return string(DEAD)
}
}
func (g *Grid) AddLine(line string, currentLine int) {
for ix, cell := range line {
g.grid[currentLine][ix] = (cell == ALIVE)
}
}
func (g *Grid) CountAliveNeighbours(line, col int) (n int) {
if line > 0 {
if col > 0 {
if g.grid[line-1][col-1] {
n += 1
}
}
if g.grid[line-1][col] {
n += 1
}
if col < g.N-1 {
if g.grid[line-1][col+1] {
n += 1
}
}
}
if col > 0 && g.grid[line][col-1] {
n += 1
}
if col < g.N-1 && g.grid[line][col+1] {
n += 1
}
if line < g.N-1 {
if col > 0 && g.grid[line+1][col-1] {
n += 1
}
if g.grid[line+1][col] {
n += 1
}
if col < g.N-1 && g.grid[line+1][col+1] {
n += 1
}
}
return
}
func (g *Grid) Step() {
nextGen := initGrid(g.N)
for pLine, line := range g.grid {
for pCol, cell := range line {
nAlive := g.CountAliveNeighbours(pLine, pCol)
if cell { // Alive cell
if nAlive == g.S1 || nAlive == g.S2 {
nextGen[pLine][pCol] = true
}
} else {
if nAlive == g.B { // Should be born ?
nextGen[pLine][pCol] = true
}
}
}
}
g.grid = nextGen
}
func (g *Grid) StepN(n int) {
count := 0
for count < n {
count += 1
g.Step()
}
}
func main() {
file, err := os.Open(os.Args[1])
if err != nil {
log.Fatal(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
var grid = new(Grid)
currentLine := 0
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if grid.N == 0 {
// Implement B3S23 game-of-life rules
// Birth if 3 alive neighbours
// Survive if 2 or 3 alive neigbours
grid.Init(len(line), 3, 2, 3)
}
grid.AddLine(line, currentLine)
currentLine += 1
}
grid.StepN(10)
fmt.Println(grid)
}