allow initial food placement for 8 snakes on 7x7 board (#87)

* allow initial food placement for 8 snakes on 7x7

* fix logic to be for small board

* fix inverted logic

* logic should be actually <= 4 snakes, not 7
This commit is contained in:
Torben 2022-06-22 16:14:15 -07:00 committed by GitHub
parent 5ecc285dcd
commit f58df66e69
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 81 additions and 48 deletions

View file

@ -213,6 +213,10 @@ func PlaceFoodAutomatically(rand Rand, b *BoardState) error {
func PlaceFoodFixed(rand Rand, b *BoardState) error {
centerCoord := Point{(b.Width - 1) / 2, (b.Height - 1) / 2}
isSmallBoard := b.Width*b.Height <= BoardSizeSmall*BoardSizeSmall
// Up to 4 snakes can be placed such that food is nearby on small boards.
// Otherwise, we skip this and only try to place food in the center.
if len(b.Snakes) <= 4 || !isSmallBoard {
// Place 1 food within exactly 2 moves of each snake, but never towards the center or in a corner
for i := 0; i < len(b.Snakes); i++ {
snakeHead := b.Snakes[i].Body[0]
@ -270,6 +274,7 @@ func PlaceFoodFixed(rand Rand, b *BoardState) error {
placedFood := availableFoodLocations[rand.Intn(len(availableFoodLocations))]
b.Food = append(b.Food, placedFood)
}
}
// Finally, always place 1 food in center of board for dramatic purposes
isCenterOccupied := true

View file

@ -8,6 +8,34 @@ import (
"github.com/stretchr/testify/require"
)
func TestDev1235(t *testing.T) {
// Small boards should no longer error and only get 1 food when num snakes > 4
state, err := CreateDefaultBoardState(MaxRand, BoardSizeSmall, BoardSizeSmall, []string{
"1", "2", "3", "4", "5", "6", "7", "8",
})
require.NoError(t, err)
require.Len(t, state.Food, 1)
state, err = CreateDefaultBoardState(MaxRand, BoardSizeSmall, BoardSizeSmall, []string{
"1", "2", "3", "4", "5",
})
require.NoError(t, err)
require.Len(t, state.Food, 1)
// Small boards with <= 4 snakes should still get more than just center food
state, err = CreateDefaultBoardState(MaxRand, BoardSizeSmall, BoardSizeSmall, []string{
"1", "2", "3", "4",
})
require.NoError(t, err)
require.Len(t, state.Food, 5)
// Medium boards should still get 9 food
state, err = CreateDefaultBoardState(MaxRand, BoardSizeMedium, BoardSizeMedium, []string{
"1", "2", "3", "4", "5", "6", "7", "8",
})
require.NoError(t, err)
require.Len(t, state.Food, 9)
}
func sortPoints(p []Point) {
sort.Slice(p, func(i, j int) bool {
if p[i].X != p[j].X {