define src.game.maze.Spec and .Seed

master
bel 2023-07-16 13:58:58 -06:00
parent 4f9a11f4f9
commit 95fe38699b
3 changed files with 58 additions and 0 deletions

21
src/game/maze/spec.go Normal file
View File

@ -0,0 +1,21 @@
package maze
type Spec struct {
Size struct {
W uint16
H uint16
}
Seed uint32
}
func NewSpec(w, h uint16) Spec {
return NewSpecWithSeed(w, h, NewSeed())
}
func NewSpecWithSeed(w, h uint16, seed uint32) Spec {
result := Spec{}
result.Size.W = w
result.Size.H = h
result.Seed = seed
return result
}

View File

@ -0,0 +1,18 @@
package maze_test
import (
"testing"
"gogs.inhome.blapointe.com/imicromouse/src/game/maze"
)
func TestNewSpec(t *testing.T) {
spec := maze.NewSpec(8, 6)
if got := spec.Size.W; got != 8 {
t.Error(got)
} else if got := spec.Size.H; got != 6 {
t.Error(got)
} else if got := spec.Seed; got == 0 {
t.Error(got)
}
}

19
src/game/maze/speed.go Normal file
View File

@ -0,0 +1,19 @@
package maze
import (
"fmt"
"hash/crc32"
"time"
)
func NewSeed() uint32 {
return NewSeedAt(time.Now())
}
func NewSeedAt(t time.Time) uint32 {
n := t.UnixNano() / int64(time.Millisecond)
b := fmt.Sprintf("%b", n)
hash := crc32.NewIEEE()
hash.Write([]byte(b))
return hash.Sum32()
}