-
Notifications
You must be signed in to change notification settings - Fork 0
/
conways.go
134 lines (119 loc) · 2.88 KB
/
conways.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
package main
import (
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"
)
type Environment struct {
cells [][]int
cellsNext [][]int
rows int
cols int
}
func main() {
if len(os.Args) != 2 {
fmt.Fprintf(os.Stderr, "Not enough command line arguments\n")
os.Exit(1)
}
content, err := ioutil.ReadFile(os.Args[1])
if err != nil {
fmt.Fprintf(os.Stderr, "Problem with file: %v\n", err)
os.Exit(1)
}
environment := initializeEnvironment(string(content))
//environment.printCells()
environment.runRules()
environment.copyNextGeneration()
environment.printCells()
}
func initializeEnvironment(content string) Environment {
lines := strings.Split(content, "\n")
s := strings.Split(lines[0], " ")
if len(s) != 2 {
fmt.Fprintf(os.Stderr, "Problem with size format values")
os.Exit(1)
}
rows, _ := strconv.Atoi(s[0])
cols, _ := strconv.Atoi(s[1])
cells := make([][]int, rows)
cellsNext := make([][]int, rows)
for i, line := range lines[1 : len(lines)-1] {
cells[i] = make([]int, cols)
cellsNext[i] = make([]int, cols)
for j := 0; j < cols; j++ {
char := line[j]
if char == '.' {
cells[i][j] = 0
cellsNext[i][j] = 0
}
if char == '*' {
cells[i][j] = 1
cellsNext[i][j] = 1
}
}
}
environment := Environment{cells, cellsNext, rows, cols}
return environment
}
//Conways 4 rules of life
//1. Any live cell with fewer than two live neighbours dies, as if caused by underpopulation.
//2. Any live cell with more than three live neighbours dies, as if by overcrowding.
//3. Any live cell with two or three live neighbours lives on to the next generation.
//4. Any dead cell with exactly three live neighbours becomes a live cell.
func (e *Environment) runRules() {
for x := 0; x < e.rows; x++ {
for y := 0; y < e.cols; y++ {
liveCells := e.surroundingLiveCells(x, y)
//fmt.Println("x:", x, "y:", y, "LiveCells:", liveCells)
if e.isAlive(x, y) {
if liveCells < 2 {
e.cellsNext[x][y] = 0
}
if liveCells > 3 {
e.cellsNext[x][y] = 0
}
} else {
if liveCells == 3 {
e.cellsNext[x][y] = 1
}
}
}
}
}
func (e *Environment) surroundingLiveCells(x int, y int) int {
liveCells := 0
for i := x - 1; i <= x+1; i++ {
for j := y - 1; j <= y+1; j++ {
if e.inEnvironment(i, j) && e.isAlive(i, j) {
liveCells++
}
}
}
return liveCells
}
func (e *Environment) printCells() {
for x := 0; x < e.rows; x++ {
for y := 0; y < e.cols; y++ {
fmt.Print(e.cells[x][y])
}
fmt.Println()
}
}
func (e *Environment) copyNextGeneration() {
for x := 0; x < e.rows; x++ {
for y := 0; y < e.cols; y++ {
e.cells[x][y] = e.cellsNext[x][y]
}
}
}
func (e *Environment) inEnvironment(x int, y int) bool {
return x > 0 && y > 0 && x < e.rows && y < e.cols
}
func (e *Environment) isAlive(x int, y int) bool {
return e.cells[x][y] == 1
}
func (e *Environment) isDead(x int, y int) bool {
return e.cells[x][y] == 0
}