Byte-snake-engine/maps/helpers.go
Rob O'Dwyer dab9178a55
DEV 1247: Add a new map generator interface (#71)
* reorganize code

* first draft of map generator interfaces

* add explicit random interface to board helpers

* implement standard map

* rename Generator to GameMap

* allow initializing snakes separately from placing them

* add random number generator to Settings

* updates to GameMap interface

* add helpers for creating and updating BoardState with maps
2022-05-11 08:26:28 -07:00

42 lines
1.2 KiB
Go

package maps
import "github.com/BattlesnakeOfficial/rules"
// SetupBoard is a shortcut for looking up a map by ID and initializing a new board state with it.
func SetupBoard(mapID string, settings rules.Settings, width, height int, snakeIDs []string) (*rules.BoardState, error) {
boardState := rules.NewBoardState(int32(width), int32(height))
rules.InitializeSnakes(boardState, snakeIDs)
gameMap, err := GetMap(mapID)
if err != nil {
return nil, err
}
editor := NewBoardStateEditor(boardState, settings.Rand())
err = gameMap.SetupBoard(boardState, settings, editor)
if err != nil {
return nil, err
}
return boardState, nil
}
// UpdateBoard is a shortcut for looking up a map by ID and updating an existing board state with it.
func UpdateBoard(mapID string, previousBoardState *rules.BoardState, settings rules.Settings) (*rules.BoardState, error) {
gameMap, err := GetMap(mapID)
if err != nil {
return nil, err
}
nextBoardState := previousBoardState.Clone()
editor := NewBoardStateEditor(nextBoardState, settings.Rand())
err = gameMap.SetupBoard(previousBoardState, settings, editor)
if err != nil {
return nil, err
}
return nextBoardState, nil
}