48 lines
776 B
Go
48 lines
776 B
Go
|
package conway
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"math/rand"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
func init() {
|
||
|
rand.Seed(time.Now().UTC().UnixNano())
|
||
|
}
|
||
|
|
||
|
func RunConsoleGame(height, width, sleepMs int, mutate bool) (int, error) {
|
||
|
sleepInterval := time.Duration(sleepMs * 1000 * 1000)
|
||
|
|
||
|
game := NewGameOfLife(height, width)
|
||
|
err := game.ImportRandomState()
|
||
|
if err != nil {
|
||
|
return 2, err
|
||
|
}
|
||
|
|
||
|
genTicks, err := game.Generations()
|
||
|
if err != nil {
|
||
|
return 3, err
|
||
|
}
|
||
|
|
||
|
for genTick := range genTicks {
|
||
|
fmt.Printf("\nGeneration %v\n%v\n", genTick.N, time.Now())
|
||
|
fmt.Println(game)
|
||
|
|
||
|
if genTick.Error != nil {
|
||
|
return 4, genTick.Error
|
||
|
}
|
||
|
|
||
|
if len(genTick.Message) > 0 {
|
||
|
fmt.Println(genTick.Message)
|
||
|
}
|
||
|
|
||
|
if mutate && genTick.N%2 == 0 {
|
||
|
game.Mutate()
|
||
|
}
|
||
|
|
||
|
time.Sleep(sleepInterval)
|
||
|
}
|
||
|
|
||
|
return 0, nil
|
||
|
}
|