-
Notifications
You must be signed in to change notification settings - Fork 0
/
game_of_life.go
158 lines (141 loc) · 3.08 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
151
152
153
154
155
156
157
158
package main
import (
"bufio"
"encoding/json"
"fmt"
"github.com/buger/goterm"
"io/ioutil"
"log"
"math/rand"
"os"
"time"
)
const ConfigFile = "config.json"
type Config struct {
Rows int `json:"rows"`
Columns int `json:"columns"`
Separator string `json:"separator"`
Alive string `json:"alive"`
Dead string `json:"dead"`
Seed int64 `json:"seed,omitempty"`
Interval int64 `json:"interval"`
}
var config Config
var board [][]int
func printBoard() {
for i := 0; i < len(board); i++ {
for j := 0; j < len(board[i]); j++ {
var symbol string
if board[i][j] == 1 {
symbol = config.Alive
} else {
symbol = config.Dead
}
goterm.Printf("%s%s", symbol, config.Separator)
}
goterm.Println("")
}
}
func outOfBoundsOrValue(i, j int) int {
if i < 1 || i > config.Rows-1 || j < 1 || j > config.Columns-1 {
return 0
}
return board[i][j]
}
func neighborCount(i, j int) int {
var x00, x01, x02,
x10, x12,
x20, x21, x22 int
x00 = outOfBoundsOrValue(i-1, j-1)
x01 = outOfBoundsOrValue(i-1, j)
x02 = outOfBoundsOrValue(i-1, j+1)
x10 = outOfBoundsOrValue(i, j-1)
x12 = outOfBoundsOrValue(i, j+1)
x20 = outOfBoundsOrValue(i+1, j-1)
x21 = outOfBoundsOrValue(i+1, j)
x22 = outOfBoundsOrValue(i+1, j+1)
return x00 + x01 + x02 + x10 + x12 + x20 + x21 + x22
}
func aliveCase(count int) int {
if count < 2 || count > 3 {
return 0
}
return 1
}
func deadCase(count int) int {
if count == 3 {
return 1
}
return 0
}
func nextFieldValue(i, j int) int {
var nextValue int
isAlive := board[i][j] == 1
count := neighborCount(i, j)
if isAlive {
nextValue = aliveCase(count)
} else {
nextValue = deadCase(count)
}
return nextValue
}
func computeNextBoard() [][]int {
newBoard := make([][]int, config.Rows)
for i := 0; i < len(board); i++ {
newBoard[i] = make([]int, config.Columns)
for j := 0; j < len(board[i]); j++ {
newBoard[i][j] = nextFieldValue(i, j)
}
}
return newBoard
}
func initBoard() {
rand.Seed(config.Seed)
fmt.Printf("Generating board with seed: %d\n", config.Seed)
board = make([][]int, config.Rows)
for i := 0; i < len(board); i++ {
board[i] = make([]int, config.Columns)
for j := 0; j < len(board[i]); j++ {
board[i][j] = rand.Intn(2)
fmt.Printf("%d%s", board[i][j], config.Separator)
}
fmt.Println("")
}
}
func initConfig() {
rawConfig, err := ioutil.ReadFile(ConfigFile)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Using %s\n%s", ConfigFile, rawConfig)
err = json.Unmarshal(rawConfig, &config)
if err != nil {
log.Fatal(err)
}
if config.Seed == 0 {
fmt.Println("Generating random seed")
config.Seed = time.Now().UnixNano()
}
}
func init() {
fmt.Println("Welcome to the game of life")
initConfig()
initBoard()
}
func main() {
fmt.Println("Press enter to begin")
reader := bufio.NewReader(os.Stdin)
reader.ReadString('\n')
goterm.Flush()
goterm.Clear()
iteration := 0
for {
iteration++
goterm.MoveCursor(1, 1)
goterm.Printf("Iteration: %d\n", iteration)
printBoard()
goterm.Flush()
board = computeNextBoard()
time.Sleep(time.Duration(config.Interval) * time.Millisecond)
}
}