Maintain at least 1 food on board at all times.
This commit is contained in:
parent
6edb4ce5d4
commit
ccd7a4e47d
2 changed files with 13 additions and 11 deletions
|
|
@ -174,7 +174,7 @@ func (r *StandardRuleset) ResolveMoves(prevState *BoardState, moves []SnakeMove)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: LOG?
|
// TODO: LOG?
|
||||||
err = r.maybeSpawnFood(nextState, 1)
|
err = r.maybeSpawnFood(nextState)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
@ -361,9 +361,9 @@ func (r *StandardRuleset) feedSnakes(b *BoardState) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *StandardRuleset) maybeSpawnFood(b *BoardState, n int) error {
|
func (r *StandardRuleset) maybeSpawnFood(b *BoardState) error {
|
||||||
if rand.Float32() <= FoodSpawnChance {
|
if len(b.Food) == 0 || rand.Float32() <= FoodSpawnChance {
|
||||||
return r.spawnFood(b, n)
|
return r.spawnFood(b, 1)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1091,15 +1091,16 @@ func TestGetUnoccupiedPoints(t *testing.T) {
|
||||||
func TestMaybeSpawnFood(t *testing.T) {
|
func TestMaybeSpawnFood(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
Seed int64
|
Seed int64
|
||||||
|
Food []Point
|
||||||
ExpectedFood []Point
|
ExpectedFood []Point
|
||||||
}{
|
}{
|
||||||
// Use pre-tested seeds and results
|
// Use pre-tested seeds and results
|
||||||
{123, []Point{}},
|
{123, []Point{}, []Point{{2, 2}}},
|
||||||
{456, []Point{}},
|
{456, []Point{{4, 4}}, []Point{{4, 4}}},
|
||||||
{789, []Point{}},
|
{789, []Point{{4, 4}}, []Point{{4, 4}}},
|
||||||
{1024, []Point{{2, 1}}},
|
{1024, []Point{}, []Point{{4, 1}}},
|
||||||
{511, []Point{{2, 0}}},
|
{511, []Point{{4, 4}}, []Point{{4, 4}, {2, 0}}},
|
||||||
{165, []Point{{3, 1}}},
|
{165, []Point{{4, 4}}, []Point{{4, 4}, {3, 1}}},
|
||||||
}
|
}
|
||||||
|
|
||||||
r := StandardRuleset{}
|
r := StandardRuleset{}
|
||||||
|
|
@ -1111,10 +1112,11 @@ func TestMaybeSpawnFood(t *testing.T) {
|
||||||
{Body: []Point{{1, 0}, {1, 1}}},
|
{Body: []Point{{1, 0}, {1, 1}}},
|
||||||
{Body: []Point{{0, 1}, {0, 2}, {0, 3}}},
|
{Body: []Point{{0, 1}, {0, 2}, {0, 3}}},
|
||||||
},
|
},
|
||||||
|
Food: test.Food,
|
||||||
}
|
}
|
||||||
|
|
||||||
rand.Seed(test.Seed)
|
rand.Seed(test.Seed)
|
||||||
err := r.maybeSpawnFood(b, 1)
|
err := r.maybeSpawnFood(b)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Equal(t, len(test.ExpectedFood), len(b.Food), "Seed %d", test.Seed)
|
require.Equal(t, len(test.ExpectedFood), len(b.Food), "Seed %d", test.Seed)
|
||||||
for i, e := range test.ExpectedFood {
|
for i, e := range test.ExpectedFood {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue