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
35
maps/registry.go
Normal file
35
maps/registry.go
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
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)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue