HTTP Server just to serve the madness
This commit is contained in:
22
maze/maze.go
22
maze/maze.go
@@ -26,6 +26,18 @@ var (
|
||||
// The algorithm is randomized depth-first carving with one entrance on the top border
|
||||
// and one exit on the bottom border.
|
||||
func Generate(width, height int) (Grid, error) {
|
||||
return generate(width, height, rand.IntN)
|
||||
}
|
||||
|
||||
// GenerateWithSeed creates a maze grid with deterministic randomness.
|
||||
//
|
||||
// The same width, height, and seed always produce the same maze.
|
||||
func GenerateWithSeed(width, height int, seed uint64) (Grid, error) {
|
||||
rng := rand.New(rand.NewPCG(seed, seed^0x9e3779b97f4a7c15))
|
||||
return generate(width, height, rng.IntN)
|
||||
}
|
||||
|
||||
func generate(width, height int, intN func(int) int) (Grid, error) {
|
||||
if width <= 0 || height <= 0 {
|
||||
return nil, ErrInvalidDimensions
|
||||
}
|
||||
@@ -51,7 +63,7 @@ func Generate(width, height int) (Grid, error) {
|
||||
for len(stackX) > 0 {
|
||||
last := len(stackX) - 1
|
||||
x, y := stackX[last], stackY[last]
|
||||
dirs := shuffledDirections()
|
||||
dirs := shuffledDirections(intN)
|
||||
|
||||
carved := false
|
||||
for _, d := range dirs {
|
||||
@@ -85,7 +97,7 @@ func Generate(width, height int) (Grid, error) {
|
||||
}
|
||||
}
|
||||
if len(topChoices) > 0 {
|
||||
entranceX := topChoices[rand.IntN(len(topChoices))]
|
||||
entranceX := topChoices[intN(len(topChoices))]
|
||||
grid[0][entranceX] = 1
|
||||
}
|
||||
|
||||
@@ -96,7 +108,7 @@ func Generate(width, height int) (Grid, error) {
|
||||
}
|
||||
}
|
||||
if len(bottomChoices) > 0 {
|
||||
exitX := bottomChoices[rand.IntN(len(bottomChoices))]
|
||||
exitX := bottomChoices[intN(len(bottomChoices))]
|
||||
grid[height-1][exitX] = 1
|
||||
}
|
||||
|
||||
@@ -227,10 +239,10 @@ func directionDelta(direction uint8) (dx, dy int) {
|
||||
}
|
||||
}
|
||||
|
||||
func shuffledDirections() [4]uint8 {
|
||||
func shuffledDirections(intN func(int) int) [4]uint8 {
|
||||
dirs := [4]uint8{0, 1, 2, 3}
|
||||
for i := 3; i > 0; i-- {
|
||||
j := rand.IntN(i + 1)
|
||||
j := intN(i + 1)
|
||||
dirs[i], dirs[j] = dirs[j], dirs[i]
|
||||
}
|
||||
return dirs
|
||||
|
||||
Reference in New Issue
Block a user