box-o-sand/conway/conways-game-of-life/main.go

61 lines
1.1 KiB
Go

package main
import (
"flag"
"fmt"
"os"
"time"
)
import (
. "github.com/meatballhat/box-o-sand/conway"
)
var (
height = flag.Int("height", 40, "Game height")
width = flag.Int("width", 80, "Game width")
mutate = flag.Bool("mutate", false, "Mutate every other generation")
sleepMs = flag.Int("sleep.ms", 200,
"Millisecond sleep interval per generation")
)
func main() {
flag.Parse()
game := NewGameOfLife(*height, *width)
err := game.ImportRandomState()
if err != nil {
fmt.Fprintf(os.Stderr, "WHAT IN FAIL?: %v\n", err)
os.Exit(2)
}
genTicks, err := game.Generations()
if err != nil {
fmt.Fprintf(os.Stderr, "No generations?: %v\n", err)
os.Exit(3)
}
sleepInterval := time.Duration(*sleepMs * 1000 * 1000)
for genTick := range genTicks {
fmt.Printf("\nGeneration %v\n%v\n", genTick.N, time.Now())
fmt.Println(game)
game.EvaluateGeneration()
if genTick.Error != nil {
fmt.Fprintf(os.Stderr, "%s: %s\n", genTick.Message, genTick.Error)
os.Exit(4)
}
if len(genTick.Message) > 0 {
fmt.Println(genTick.Message)
}
if *mutate && genTick.N%2 == 0 {
game.Mutate()
}
time.Sleep(sleepInterval)
}
}