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
56
rand.go
Normal file
56
rand.go
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
package rules
|
||||
|
||||
import "math/rand"
|
||||
|
||||
type Rand interface {
|
||||
Intn(n int) int
|
||||
Shuffle(n int, swap func(i, j int))
|
||||
}
|
||||
|
||||
// A Rand implementation that just uses the global math/rand generator.
|
||||
var GlobalRand globalRand
|
||||
|
||||
type globalRand struct{}
|
||||
|
||||
func (globalRand) Intn(n int) int {
|
||||
return rand.Intn(n)
|
||||
}
|
||||
|
||||
func (globalRand) Shuffle(n int, swap func(i, j int)) {
|
||||
rand.Shuffle(n, swap)
|
||||
}
|
||||
|
||||
// For testing purposes
|
||||
|
||||
// A Rand implementation that always returns the minimum value for any method.
|
||||
var MinRand minRand
|
||||
|
||||
type minRand struct{}
|
||||
|
||||
func (minRand) Intn(n int) int {
|
||||
return 0
|
||||
}
|
||||
|
||||
func (minRand) Shuffle(n int, swap func(i, j int)) {
|
||||
// no shuffling
|
||||
}
|
||||
|
||||
// A Rand implementation that always returns the maximum value for any method.
|
||||
var MaxRand maxRand
|
||||
|
||||
type maxRand struct{}
|
||||
|
||||
func (maxRand) Intn(n int) int {
|
||||
return n - 1
|
||||
}
|
||||
|
||||
func (maxRand) Shuffle(n int, swap func(i, j int)) {
|
||||
// rotate by one element so every element is moved
|
||||
if n < 2 {
|
||||
return
|
||||
}
|
||||
for i := 0; i < n-2; i++ {
|
||||
swap(i, i+1)
|
||||
}
|
||||
swap(n-2, n-1)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue