-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgol.go
187 lines (165 loc) · 3.59 KB
/
gol.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
package main
import (
"flag"
"fmt"
"log"
"math/rand"
"os"
"os/signal"
"strings"
"time"
)
const (
defaultRows = 30
defaultCols = 60
defaultInterval = 100
)
var (
sb strings.Builder // string builder for drawing
clearScreen = "\u001b[2J"
cellAlive = []byte("\u001b[48;5;28m \u001b[0m") // green
cellDead = []byte("\u001b[48;5;252m \u001b[0m") // white
)
func init() {
rand.Seed(time.Now().UnixNano())
}
type game struct {
rows int
cols int
interval time.Duration
prev [][]bool
current [][]bool
frozen bool // if frozen, game will stop
}
func newGame(rows, cols, interval int) *game {
prev := make([][]bool, rows)
current := make([][]bool, rows)
for x := 0; x < rows; x++ {
prev[x] = make([]bool, cols)
current[x] = make([]bool, cols)
for y := 0; y < cols; y++ {
current[x][y] = rand.Intn(2) != 0
}
}
return &game{
rows: rows,
cols: cols,
interval: time.Millisecond * time.Duration(interval),
prev: prev,
current: current,
frozen: false,
}
}
func (g *game) run() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
fmt.Print(clearScreen) // clean the whole screen
for {
select {
case <-c:
log.Println("Bye.")
return
default:
g.draw()
g.next()
if g.frozen {
log.Println("lives are frozen, game stopped!")
return
}
time.Sleep(g.interval)
}
}
}
func (g *game) draw() {
sb.Reset()
sb.WriteString(fmt.Sprintf("\u001b[%dD", g.rows*(g.cols+1)))
sb.WriteString(fmt.Sprintf("\u001b[%dA", g.rows))
for _, col := range g.current {
for _, cell := range col {
if cell { // alive
sb.Write(cellAlive)
} else {
sb.Write(cellDead) // dead
}
}
sb.WriteByte('\n')
}
fmt.Print(sb.String())
}
func (g *game) next() {
g.prev, g.current = g.current, g.prev // switch prev and current
hasChange := false
for x := 0; x < g.rows; x++ {
for y := 0; y < g.cols; y++ {
// get alive count around cell
aliveCount := 0
if x > 0 && y > 0 && g.prev[x-1][y-1] { // x-1, y-1
aliveCount++
}
if x > 0 && g.prev[x-1][y] { // x-1, y
aliveCount++
}
if x > 0 && y < g.cols-1 && g.prev[x-1][y+1] { // x-1, y+1
aliveCount++
}
if y > 0 && g.prev[x][y-1] { // x, y-1
aliveCount++
}
if y < g.cols-1 && g.prev[x][y+1] { // x, y+1
aliveCount++
}
if x < g.rows-1 && y > 0 && g.prev[x+1][y-1] { // x+1, y-1
aliveCount++
}
if x < g.rows-1 && g.prev[x+1][y] { // x+1, y
aliveCount++
}
if x < g.rows-1 && y < g.cols-1 && g.prev[x+1][y+1] { // x+1, y+1
aliveCount++
}
if g.prev[x][y] { // alive
if aliveCount != 2 && aliveCount != 3 {
g.current[x][y] = false
if !hasChange {
hasChange = true
}
} else {
g.current[x][y] = true
}
} else { // dead
if aliveCount == 3 {
g.current[x][y] = true
if !hasChange {
hasChange = true
}
} else {
g.current[x][y] = false
}
}
}
}
if !hasChange { // no change in this round, game freeze
g.frozen = true
}
}
func main() {
var rows, cols, interval int
flag.IntVar(&rows, "r", defaultRows, "rows count")
flag.IntVar(&cols, "c", defaultCols, "columns count")
flag.IntVar(&interval, "i", defaultInterval, "sleep interval between iterations (ms)")
flag.Parse()
if rows <= 0 {
log.Printf("Invalid rows %d, use default value", rows)
rows = defaultRows
}
if cols <= 0 {
log.Printf("Invalid columns %d, use default value", cols)
cols = defaultCols
}
if interval <= 0 {
log.Printf("Invalid interval %d, use default value", interval)
interval = defaultInterval
}
g := newGame(rows, cols, interval)
g.run()
}