Implementing dead cell reproduction

This commit is contained in:
Dan Buch 2012-12-09 22:33:42 -05:00
parent c2ffaee979
commit ffb8d1e43c
2 changed files with 54 additions and 16 deletions

View File

@ -18,6 +18,7 @@ func init() {
tsis.Set(1, 0, 1) tsis.Set(1, 0, 1)
tsis.Set(1, 1, 1) tsis.Set(1, 1, 1)
tsis.Set(2, 2, 1) tsis.Set(2, 2, 1)
tsis.Set(3, 0, 1)
tsis.Set(3, 2, 1) tsis.Set(3, 2, 1)
} }
@ -89,6 +90,29 @@ func TestLiveCellWithMoreThanThreeLiveNeighborsDies(t *testing.T) {
} }
} }
func TestDeadCellWithExactlyThreeLiveNeighborsBecomesAlive(t *testing.T) {
game := NewGameOfLife(16, 16)
err := game.ImportState(TEST_SIMPLE_INITIAL_STATE)
if err != nil {
t.Error(err)
return
}
game.EvaluateGeneration()
value, err := game.State.Get(3, 1)
if err != nil {
t.Error(err)
return
}
if value != 1 {
t.Errorf("%v != 1", value)
return
}
}
func TestNewGameOfLifeHasCorrectDimensions(t *testing.T) { func TestNewGameOfLifeHasCorrectDimensions(t *testing.T) {
game := NewGameOfLife(16, 16) game := NewGameOfLife(16, 16)
if game.State.Height() != 16 || game.State.Width() != 16 { if game.State.Height() != 16 || game.State.Width() != 16 {

View File

@ -123,29 +123,43 @@ func (game *GameOfLife) EvaluateGeneration() error {
return err return err
} }
if score < 2 {
err = game.State.Deaden(x, y)
if err != nil {
return err
}
continue
}
curState, err := game.State.Get(x, y) curState, err := game.State.Get(x, y)
if err != nil { if err != nil {
return err return err
} }
if curState == 1 && (score == 2 || score == 3) { if curState == 1 {
continue
}
if curState == 1 && score > 3 { if score < 2 {
err = game.State.Deaden(x, y) err = game.State.Deaden(x, y)
if err != nil { if err != nil {
return err 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
} }
continue
} }
} }
} }