You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
81 lines
1.7 KiB
81 lines
1.7 KiB
12 years ago
|
package main
|
||
|
|
||
12 years ago
|
import (
|
||
12 years ago
|
"flag"
|
||
12 years ago
|
"fmt"
|
||
|
"os"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
import (
|
||
12 years ago
|
. "github.com/meatballhat/box-o-sand/conway"
|
||
12 years ago
|
)
|
||
|
|
||
12 years ago
|
var (
|
||
|
height = flag.Int("height", 40, "Game height")
|
||
|
width = flag.Int("width", 80, "Game width")
|
||
12 years ago
|
mutate = flag.Bool("mutate", false, "Mutate every other generation")
|
||
12 years ago
|
sleepMs = flag.Int("sleep.ms", 200,
|
||
12 years ago
|
"Millisecond sleep interval per generation")
|
||
|
)
|
||
|
|
||
12 years ago
|
func pushChecksum(checksum string, checksums []string) {
|
||
12 years ago
|
head := make([]string, 3)
|
||
|
copy(head, checksums[:3])
|
||
12 years ago
|
checksums[0] = checksum
|
||
12 years ago
|
checksums[1] = head[0]
|
||
|
checksums[2] = head[1]
|
||
|
checksums[3] = head[2]
|
||
12 years ago
|
}
|
||
|
|
||
12 years ago
|
func main() {
|
||
12 years ago
|
flag.Parse()
|
||
|
|
||
|
game := NewGameOfLife(*height, *width)
|
||
12 years ago
|
err := game.ImportRandomState()
|
||
|
if err != nil {
|
||
|
fmt.Fprintf(os.Stderr, "WHAT IN FAIL?: %v\n", err)
|
||
|
os.Exit(2)
|
||
|
}
|
||
|
|
||
12 years ago
|
checksums := make([]string, 4)
|
||
|
checksums[0], checksums[1], checksums[2], checksums[3] = "foo", "bar", "baz", "qwx"
|
||
12 years ago
|
pushChecksum(game.Checksum(), checksums)
|
||
12 years ago
|
|
||
12 years ago
|
ticks := time.Tick(time.Duration(*sleepMs) * time.Millisecond)
|
||
|
generations := 0
|
||
12 years ago
|
for now := range ticks {
|
||
12 years ago
|
fmt.Printf("\nGeneration %v\n%v\n", generations, now)
|
||
12 years ago
|
fmt.Println(game)
|
||
12 years ago
|
|
||
12 years ago
|
game.EvaluateGeneration()
|
||
12 years ago
|
|
||
12 years ago
|
curChecksum := game.Checksum()
|
||
|
|
||
12 years ago
|
if checksums[0] == curChecksum || checksums[1] == curChecksum {
|
||
12 years ago
|
fmt.Println("Stasis!")
|
||
|
os.Exit(0)
|
||
|
}
|
||
|
|
||
12 years ago
|
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)!")
|
||
12 years ago
|
os.Exit(0)
|
||
|
}
|
||
|
|
||
|
pushChecksum(curChecksum, checksums)
|
||
12 years ago
|
|
||
12 years ago
|
if *mutate && generations%2 == 0 {
|
||
|
game.Mutate()
|
||
|
}
|
||
12 years ago
|
|
||
12 years ago
|
generations++
|
||
12 years ago
|
}
|
||
12 years ago
|
}
|