diff --git a/conway/go/conway_test.go b/conway/go/conway_test.go index 25e3c3a..9fd2f6b 100644 --- a/conway/go/conway_test.go +++ b/conway/go/conway_test.go @@ -16,6 +16,7 @@ func init() { tsis.Set(1, 0, 1) tsis.Set(0, 1, 1) tsis.Set(1, 1, 1) + tsis.Set(2, 2, 1) } func TestNewGameOfLifeHasCorrectDimensions(t *testing.T) { @@ -25,24 +26,62 @@ func TestNewGameOfLifeHasCorrectDimensions(t *testing.T) { } } +func TestNewGameOfLifeHasGameStateOfSameDimensions(t *testing.T) { + game := NewGameOfLife(16, 16) + + if len(game.State.Rows) < 16 { + t.Fail() + } + + if len(game.State.Rows[0].Cols) < 16 { + t.Fail() + } +} + +func TestGameStateCanImportState(t *testing.T) { + game := NewGameOfLife(16, 16) + err := game.State.ImportState(TEST_SIMPLE_INITIAL_STATE) + if err != nil { + t.Fail() + } + + if value, err := game.State.Get(0, 0); err != nil || value != 1 { + t.Fail() + } + + if value, err := game.State.Get(1, 0); err != nil || value != 1 { + t.Fail() + } + + if value, err := game.State.Get(0, 1); err != nil || value != 1 { + t.Fail() + } + + if value, err := game.State.Get(1, 1); err != nil || value != 1 { + t.Fail() + } + + if value, err := game.State.Get(2, 2); err != nil || value != 1 { + t.Fail() + } +} + func TestLiveCellWithFewerThanTwoLiveNeighborsDies(t *testing.T) { - state := NewGameState(16, 16) - state.ImportState(TEST_SIMPLE_INITIAL_STATE) - - if value, err := state.Get(0, 0); err != nil || value != 1 { - t.Fail() + game := NewGameOfLife(16, 16) + err := game.State.ImportState(TEST_SIMPLE_INITIAL_STATE) + if err != nil { + t.Error(err) } - if value, err := state.Get(1, 0); err != nil || value != 1 { - t.Fail() + game.EvaluateGeneration() + + value, err := game.State.Get(2, 2) + if err != nil { + t.Error(err) } - if value, err := state.Get(0, 1); err != nil || value != 1 { - t.Fail() - } - - if value, err := state.Get(1, 1); err != nil || value != 1 { - t.Fail() + if value != 0 { + t.Errorf("%v != 0", value) } } @@ -50,7 +89,7 @@ func TestNewGameStatesAreAllDead(t *testing.T) { state := NewGameState(4, 4) for x := 0; x < 3; x++ { for y := 0; y < 3; y++ { - if state.Rows[y].Cols[x] != 0 { + if value, err := state.Get(x, y); err != nil || value != 0 { t.Fail() } } diff --git a/conway/go/game_of_life.go b/conway/go/game_of_life.go index e60100a..2f75760 100644 --- a/conway/go/game_of_life.go +++ b/conway/go/game_of_life.go @@ -7,20 +7,21 @@ import ( type GameOfLife struct { Height int Width int + State *GameState } -type GameGridRow struct { +type gameGridRow struct { Cols []int } type GameState struct { - Rows []*GameGridRow + Rows []*gameGridRow } func NewGameState(height, width int) *GameState { state := &GameState{} for i := 0; i < height; i++ { - row := &GameGridRow{} + row := &gameGridRow{} for j := 0; j < width; j++ { row.Cols = append(row.Cols, 0) } @@ -29,7 +30,7 @@ func NewGameState(height, width int) *GameState { return state } -func (state *GameState) GetRow(y int) (*GameGridRow, error) { +func (state *GameState) GetRow(y int) (*gameGridRow, error) { if len(state.Rows) < y+1 { return nil, errors.New("y coordinate is out of bounds!") } @@ -72,13 +73,6 @@ func (state *GameState) Deaden(x, y int) error { return state.Set(x, y, 0) } -func NewGameOfLife(height, width int) *GameOfLife { - return &GameOfLife{ - Height: height, - Width: width, - } -} - func (state *GameState) ImportState(other *GameState) (err error) { for y, row := range other.Rows { for x, cell := range row.Cols { @@ -89,3 +83,15 @@ func (state *GameState) ImportState(other *GameState) (err error) { } return nil } + +func NewGameOfLife(height, width int) *GameOfLife { + return &GameOfLife{ + Height: height, + Width: width, + State: NewGameState(height, width), + } +} + +func (game *GameOfLife) EvaluateGeneration() { + return +}