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:
parent
ad1bd2537a
commit
3ff6e2e999
@ -29,15 +29,27 @@ func main() {
|
|||||||
os.Exit(2)
|
os.Exit(2)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cksum := game.Checksum()
|
||||||
|
|
||||||
ticks := time.Tick(time.Duration(*sleepMs) * time.Millisecond)
|
ticks := time.Tick(time.Duration(*sleepMs) * time.Millisecond)
|
||||||
generations := 0
|
generations := 0
|
||||||
for now := range ticks {
|
for now := range ticks {
|
||||||
fmt.Printf("\nGeneration %v\n%v\n", generations, now)
|
fmt.Printf("\nGeneration %v\n%v\n", generations, now)
|
||||||
fmt.Println(game)
|
fmt.Println(game)
|
||||||
|
|
||||||
game.EvaluateGeneration()
|
game.EvaluateGeneration()
|
||||||
|
|
||||||
|
if cksum == game.Checksum() {
|
||||||
|
fmt.Println("Stasis!")
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
cksum = game.Checksum()
|
||||||
|
|
||||||
if *mutate && generations%2 == 0 {
|
if *mutate && generations%2 == 0 {
|
||||||
game.Mutate()
|
game.Mutate()
|
||||||
}
|
}
|
||||||
|
|
||||||
generations++
|
generations++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,8 +7,13 @@ import (
|
|||||||
"math"
|
"math"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
rand.Seed(time.Now().UTC().UnixNano())
|
||||||
|
}
|
||||||
|
|
||||||
type GameOfLife struct {
|
type GameOfLife struct {
|
||||||
State *GameState
|
State *GameState
|
||||||
}
|
}
|
||||||
@ -159,6 +164,10 @@ func (state *GameState) Mutate() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (game *GameOfLife) Checksum() string {
|
||||||
|
return game.State.Checksum()
|
||||||
|
}
|
||||||
|
|
||||||
func (state *GameState) Checksum() string {
|
func (state *GameState) Checksum() string {
|
||||||
ck := sha1.New()
|
ck := sha1.New()
|
||||||
cells := make(chan *GameStateCell)
|
cells := make(chan *GameStateCell)
|
||||||
@ -282,7 +291,7 @@ func (state *GameState) String() string {
|
|||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
if value == 0 {
|
if value == 1 {
|
||||||
stringVal = "·"
|
stringVal = "·"
|
||||||
}
|
}
|
||||||
cells = append(cells, stringVal)
|
cells = append(cells, stringVal)
|
||||||
|
Loading…
Reference in New Issue
Block a user