Implementing overcrowding rule

This commit is contained in:
Dan Buch 2012-12-09 22:16:16 -05:00
parent aaba7c22df
commit c2ffaee979
2 changed files with 91 additions and 48 deletions

View File

@ -13,10 +13,80 @@ var TEST_SIMPLE_INITIAL_STATE = NewGameState(16, 16)
func init() {
tsis := TEST_SIMPLE_INITIAL_STATE
tsis.Set(0, 0, 1)
tsis.Set(1, 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, 2, 1)
}
func TestLiveCellWithFewerThanTwoLiveNeighborsDies(t *testing.T) {
game := NewGameOfLife(16, 16)
err := game.State.Import(TEST_SIMPLE_INITIAL_STATE)
if err != nil {
t.Error(err)
return
}
game.EvaluateGeneration()
value, err := game.State.Get(3, 2)
if err != nil {
t.Error(err)
return
}
if value != 0 {
t.Errorf("%v != 0", value)
return
}
}
func TestLiveCellWithTwoOrThreeLiveNeighborsLivesOn(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(0, 0)
if err != nil {
t.Error(err)
return
}
if value != 1 {
t.Errorf("%v != 1", value)
return
}
}
func TestLiveCellWithMoreThanThreeLiveNeighborsDies(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(0, 1)
if err != nil {
t.Error(err)
return
}
if value != 0 {
t.Errorf("%v != 0", value)
return
}
}
func TestNewGameOfLifeHasCorrectDimensions(t *testing.T) {
@ -66,51 +136,6 @@ func TestGameStateCanImportState(t *testing.T) {
}
}
func TestLiveCellWithFewerThanTwoLiveNeighborsDies(t *testing.T) {
game := NewGameOfLife(16, 16)
err := game.State.Import(TEST_SIMPLE_INITIAL_STATE)
if err != nil {
t.Error(err)
return
}
game.EvaluateGeneration()
value, err := game.State.Get(2, 2)
if err != nil {
t.Error(err)
return
}
if value != 0 {
t.Errorf("%v != 0", value)
return
}
}
func TestLiveCellWithTwoOrThreeLiveNeighborsLivesOn(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(0, 0)
if err != nil {
t.Error(err)
return
}
if value != 1 {
t.Errorf("%v != 1", value)
return
}
}
func TestNewGameStatesAreAllDead(t *testing.T) {
state := NewGameState(4, 4)
for x := 0; x < 3; x++ {
@ -203,12 +228,12 @@ func TestGenerationScoreCardCalculateResultsInCorrectNeighborCounts(t *testing.T
return
}
value, err = genScore.Get(2, 2)
value, err = genScore.Get(3, 2)
if err != nil {
t.Error(err)
return
} else if value != 1 {
t.Errorf("[2, 2] value %d != 1", value)
t.Errorf("[3, 2] value %d != 1", value)
return
}
}

View File

@ -128,6 +128,24 @@ func (game *GameOfLife) EvaluateGeneration() error {
if err != nil {
return err
}
continue
}
curState, err := game.State.Get(x, y)
if err != nil {
return err
}
if curState == 1 && (score == 2 || score == 3) {
continue
}
if curState == 1 && score > 3 {
err = game.State.Deaden(x, y)
if err != nil {
return err
}
continue
}
}
}