define src.game.maze.Spec and .Seed
parent
4f9a11f4f9
commit
95fe38699b
|
|
@ -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
|
||||
}
|
||||
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
@ -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()
|
||||
}
|
||||
Loading…
Reference in New Issue