Byte-snake-engine/maps/registry_test.go
Corey Alexander 08cb7ae61d
[Custom Map] Single Player Maze (#81)
* Commit from Stream

We got the maze working decently well!

Started off with exploring the repo a bit and getting used to the Map
stuff

The Maze is working pretty well except the mazes that are generated
aren't actually complete-able yet. They end up creating spaces that are
blocked off from the rest of the map

The maze generation algo is based off 'Recursive division method' mostly
following this Wiki

This looked to always create a 'valid' maze, while mine doesn't so need
to figure out what my bug is, cause I'm pretty sure there is one!

* I think we got maze generation working!

Did a lot, should have commited more lol

Got map generation working where I can see it go frame by frame, and
think we squashed the last few bugs with wall generation and hole
cutting

Now need it to actually make a new maze when you finish this first one

* We can run on a bigger board now, and we place the food randomly on the board after you grab it the first time

* Make sure my health is always 100 so don't have to worry about that lol

* Fix placing of food on maze to NOT be on an existing hazard and after 3 foods make a new maze

* Maze generation works better now and a bit of cleanup

* Committing old code

* Got state saving as Hazards Bits Working well, I think whats up next is making the map grow each level

* Draw maze in the center of the board, the maze grows as you complete levels until it fills the entire board

* Ran go fmt

* Remove my little debugging script that I added

* Revert changes I made initially to the map interface

* Move my copy-pasted printMap function to my map specifically

* Go fmt

* Write some doc comments and fix some weird code artifacts

* A few more comments and one last go fmt

* Cut less holes, this makes the maze more exciting

* Add version to Metadata and go fmt again

* Can collapse this down

* Random food spawns are more fun for sure

* add minimal support for serving a game to the board UI

* We did 3 main things here:

- Fixed the bug where hazards were getting multi-stacked
- Fixed the bug where the food could spawn on top of the snake head
- Fixed the lint errors

* Games aren't infinite now, they end by not spawning a food when you have reached a certain number of turns at the max board size

* Extract some functions to make deciding when the game is over easier

* Get evil mode implemented to force the game to end

* Fix small bugs about where we spawned the food in evil mode

* Have the snake keep growing by adding tail segments to match the current level. We start at two length because 1 lenth snakes look weird on the board since they don't have a neck to orient themselves. Here we also fix a bug where evil mode was trying to place food off the map

* Run go fmt

* Add the missing meta-data pieces

* Support a smaller board without the change of infinite loops and change the name of the maze

* Rename the actual struct and fix the tests to not make extra snakes when the map doesn't support it

* Revert "add minimal support for serving a game to the board UI"

This reverts commit 0ac3592c7669ab1bccd7bd3322adffbef5e911ce.

* add minimal support for serving a game to the board UI

* Cleanup and extract and function to place food so we don't have basically the same body twice. This also will prevent the initial food spawn from being right next to the snake

* Revert "add minimal support for serving a game to the board UI"

This reverts commit c1a3b134213dabf44df56e3ca1e6d30ff8aaa516.

* Fix lint

* 2 length snakes are silly, so lets start with 3 each time

* We start out with 3 length snakes, and a fixed set of map sizes

Co-authored-by: Rob O'Dwyer <odwyerrob@gmail.com>
2022-07-04 10:25:28 -07:00

114 lines
4.9 KiB
Go

package maps
import (
"fmt"
"testing"
"github.com/BattlesnakeOfficial/rules"
"github.com/stretchr/testify/require"
)
const maxBoardWidth, maxBoardHeight = 25, 25
var testSettings rules.Settings = rules.Settings{
FoodSpawnChance: 25,
MinimumFood: 1,
HazardDamagePerTurn: 14,
RoyaleSettings: rules.RoyaleSettings{
ShrinkEveryNTurns: 1,
},
}
func TestRegisteredMaps(t *testing.T) {
for mapName, gameMap := range globalRegistry {
t.Run(mapName, func(t *testing.T) {
require.Equalf(t, mapName, gameMap.ID(), "%#v game map doesn't return its own ID", mapName)
meta := gameMap.Meta()
require.True(t, meta.Version > 0, fmt.Sprintf("registered maps must have a valid version (>= 1) - '%d' is invalid", meta.Version))
require.NotZero(t, meta.MinPlayers, "registered maps must have minimum players declared")
require.NotZero(t, meta.MaxPlayers, "registered maps must have maximum players declared")
require.LessOrEqual(t, meta.MaxPlayers, meta.MaxPlayers, "max players should always be >= min players")
require.NotEmpty(t, meta.BoardSizes, "registered maps must have at least one supported size declared")
var setupBoardState *rules.BoardState
// "fuzz test" supported players
mapSize := pickSize(meta)
for i := meta.MinPlayers; i < meta.MaxPlayers; i++ {
t.Run(fmt.Sprintf("%d players", i), func(t *testing.T) {
initialBoardState := rules.NewBoardState(int(mapSize.Width), int(mapSize.Height))
for j := uint(0); j < i; j++ {
initialBoardState.Snakes = append(initialBoardState.Snakes, rules.Snake{ID: fmt.Sprint(j), Body: []rules.Point{}})
}
err := gameMap.SetupBoard(initialBoardState, testSettings, NewBoardStateEditor(initialBoardState))
require.NoError(t, err, fmt.Sprintf("%d players should be supported by this map", i))
})
}
// "fuzz test" supported map sizes
if !meta.BoardSizes.IsUnlimited() {
for _, mapSize := range meta.BoardSizes {
t.Run(fmt.Sprintf("%dx%d map size", mapSize.Width, mapSize.Height), func(t *testing.T) {
initialBoardState := rules.NewBoardState(int(mapSize.Width), int(mapSize.Height))
for i := uint(0); i < meta.MaxPlayers; i++ {
initialBoardState.Snakes = append(initialBoardState.Snakes, rules.Snake{ID: fmt.Sprint(i), Body: []rules.Point{}})
}
err := gameMap.SetupBoard(initialBoardState, testSettings, NewBoardStateEditor(initialBoardState))
require.NoError(t, err, "error setting up map")
})
}
}
// Check that at least one map size can be setup without error
for width := 0; width < maxBoardWidth; width++ {
for height := 0; height < maxBoardHeight; height++ {
initialBoardState := rules.NewBoardState(width, height)
initialBoardState.Snakes = append(initialBoardState.Snakes, rules.Snake{ID: "1", Body: []rules.Point{}})
if meta.MaxPlayers > 1 {
initialBoardState.Snakes = append(initialBoardState.Snakes, rules.Snake{ID: "2", Body: []rules.Point{}})
}
passedBoardState := initialBoardState.Clone()
tempBoardState := initialBoardState.Clone()
err := gameMap.SetupBoard(passedBoardState, testSettings, NewBoardStateEditor(tempBoardState))
if err == nil {
setupBoardState = tempBoardState
require.Equal(t, initialBoardState, passedBoardState, "BoardState should not be modified directly by GameMap.SetupBoard")
break
}
}
}
require.NotNil(t, setupBoardState, "Map does not successfully setup the board at any supported combination of width and height")
require.NotNil(t, setupBoardState.Food)
require.NotNil(t, setupBoardState.Hazards)
require.NotNil(t, setupBoardState.Snakes)
for _, snake := range setupBoardState.Snakes {
require.NotEmpty(t, snake.Body, "Map should place all snakes by initializing their body")
}
previousBoardState := rules.NewBoardState(rules.BoardSizeMedium, rules.BoardSizeMedium)
previousBoardState.Food = append(previousBoardState.Food, []rules.Point{{X: 1, Y: 2}, {X: 3, Y: 4}}...)
previousBoardState.Hazards = append(previousBoardState.Food, []rules.Point{{X: 4, Y: 3}, {X: 2, Y: 1}}...)
previousBoardState.Snakes = append(previousBoardState.Snakes, rules.Snake{
ID: "1",
Body: []rules.Point{{X: 5, Y: 5}, {X: 5, Y: 4}, {X: 5, Y: 3}},
Health: 100,
})
previousBoardState.Turn = 0
passedBoardState := previousBoardState.Clone()
tempBoardState := previousBoardState.Clone()
err := gameMap.UpdateBoard(passedBoardState, testSettings, NewBoardStateEditor(tempBoardState))
require.NoError(t, err, "GameMap.UpdateBoard returned an error")
require.Equal(t, previousBoardState, passedBoardState, "BoardState should not be modified directly by GameMap.UpdateBoard")
})
}
}
func pickSize(meta Metadata) Dimensions {
// For unlimited, we can pick any size
if meta.BoardSizes.IsUnlimited() {
return Dimensions{Width: 11, Height: 11}
}
// For fixed, just pick the first supported size
return meta.BoardSizes[0]
}