-
Notifications
You must be signed in to change notification settings - Fork 0
/
grid_universe.go
85 lines (74 loc) · 1.74 KB
/
grid_universe.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
package golife
import (
"bytes"
"math/rand"
)
// gridUniverse contains a representation of a rectangular universe
type gridUniverse struct {
height int
width int
grid [][]bool
}
// NewGridUniverse creates a Universe of the specified size.
// It also generates an initial randome state with ~25% alive cells.
func NewGridUniverse(width, height int) Universe {
grid := createGrid(width, height)
for i := 0; i < (width * height / 4); i++ {
grid[rand.Intn(height)][rand.Intn(width)] = true
}
return gridUniverse{
width: width,
height: height,
grid: grid,
}
}
// Apply uses the rules to generate a new generation
// using current universe grid.
func (u gridUniverse) Apply(rules Rules) Universe {
future := createGrid(u.width, u.height)
for y := 0; y < u.height; y++ {
for x := 0; x < u.width; x++ {
future[y][x] = rules(u.grid[y][x], u.neighbours(x, y))
}
}
u.grid = future
return u
}
// String represents the Universe as a string.
// Alive cells are marked with an '*'.
func (u gridUniverse) String() string {
var buf bytes.Buffer
for y := 0; y < u.height; y++ {
for x := 0; x < u.width; x++ {
b := byte(' ')
if u.grid[y][x] {
b = '*'
}
buf.WriteByte(b)
}
buf.WriteByte('\n')
}
return buf.String()
}
func (u gridUniverse) neighbours(x, y int) (count int) {
for i := -1; i <= 1; i++ {
for j := -1; j <= 1; j++ {
if (j != 0 || i != 0) && u.wrap(x+i, y+j) {
count++
}
}
}
return
}
func (u gridUniverse) wrap(x, y int) bool {
wrapped_x := (x + u.width) % u.width
wrapped_y := (y + u.height) % u.height
return u.grid[wrapped_y][wrapped_x]
}
func createGrid(width, height int) [][]bool {
grid := make([][]bool, height)
for i := range grid {
grid[i] = make([]bool, width)
}
return grid
}