-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
229 lines (204 loc) · 5 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
package main
import (
"bufio"
"fmt"
"log"
"os"
"os/exec"
"strconv"
"strings"
"time"
)
// Cell represents a cell in the game of life
// aliveNext is used as a buffer to store the next cell's status
type Cell struct {
alive bool
aliveNext bool
aliveNeighbors int
}
func main() {
// number of milliseconds between prints
var millisecondsNb int = 150
var filename string
args := len(os.Args)
fmt.Println(len(os.Args))
switch {
case args <= 1 || args > 3:
fmt.Printf("1 or 2 arguments expected.Exiting.")
os.Exit(1)
case args == 2:
filename = os.Args[1]
case args == 3:
filename = os.Args[1]
millisecondsNb, _ = strconv.Atoi(os.Args[2])
}
clear, err := exec.Command("/usr/bin/clear").Output()
if err != nil {
log.Fatal(err)
}
seed, err := seedFromFile(filename)
if err != nil {
log.Fatal(err)
}
cellsGrid := initializeCells(seed)
_, x := sizecellsGrid(cellsGrid)
for {
if aliveCells := aliveCells(cellsGrid); aliveCells < 5 {
break
}
fmt.Println(strings.Repeat("- ", x))
printCells(cellsGrid)
fmt.Println(strings.Repeat("- ", x))
computeStates(cellsGrid)
time.Sleep(time.Duration(millisecondsNb) * time.Millisecond)
os.Stdout.Write(clear)
}
}
// seedFromFile returns the initial seed as a *[][]string from a file
func seedFromFile(filename string) (*[][]string, error) {
seed := make([][]string, 0)
for i := range seed {
seed[i] = make([]string, 0)
}
file, err := os.Open(filename)
if err != nil {
return nil, fmt.Errorf("error during the opening of file : '%v'", filename)
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
seed = append(seed, strings.Split(scanner.Text(), " "))
}
if err := scanner.Err(); err != nil {
return nil, fmt.Errorf("error encountered by the Scanner")
}
return &seed, nil
}
func printCells(cellArray *[][]Cell) {
for col := range *cellArray {
for row := range (*cellArray)[col] {
if (*cellArray)[col][row].alive == true {
fmt.Printf("0 ")
} else {
fmt.Printf(" ")
}
}
fmt.Print("\n")
}
}
func initializeCells(seed *[][]string) *[][]Cell {
// retrieve size from seed
y, x := sizeStringGrid(seed)
// initialize cell grid using seed size
cellsGrid := make([][]Cell, y)
for i := range cellsGrid {
cellsGrid[i] = make([]Cell, x)
}
// Temp variables
var alive bool
var aliveNeighbors int
// loop through seed
for col := range *seed {
for row := range (*seed)[col] {
// if not '-' in seed -> Cell is alive
if (*seed)[col][row] != "-" {
alive = true
} else {
alive = false
}
// counts the number of alive neighbors around the Cell
for i := -1; i < 2; i++ {
for j := -1; j < 2; j++ {
// if not out of bounds
if (col+i >= 0 && row+j >= 0) && (col+i < y && row+j < x) {
// if its not dead and its not the Cell itself -> its an alive neighbor
if (*seed)[col+i][row+j] != "-" && !(i == 0 && j == 0) {
aliveNeighbors++
}
}
}
}
// Add Cell to the grid
cellsGrid[col][row] = Cell{alive, false, aliveNeighbors}
// reset aliveNeighbors
aliveNeighbors = 0
}
}
return &cellsGrid
}
// computeStates computes Cells Grid next state
func computeStates(grid *[][]Cell) {
y, x := sizecellsGrid(grid)
var aliveNeighbors int
// compute Cells states
for col := range *grid {
for row := range (*grid)[col] {
cell := &(*grid)[col][row]
// if Cell is alive and has neighbors < 1 => dead
// else if Cell is dead and has neighbors > 4 => born(alive)
// else it keeps its actual state
if (*cell).isAlive() && ((*cell).aliveNeighbors <= 1 || (*cell).aliveNeighbors >= 4) {
(*cell).aliveNext = false
} else if !(*cell).isAlive() && ((*cell).aliveNeighbors == 3) {
(*cell).aliveNext = true
} else {
(*cell).aliveNext = (*cell).alive
}
}
}
// compute alive neighbors
for col := range *grid {
for row := range (*grid)[col] {
// counts the number of alive neighbors around the Cell
for i := -1; i < 2; i++ {
for j := -1; j < 2; j++ {
// Boundaries
if (col+i >= 0 && row+j >= 0) && (col+i < y && row+j < x) {
// if its not dead and its not the Cell itself -> its an alive neighbor
if (*grid)[col+i][row+j].isAliveNext() && !(i == 0 && j == 0) {
aliveNeighbors++
}
}
}
}
(*grid)[col][row].aliveNeighbors = aliveNeighbors
// put aliveNext into alive for printing as the final computes Cell state
(*grid)[col][row].alive = (*grid)[col][row].aliveNext
aliveNeighbors = 0
}
}
}
// Tool functions
func sizecellsGrid(p *[][]Cell) (y int, x int) {
for _, h := range *p {
x = len(h)
break
}
y = len(*p)
return y, x
}
func sizeStringGrid(p *[][]string) (y int, x int) {
for _, h := range *p {
x = len(h)
break
}
y = len(*p)
return y, x
}
func (c Cell) isAlive() bool {
return c.alive
}
func (c Cell) isAliveNext() bool {
return c.aliveNext
}
func aliveCells(p *[][]Cell) int {
aliveCells := 0
for col := range *p {
for row := range (*p)[col] {
if (*p)[col][row].isAlive() {
aliveCells++
}
}
}
return aliveCells
}