* 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
35 lines
860 B
Go
35 lines
860 B
Go
package maps
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/BattlesnakeOfficial/rules"
|
|
)
|
|
|
|
// MapRegistry is a mapping of map names to game maps.
|
|
type MapRegistry map[string]GameMap
|
|
|
|
var globalRegistry = MapRegistry{}
|
|
|
|
// RegisterMap adds a stage to the registry.
|
|
// If a map has already been registered this will panic.
|
|
func (registry MapRegistry) RegisterMap(id string, m GameMap) {
|
|
if _, ok := registry[id]; ok {
|
|
panic(fmt.Sprintf("map '%s' has already been registered", id))
|
|
}
|
|
|
|
registry[id] = m
|
|
}
|
|
|
|
// GetMap returns the map associated with the given ID.
|
|
func (registry MapRegistry) GetMap(id string) (GameMap, error) {
|
|
if m, ok := registry[id]; ok {
|
|
return m, nil
|
|
}
|
|
return nil, rules.ErrorMapNotFound
|
|
}
|
|
|
|
// GetMap returns the map associated with the given ID from the global registry.
|
|
func GetMap(id string) (GameMap, error) {
|
|
return globalRegistry.GetMap(id)
|
|
}
|