-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
241 lines (190 loc) · 4.27 KB
/
main.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package main
import (
"flag"
"fmt"
"os"
"os/exec"
"time"
"github.com/wsxiaoys/terminal/color"
)
type Game struct {
Generation int
Rows []Row
SleepTime int
}
func (g *Game) Get(col int, row int) *Cell {
return &g.Rows[row].Cells[col]
}
type Row struct {
Cells []Cell
}
type Cell struct {
Col int
Row int
Value int
NextValue int
Color string
}
// offset values tht are used when finding neighbours
var offsets [8][]int
func init() {
// offsets define the x (column), and y (row) offset from origin (0, 0)
offsets[0] = []int{-1, -1}
offsets[1] = []int{0, -1}
offsets[2] = []int{1, -1}
offsets[3] = []int{-1, 0}
offsets[4] = []int{1, 0}
offsets[5] = []int{-1, 1}
offsets[6] = []int{0, 1}
offsets[7] = []int{1, 1}
}
// Draw all Cell structures
func (game *Game) Draw() {
clearScreen()
fmt.Printf("Generation : %v\n", game.Generation)
for _, r := range game.Rows {
for _, c := range r.Cells {
if c.Value == 1 {
color.Print(fmt.Sprintf("%v ", c.Color))
} else {
color.Print("@c.")
}
}
fmt.Printf("\n")
}
game.Sleep()
}
// Return the number of live neighbours a Cell has
func (g *Game) NeighbourCount(c *Cell) int {
alive := 0
for _, offset := range offsets {
col := c.Col + offset[0]
row := c.Row + offset[1]
if col >= 0 && col < len(g.Rows[0].Cells) {
if row >= 0 && row < len(g.Rows) {
neighbour := g.Rows[row].Cells[col]
if neighbour.IsAlive() {
alive += 1
}
}
}
}
return alive
}
func (g *Game) Sleep() {
time.Sleep(time.Millisecond * time.Duration(g.SleepTime))
}
// Returns true if the Cell is currently alive, false otherwise
func (c *Cell) IsAlive() bool {
return c.Value == 1
}
// Kill the Cell in the next generation
func (c *Cell) Die() {
c.NextValue = 0
}
// Keep a Cell alive in the next generation
func (c *Cell) Live() {
c.NextValue = 1
// Use a green block
c.Color = "@{G}"
}
// Bring the Cell back to life in the next generation
func (c *Cell) Spawn() {
c.NextValue = 1
// Use a white block
c.Color = "@{W}"
}
// Copy next values of the Cell's to the current values.
func (g *Game) PrepareValues() {
for row := 0; row < len(g.Rows); row++ {
for col := 0; col < len(g.Rows[0].Cells); col++ {
c := g.Get(col, row)
c.Value = c.NextValue
}
}
}
func NewGame(filename string, sleepTime int) *Game {
// load the game data
data, err := ReadLifeData(filename)
if err != nil {
fmt.Printf("%v\n", err)
os.Exit(2)
}
rowCount := len(data)
// colCount is the maximum string length of a row of data
maxLength := 0
for _, line := range data {
if len(line) > maxLength {
maxLength = len(line)
}
}
colCount := maxLength
rows := make([]Row, rowCount)
for row := 0; row < len(rows); row++ {
rows[row].Cells = make([]Cell, colCount)
for col := 0; col < colCount; col++ {
c := *NewCell(col, row)
if row < len(data) && col < len(data[row]) {
if string(data[row][col]) == "*" {
// this is a live cell
c.Live()
c.Value = 1
}
}
rows[row].Cells[col] = c
}
}
game := &Game{Rows: rows, SleepTime: sleepTime}
return game
}
// Create a new Cell
func NewCell(col int, row int) *Cell {
return &Cell{Value: 0, Col: col, Row: row}
}
// Update all Cells
func (game *Game) Tick() {
game.Generation += 1
for row := 0; row < len(game.Rows); row++ {
for col := 0; col < len(game.Rows[0].Cells); col++ {
c := &game.Rows[row].Cells[col]
n := game.NeighbourCount(c)
if c.IsAlive() {
if n < 2 {
// live cell with less than 2 live neighbour
c.Die()
} else if n == 2 || n == 3 {
// live cell with 2 or 3 live neighbours
c.Live()
} else if n > 3 {
// live cell with 3+ live neighbours
c.Die()
}
} else if !c.IsAlive() && n == 3 {
// dead cell with 3 neighbours
c.Spawn()
}
}
}
game.PrepareValues()
}
// Clear the terminal
func clearScreen() {
c := exec.Command("clear")
c.Stdout = os.Stdout
c.Run()
}
func main() {
iterations := flag.Int("iterations", 100, "Iterations")
sleepTime := flag.Int("sleep", 100, "Pause between frames (ms)")
filename := flag.String("file", "", "Name of life data file")
flag.Parse()
if *filename == "" {
flag.Usage()
os.Exit(1)
}
game := NewGame(*filename, *sleepTime)
for i := 0; i < *iterations; i++ {
game.Draw()
game.Tick()
}
}