package conway import ( "errors" "fmt" ) type GameOfLife struct { State *GameState } type gameGridRow struct { Cols []int } type GameState struct { Rows []*gameGridRow } type GenerationScoreCard struct { GameState } func NewGameState(height, width int) *GameState { state := &GameState{} for i := 0; i < height; i++ { row := &gameGridRow{} for j := 0; j < width; j++ { row.Cols = append(row.Cols, 0) } state.Rows = append(state.Rows, row) } return state } func (state *GameState) Height() int { return len(state.Rows) } func (state *GameState) Width() int { if len(state.Rows) < 1 { return -1 } return len(state.Rows[0].Cols) } func (state *GameState) GetRow(y int) (*gameGridRow, error) { if y+1 > len(state.Rows) { return nil, errors.New("y coordinate is out of bounds!") } return state.Rows[y], nil } func (state *GameState) Set(x, y, value int) error { if x < 0 || y < 0 { errMsg := fmt.Sprintf("coordinates (%v, %v) are out of bounds!", x, y) return errors.New(errMsg) } row, err := state.GetRow(y) if err != nil { return err } if x+1 > len(row.Cols) { errMsg := fmt.Sprintf("x coordinate %v is out of bounds!", x) return errors.New(errMsg) } row.Cols[x] = value return nil } func (state *GameState) Get(x, y int) (int, error) { row, err := state.GetRow(y) if err != nil { return -1, err } if len(row.Cols) < x+1 { return -1, errors.New("x coordinate is out of bounds!") } return row.Cols[x], nil } func (state *GameState) Enliven(x, y int) error { return state.Set(x, y, 1) } func (state *GameState) Deaden(x, y int) error { return state.Set(x, y, 0) } func (state *GameState) Import(other *GameState) (err error) { for y, row := range other.Rows { for x, cell := range row.Cols { if err = state.Set(x, y, cell); err != nil { return err } } } return nil } func NewGameOfLife(height, width int) *GameOfLife { return &GameOfLife{ State: NewGameState(height, width), } } func (game *GameOfLife) EvaluateGeneration() error { height, width := game.State.Height(), game.State.Width() genScore := NewGenerationScoreCard(height, width) genScore.Calculate(game.State) for y := 0; y < height-1; y++ { for x := 0; x < width-1; x++ { score, err := genScore.Get(x, y) if err != nil { return err } curState, err := game.State.Get(x, y) if err != nil { return err } if curState == 1 { if score < 2 { err = game.State.Deaden(x, y) if err != nil { return err } continue } if score == 2 || score == 3 { continue } if score > 3 { err = game.State.Deaden(x, y) if err != nil { return err } continue } } else if curState == 0 { if score == 3 { err = game.State.Enliven(x, y) if err != nil { return err } continue } else { continue } } } } return nil } func (game *GameOfLife) ImportState(state *GameState) error { return game.State.Import(state) } func NewGenerationScoreCard(height, width int) *GenerationScoreCard { genScore := &GenerationScoreCard{} for i := 0; i < height; i++ { row := &gameGridRow{} for j := 0; j < width; j++ { row.Cols = append(row.Cols, 0) } genScore.Rows = append(genScore.Rows, row) } return genScore } 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++ { value, err := state.Get(x, y) if err != nil { return err } if value == 1 { for yOffset := -1; yOffset < 2; yOffset++ { for xOffset := -1; xOffset < 2; xOffset++ { xTarget := x + xOffset yTarget := y + yOffset if xTarget < 0 || yTarget < 0 { continue } if xTarget+1 > stateWidth || yTarget+1 > stateWidth { continue } if xTarget == x && yTarget == y { continue } curScore, err := genScore.Get(xTarget, yTarget) if err != nil { continue // assume out of bounds, which is stinky } err = genScore.Set(xTarget, yTarget, curScore+1) if err != nil { return err } } } } } } return nil }