-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgame-of-life.go
229 lines (193 loc) · 5.33 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
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 (
"fmt"
"log"
"math/rand"
"os"
"os/exec"
"runtime"
"time"
"gopkg.in/alecthomas/kingpin.v2"
)
var (
inputX = kingpin.Flag("xsize", "The width of the grid").Short('x').Default("80").Int()
inputY = kingpin.Flag("ysize", "The height of the grid").Short('y').Default("40").Int()
inputI = kingpin.Flag("iterations", "Number of iterations. Any negative number will use the default, infinity").Short('i').Default("-1").Int()
inputF = kingpin.Flag("fps", "Frames per second, how log to wait until the next iteration is displayed").Short('f').Default("10").Int()
inputP = kingpin.Flag("percentage", "Percentage of living cells at the start").Short('p').Default("33").Int()
)
/*
Coordinates start top left
x ->
y
|
V
*/
func main() {
kingpin.Version("1.0.0")
kingpin.Parse()
g := NewGameBoard(*inputX, *inputY)
g.RandInit(*inputP)
sleepTime := time.Duration(1000 / *inputF) * time.Millisecond
//Main game loop
i := *inputI
for {
if i == 0 {
break
}
i--
cmd := exec.Command("clear")
if runtime.GOOS == "windows" {
cmd = exec.Command("cmd", "/c", "cls")
}
cmd.Stdout = os.Stdout
cmd.Run()
g.Print()
fmt.Printf("Generation: %d\n", -i)
g.Iterate()
time.Sleep(sleepTime)
}
}
//GameBoard holds the grid on which the cells are placed as well as a few of it's attributes.
//It provides the necessary methods to run the initialize and start the game
type GameBoard struct {
generation int
xSize, ySize int
cells []bool
}
//NewGameBoard initializes a new game (constructor) with an all-dead grid
func NewGameBoard(x, y int) *GameBoard {
cells := make([]bool, x*y)
return &GameBoard{generation: 0, xSize: x, ySize: y, cells: cells}
}
//RandInit sets a given percentage of the cells in the grid to "alive".
//The grid is then randomized, leaving a random grid with a defined amount of
//living cells.
func (gb *GameBoard) RandInit(percentage int) {
//Calculate number of living cells
numAlive := percentage * len(gb.cells) / 100
//Insert living cells at the beginning
for i := 0; i < numAlive; i++ {
gb.cells[i] = true
}
//Randomize slice
vals := gb.cells
r := rand.New(rand.NewSource(time.Now().Unix()))
for n := len(vals); n > 0; n-- {
randIndex := r.Intn(n)
vals[n-1], vals[randIndex] = vals[randIndex], vals[n-1]
}
gb.cells = vals
}
//Iterate implements the rules for Conway's Game of Life. It takes the current
//grid, applies the 4 rules and sets the grid to it's new state.
func (gb *GameBoard) Iterate() {
gbOld := NewGameBoard(gb.xSize, gb.ySize)
copy(gbOld.cells, gb.cells)
for y := 0; y < gb.ySize; y++ { //Rows
for x := 0; x < gb.xSize; x++ { //Collumns
//Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.
if !gbOld.Get(x, y) && gbOld.Neighbours(x, y) == 3 {
gb.Set(x, y, true)
continue
}
//Any live cell with fewer than two live neighbors dies, as if by underpopulation.
if gbOld.Get(x, y) && gbOld.Neighbours(x, y) < 2 {
gb.Set(x, y, false)
continue
}
//Any live cell with two or three live neighbors lives on to the next generation.
if gbOld.Get(x, y) && ((gbOld.Neighbours(x, y) == 2) || (gbOld.Neighbours(x, y) == 3)) {
//No need to set, already alive
continue
}
//Any live cell with more than three live neighbors dies, as if by overpopulation.
if gbOld.Get(x, y) && (gbOld.Neighbours(x, y) > 3) {
gb.Set(x, y, false)
continue
}
}
}
}
//Equal is a helper function to compare a GameBoard to another. It returns true
//if size, dimensions and the cell's states are identical
func (gb *GameBoard) Equal(gb2 *GameBoard) bool {
if gb.xSize != gb2.xSize || gb.ySize != gb2.ySize {
return false
}
if len(gb.cells) != len(gb2.cells) {
return false
}
for k := range gb.cells {
if gb.cells[k] != gb2.cells[k] {
return false
}
}
return true
}
//Set sets a cell defined by it's x and y coordinates to a given state (alive:
//true, dead: false)
func (gb *GameBoard) Set(x, y int, val bool) {
if !gb.InBounds(x, y) {
log.Fatal("Invalid Coordinate")
}
gb.cells[y*(gb.xSize)+x] = val
}
//Get retrieves a cell's state by it's x and y coordinates (alive: true, dead:
//false)
func (gb *GameBoard) Get(x, y int) bool {
if !gb.InBounds(x, y) {
log.Fatal("Invalid Coordinate")
}
return gb.cells[y*(gb.xSize)+x]
}
//Neighbours returns the number of alive neighbours of a given cell
func (gb *GameBoard) Neighbours(x, y int) int {
count := 0
arr := []int{-1, 0, 1}
for _, v1 := range arr {
for _, v2 := range arr {
if gb.InBounds(x+v1, y+v2) {
if gb.Get(x+v1, y+v2) && !(v1 == 0 && v2 == 0) {
count++
}
}
}
}
return count
}
//InBounds is a helper function to check if a coordinate tupel is inside the grid.
func (gb *GameBoard) InBounds(x int, y int) bool {
return (x >= 0 &&
x < gb.xSize &&
y >= 0 &&
y < gb.ySize)
}
//Print displays a ASCII representation of the grid to stout
func (gb *GameBoard) Print() {
//Top margin
fmt.Print("╔")
for x := 1; x <= gb.xSize; x++ {
fmt.Print("══")
}
fmt.Println("╗")
//Rows
for y := 0; y < gb.ySize; y++ {
fmt.Print("║")
//Collumns
for x := 0; x < gb.xSize; x++ {
if gb.Get(x, y) {
fmt.Print("██")
} else {
fmt.Print(" ")
}
}
fmt.Println("║")
}
//Bottom margin
fmt.Print("╚")
for x := 1; x <= gb.xSize; x++ {
fmt.Print("══")
}
fmt.Println("╝")
}