|
|
@ -3,6 +3,7 @@ package conway
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"math/rand"
|
|
|
|
"strings"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
@ -117,8 +118,8 @@ func (game *GameOfLife) EvaluateGeneration() error {
|
|
|
|
genScore := NewGenerationScoreCard(height, width)
|
|
|
|
genScore := NewGenerationScoreCard(height, width)
|
|
|
|
genScore.Calculate(game.State)
|
|
|
|
genScore.Calculate(game.State)
|
|
|
|
|
|
|
|
|
|
|
|
for y := 0; y < height-1; y++ {
|
|
|
|
for y := 0; y < height; y++ {
|
|
|
|
for x := 0; x < width-1; x++ {
|
|
|
|
for x := 0; x < width; x++ {
|
|
|
|
score, err := genScore.Get(x, y)
|
|
|
|
score, err := genScore.Get(x, y)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
@ -172,18 +173,46 @@ func (game *GameOfLife) ImportState(state *GameState) error {
|
|
|
|
return game.State.Import(state)
|
|
|
|
return game.State.Import(state)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (game *GameOfLife) ImportRandomState() error {
|
|
|
|
|
|
|
|
height := game.State.Height()
|
|
|
|
|
|
|
|
width := game.State.Width()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if height < 0 || width < 0 {
|
|
|
|
|
|
|
|
errStr := fmt.Sprintf("current game has invalid dimensions! %vx%v",
|
|
|
|
|
|
|
|
height, width)
|
|
|
|
|
|
|
|
return errors.New(errStr)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
randState := NewGameState(height, width)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for y := 0; y < height; y++ {
|
|
|
|
|
|
|
|
for x := 0; x < width; x++ {
|
|
|
|
|
|
|
|
err := randState.Set(x, y, rand.Intn(2))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return game.ImportState(randState)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (game *GameOfLife) String() string {
|
|
|
|
func (game *GameOfLife) String() string {
|
|
|
|
|
|
|
|
return fmt.Sprintf("%s\n", game.State)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (state *GameState) String() string {
|
|
|
|
var rows []string
|
|
|
|
var rows []string
|
|
|
|
|
|
|
|
|
|
|
|
height := game.State.Height()
|
|
|
|
height := state.Height()
|
|
|
|
width := game.State.Width()
|
|
|
|
width := state.Width()
|
|
|
|
|
|
|
|
|
|
|
|
for y := 0; y < height; y++ {
|
|
|
|
for y := 0; y < height; y++ {
|
|
|
|
var cells []string
|
|
|
|
var cells []string
|
|
|
|
for x := 0; x < width; x++ {
|
|
|
|
for x := 0; x < width; x++ {
|
|
|
|
stringVal := "X"
|
|
|
|
stringVal := "X"
|
|
|
|
|
|
|
|
|
|
|
|
value, err := game.State.Get(x, y)
|
|
|
|
value, err := state.Get(x, y)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -212,8 +241,8 @@ func NewGenerationScoreCard(height, width int) *GenerationScoreCard {
|
|
|
|
|
|
|
|
|
|
|
|
func (genScore *GenerationScoreCard) Calculate(state *GameState) error {
|
|
|
|
func (genScore *GenerationScoreCard) Calculate(state *GameState) error {
|
|
|
|
stateHeight, stateWidth := state.Height(), state.Width()
|
|
|
|
stateHeight, stateWidth := state.Height(), state.Width()
|
|
|
|
for y := 0; y < stateHeight-1; y++ {
|
|
|
|
for y := 0; y < stateHeight; y++ {
|
|
|
|
for x := 0; x < stateWidth-1; x++ {
|
|
|
|
for x := 0; x < stateWidth; x++ {
|
|
|
|
value, err := state.Get(x, y)
|
|
|
|
value, err := state.Get(x, y)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|