Created
June 1, 2015 03:14
-
-
Save fvbock/ba2adf95849640c6f76b to your computer and use it in GitHub Desktop.
game of life - section 2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"math/rand" | |
"time" | |
) | |
type World struct { | |
Width int | |
Height int | |
Cells [][]*Cell | |
} | |
func (w *World) String() (s string) { | |
for _, row := range w.Cells { | |
s += fmt.Sprintf("%s\n", row) | |
} | |
return | |
} | |
func NewWorld(width int, height int) (w *World) { | |
w = &World{ | |
Height: height, | |
Width: width, | |
Cells: make([][]*Cell, width), | |
} | |
for x := 0; x < w.Width; x++ { | |
w.Cells[x] = make([]*Cell, height) | |
} | |
return w | |
} | |
func (w *World) GetNeighbors(c *Cell) (neighbors []*Cell) { | |
neighbors = []*Cell{} | |
for x := c.PositionX - 1; x < c.PositionX+2 && x < w.Width; x++ { | |
// log.Println("x", x) | |
if x == -1 { | |
continue | |
} | |
innerLoop: | |
for y := c.PositionY - 1; y < c.PositionY+2 && y < w.Height; y++ { | |
// log.Println("y", y) | |
if y == -1 { | |
continue innerLoop | |
} | |
if x != c.PositionX || y != c.PositionY { | |
// log.Println("ADD", x, y) | |
neighbors = append(neighbors, w.Cells[x][y]) | |
} | |
} | |
} | |
// fmt.Println("==", c.PositionX, c.PositionY, neighbors) | |
return | |
} | |
func (w *World) Initialize() { | |
rand.Seed(time.Now().UnixNano()) | |
for x := 0; x < w.Width; x++ { | |
for y := 0; y < w.Height; y++ { | |
w.Cells[x][y] = &Cell{ | |
PositionX: x, | |
PositionY: y, | |
} | |
r := rand.Intn(9) | |
if r >= 7 { | |
w.Cells[x][y].Alive = true | |
} | |
} | |
} | |
} | |
func (w *World) NextGeneration() (newWorld *World) { | |
newWorld = NewWorld(w.Width, w.Height) | |
for x := 0; x < w.Width; x++ { | |
for y := 0; y < w.Height; y++ { | |
newWorld.Cells[x][y] = w.Cells[x][y].NextGeneration() | |
} | |
} | |
return | |
} | |
type Cell struct { | |
PositionX int | |
PositionY int | |
Alive bool | |
} | |
func (c *Cell) String() string { | |
if c.Alive { | |
return "π" // "π" | |
} | |
return "γ" // "π²" | |
} | |
func (c *Cell) NextGeneration() (nc *Cell) { | |
var aliveCount int = 0 | |
// fmt.Println("==", c.PositionX, c.PositionY, TheWorld.GetNeighbors(c)) | |
for _, neighborCell := range TheWorld.GetNeighbors(c) { | |
if neighborCell.Alive { | |
aliveCount++ | |
} | |
} | |
nc = &Cell{ | |
PositionX: c.PositionX, | |
PositionY: c.PositionY, | |
} | |
if c.Alive { | |
nc.Alive = (aliveCount == 2 || aliveCount == 3) | |
} else { | |
nc.Alive = (aliveCount == 3) | |
} | |
return | |
} | |
var ( | |
TheWorld *World | |
) | |
func main() { | |
TheWorld = NewWorld(10, 10) | |
TheWorld.Initialize() | |
fmt.Println(TheWorld) | |
for i := 0; i < 100; i++ { | |
TheWorld = TheWorld.NextGeneration() | |
time.Sleep(100 * time.Millisecond) | |
fmt.Println(TheWorld) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment