Ending game automatically when statis is reached

plus properly seeding math/rand and swapping the chars used for grid to
better match black/white from canonical example.
This commit is contained in:
Dan Buch 2012-12-10 00:35:09 -05:00
parent ad1bd2537a
commit 3ff6e2e999
2 changed files with 22 additions and 1 deletions

View File

@ -29,15 +29,27 @@ func main() {
os.Exit(2)
}
cksum := game.Checksum()
ticks := time.Tick(time.Duration(*sleepMs) * time.Millisecond)
generations := 0
for now := range ticks {
fmt.Printf("\nGeneration %v\n%v\n", generations, now)
fmt.Println(game)
game.EvaluateGeneration()
if cksum == game.Checksum() {
fmt.Println("Stasis!")
os.Exit(0)
}
cksum = game.Checksum()
if *mutate && generations%2 == 0 {
game.Mutate()
}
generations++
}
}

View File

@ -7,8 +7,13 @@ import (
"math"
"math/rand"
"strings"
"time"
)
func init() {
rand.Seed(time.Now().UTC().UnixNano())
}
type GameOfLife struct {
State *GameState
}
@ -159,6 +164,10 @@ func (state *GameState) Mutate() error {
return nil
}
func (game *GameOfLife) Checksum() string {
return game.State.Checksum()
}
func (state *GameState) Checksum() string {
ck := sha1.New()
cells := make(chan *GameStateCell)
@ -282,7 +291,7 @@ func (state *GameState) String() string {
return ""
}
if value == 0 {
if value == 1 {
stringVal = "·"
}
cells = append(cells, stringVal)