2012-12-09 02:47:21 +00:00
|
|
|
package main
|
|
|
|
|
2012-12-10 04:20:43 +00:00
|
|
|
import (
|
2012-12-10 04:38:38 +00:00
|
|
|
"flag"
|
2012-12-10 04:20:43 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
import (
|
2012-12-11 23:28:10 +00:00
|
|
|
. "github.com/meatballhat/box-o-sand/conway"
|
2012-12-10 04:20:43 +00:00
|
|
|
)
|
|
|
|
|
2012-12-10 04:38:38 +00:00
|
|
|
var (
|
|
|
|
height = flag.Int("height", 40, "Game height")
|
|
|
|
width = flag.Int("width", 80, "Game width")
|
2012-12-10 05:19:31 +00:00
|
|
|
mutate = flag.Bool("mutate", false, "Mutate every other generation")
|
2012-12-10 14:14:29 +00:00
|
|
|
sleepMs = flag.Int("sleep.ms", 200,
|
2012-12-10 04:38:38 +00:00
|
|
|
"Millisecond sleep interval per generation")
|
|
|
|
)
|
|
|
|
|
2012-12-10 14:14:29 +00:00
|
|
|
func pushChecksum(checksum string, checksums []string) {
|
2012-12-10 14:39:58 +00:00
|
|
|
head := make([]string, 3)
|
|
|
|
copy(head, checksums[:3])
|
2012-12-10 14:14:29 +00:00
|
|
|
checksums[0] = checksum
|
2012-12-10 14:39:58 +00:00
|
|
|
checksums[1] = head[0]
|
|
|
|
checksums[2] = head[1]
|
|
|
|
checksums[3] = head[2]
|
2012-12-10 14:14:29 +00:00
|
|
|
}
|
|
|
|
|
2012-12-09 02:47:21 +00:00
|
|
|
func main() {
|
2012-12-10 04:38:38 +00:00
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
game := NewGameOfLife(*height, *width)
|
2012-12-10 04:20:43 +00:00
|
|
|
err := game.ImportRandomState()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "WHAT IN FAIL?: %v\n", err)
|
|
|
|
os.Exit(2)
|
|
|
|
}
|
|
|
|
|
2012-12-10 14:39:58 +00:00
|
|
|
checksums := make([]string, 4)
|
|
|
|
checksums[0], checksums[1], checksums[2], checksums[3] = "foo", "bar", "baz", "qwx"
|
2012-12-12 03:30:34 +00:00
|
|
|
ck, err := game.Checksum()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "NAY NAY: %v\n", err)
|
|
|
|
os.Exit(3)
|
|
|
|
}
|
|
|
|
pushChecksum(ck, checksums)
|
2012-12-10 05:35:09 +00:00
|
|
|
|
2012-12-10 04:38:38 +00:00
|
|
|
ticks := time.Tick(time.Duration(*sleepMs) * time.Millisecond)
|
|
|
|
generations := 0
|
2012-12-10 04:20:43 +00:00
|
|
|
for now := range ticks {
|
2012-12-10 04:38:38 +00:00
|
|
|
fmt.Printf("\nGeneration %v\n%v\n", generations, now)
|
2012-12-10 04:20:43 +00:00
|
|
|
fmt.Println(game)
|
2012-12-10 05:35:09 +00:00
|
|
|
|
2012-12-10 04:20:43 +00:00
|
|
|
game.EvaluateGeneration()
|
2012-12-10 05:35:09 +00:00
|
|
|
|
2012-12-12 03:30:34 +00:00
|
|
|
curChecksum, err := game.Checksum()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "Failed to calculate current game checksum: %v\n", err)
|
|
|
|
os.Exit(4)
|
|
|
|
}
|
2012-12-10 14:14:29 +00:00
|
|
|
|
2012-12-10 14:39:58 +00:00
|
|
|
if checksums[0] == curChecksum || checksums[1] == curChecksum {
|
2012-12-10 05:35:09 +00:00
|
|
|
fmt.Println("Stasis!")
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
2012-12-10 14:39:58 +00:00
|
|
|
if checksums[2] == curChecksum {
|
|
|
|
fmt.Println("Checksum found 2 periods ago")
|
|
|
|
fmt.Println("Stasis with 2-period oscillator(s)!")
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
if checksums[3] == curChecksum {
|
|
|
|
fmt.Println("Checksum found 3 periods ago")
|
|
|
|
fmt.Println("Stasis with 3-period oscillator(s)!")
|
2012-12-10 14:14:29 +00:00
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
pushChecksum(curChecksum, checksums)
|
2012-12-10 05:35:09 +00:00
|
|
|
|
2012-12-10 05:19:31 +00:00
|
|
|
if *mutate && generations%2 == 0 {
|
|
|
|
game.Mutate()
|
|
|
|
}
|
2012-12-10 05:35:09 +00:00
|
|
|
|
2012-12-10 04:38:38 +00:00
|
|
|
generations++
|
2012-12-10 04:20:43 +00:00
|
|
|
}
|
2012-12-09 02:47:21 +00:00
|
|
|
}
|