-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
95 lines (81 loc) · 1.67 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
// Conway's Game of Life Imprementation
package main
import (
"bytes"
"fmt"
"math/rand"
"time"
)
type World struct {
s [][]bool
w, h int
}
func NewWorld(w, h int) (World) {
s := make([][]bool, h)
for i := range s {
s[i] = make([]bool, w)
}
return World{s: s, w: w, h: h}
}
func (world *World) InitWorld() {
rand.Seed(time.Now().UTC().UnixNano())
for h:= 0; h < world.h; h++ {
for w:= 0; w < world.w; w++ {
if rand.Int31n(2) == 0 {
world.s[h][w] = false
} else {
world.s[h][w] = true
}
}
}
}
func (world *World) Tick() {
evolution := NewWorld(world.w, world.h)
for h := 0; h < world.h; h++ {
for w := 0; w < world.w; w++ {
evolution.s[h][w] = world.CellNextState(h, w)
}
}
*world = evolution
}
func (world *World) CellNextState(h, w int) (bool) {
alive := 0
for i := -1; i <= 1; i++ {
for j := -1; j <= 1; j++ {
if (i != 0 || j !=0) && world.CellAlive(h+i, w+j) {
alive++
}
}
}
return alive == 3 || alive == 2 && world.s[h][w]
}
func (world *World) CellAlive(h, w int) bool {
if (h < 0 || h >= world.h || w < 0 || w >= world.w) {
return false
} else {
return world.s[h][w]
}
}
func (world *World) String() string {
var buf bytes.Buffer
for h := 0; h < world.h; h++ {
for w := 0; w < world.w; w++ {
b := byte(' ')
if world.CellAlive(h, w) {
b = '*'
}
buf.WriteByte(b)
}
buf.WriteByte('\n')
}
return buf.String()
}
func main() {
world := NewWorld(40, 15)
world.InitWorld()
for i := 0; i < 300; i++ {
world.Tick()
fmt.Println(world.String())
time.Sleep(time.Second / 30)
}
}