From 00b750e2b08aaf39bd55e65e643daa7247540658 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Sun, 9 Dec 2012 23:20:43 -0500 Subject: [PATCH] Plugging stuff into command-line runner including adding random initial state generation. --- conway/go/conway_test.go | 79 +++++++++++++++++++++++--- conway/go/conways-game-of-life/main.go | 24 +++++++- conway/go/game_of_life.go | 43 +++++++++++--- 3 files changed, 129 insertions(+), 17 deletions(-) diff --git a/conway/go/conway_test.go b/conway/go/conway_test.go index 585ae34..c1f5ad2 100644 --- a/conway/go/conway_test.go +++ b/conway/go/conway_test.go @@ -2,6 +2,7 @@ package conway_test import ( "fmt" + "strings" "testing" ) @@ -12,15 +13,21 @@ import ( var TEST_SIMPLE_INITIAL_STATE = NewGameState(16, 16) func init() { - tsis := TEST_SIMPLE_INITIAL_STATE - tsis.Set(0, 0, 1) - tsis.Set(0, 1, 1) - tsis.Set(0, 2, 1) - tsis.Set(1, 0, 1) - tsis.Set(1, 1, 1) - tsis.Set(2, 2, 1) - tsis.Set(3, 0, 1) - tsis.Set(3, 2, 1) + s := TEST_SIMPLE_INITIAL_STATE + s.Set(0, 0, 1) + s.Set(0, 1, 1) + s.Set(0, 2, 1) + s.Set(1, 0, 1) + s.Set(1, 1, 1) + s.Set(2, 2, 1) + s.Set(3, 0, 1) + s.Set(3, 2, 1) + s.Set(13, 13, 1) + s.Set(13, 14, 1) + s.Set(14, 14, 1) + s.Set(14, 15, 1) + s.Set(15, 13, 1) + s.Set(15, 15, 1) } func TestLiveCellWithFewerThanTwoLiveNeighborsDies(t *testing.T) { @@ -179,6 +186,60 @@ func TestGameStateCanImportState(t *testing.T) { } } +func TestGameOfLifeCanImportRandomState(t *testing.T) { + game := NewGameOfLife(16, 16) + err := game.ImportRandomState() + + if err != nil { + t.Error(err) + return + } + + zeroCount := 0 + + for y := 0; y < 16; y++ { + for x := 0; x < 16; x++ { + value, err := game.State.Get(x, y) + if err != nil { + t.Error(err) + return + } + + if value == 0 { + zeroCount++ + } + } + } + + if zeroCount == 256 { + t.Fail() + } +} + +func TestGameStateCanDisplayItself(t *testing.T) { + state := NewGameState(16, 16) + + err := state.Import(TEST_SIMPLE_INITIAL_STATE) + if err != nil { + t.Error(err) + return + } + + grid := fmt.Sprintf("%s\n", state) + lenGrid := len(grid) + + if lenGrid < 256 { + t.Errorf("%v < 256", lenGrid) + return + } + + nLines := strings.Count(grid, "\n") + if nLines < 16 { + t.Errorf("%v < 16 (lines)", nLines) + return + } +} + func TestNewGameStatesAreAllDead(t *testing.T) { state := NewGameState(4, 4) for x := 0; x < 3; x++ { diff --git a/conway/go/conways-game-of-life/main.go b/conway/go/conways-game-of-life/main.go index e0b87a2..870eae9 100644 --- a/conway/go/conways-game-of-life/main.go +++ b/conway/go/conways-game-of-life/main.go @@ -1,5 +1,27 @@ package main +import ( + "fmt" + "os" + "time" +) + +import ( + . "github.com/meatballhat/box-o-sand/conway/go" +) + func main() { - return + game := NewGameOfLife(40, 40) + err := game.ImportRandomState() + if err != nil { + fmt.Fprintf(os.Stderr, "WHAT IN FAIL?: %v\n", err) + os.Exit(2) + } + + ticks := time.Tick(1 * time.Second) + for now := range ticks { + fmt.Printf("\n\n%v\n", now) + fmt.Println(game) + game.EvaluateGeneration() + } } diff --git a/conway/go/game_of_life.go b/conway/go/game_of_life.go index 8dd5405..d8aa865 100644 --- a/conway/go/game_of_life.go +++ b/conway/go/game_of_life.go @@ -3,6 +3,7 @@ package conway import ( "errors" "fmt" + "math/rand" "strings" ) @@ -117,8 +118,8 @@ func (game *GameOfLife) EvaluateGeneration() error { genScore := NewGenerationScoreCard(height, width) genScore.Calculate(game.State) - for y := 0; y < height-1; y++ { - for x := 0; x < width-1; x++ { + for y := 0; y < height; y++ { + for x := 0; x < width; x++ { score, err := genScore.Get(x, y) if err != nil { return err @@ -172,18 +173,46 @@ func (game *GameOfLife) ImportState(state *GameState) error { 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 { + return fmt.Sprintf("%s\n", game.State) +} + +func (state *GameState) String() string { var rows []string - height := game.State.Height() - width := game.State.Width() + height := state.Height() + width := state.Width() for y := 0; y < height; y++ { var cells []string for x := 0; x < width; x++ { stringVal := "X" - value, err := game.State.Get(x, y) + value, err := state.Get(x, y) if err != nil { return "" } @@ -212,8 +241,8 @@ func NewGenerationScoreCard(height, width int) *GenerationScoreCard { func (genScore *GenerationScoreCard) Calculate(state *GameState) error { stateHeight, stateWidth := state.Height(), state.Width() - for y := 0; y < stateHeight-1; y++ { - for x := 0; x < stateWidth-1; x++ { + for y := 0; y < stateHeight; y++ { + for x := 0; x < stateWidth; x++ { value, err := state.Get(x, y) if err != nil { return err