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
This commit is contained in:
parent
1c3f434841
commit
dab9178a55
16 changed files with 916 additions and 160 deletions
42
maps/helpers.go
Normal file
42
maps/helpers.go
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
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
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue