Improv
This commit is contained in:
6
main.go
6
main.go
@@ -8,9 +8,9 @@ import (
|
||||
"test-maze/mazer"
|
||||
)
|
||||
|
||||
const SIZE_X = 48
|
||||
const SIZE_Y = 48
|
||||
const CELL_SCALE = 10
|
||||
const SIZE_X = 61
|
||||
const SIZE_Y = 61
|
||||
const CELL_SCALE = 1
|
||||
|
||||
func main() {
|
||||
matrix := mazer.GenerateMaze(SIZE_X, SIZE_Y)
|
||||
|
||||
110
mazer/maze.go
110
mazer/maze.go
@@ -14,83 +14,77 @@ func GenerateMaze(width int, height int) [][]int {
|
||||
matrix[y] = cells[rowStart : rowStart+width]
|
||||
}
|
||||
|
||||
// Keep a full black border: path only exists inside [1..width-2] x [1..height-2].
|
||||
if width < 3 || height < 3 {
|
||||
return matrix
|
||||
}
|
||||
|
||||
startX, startY := 1, 1
|
||||
endX, endY := width-2, height-2
|
||||
matrix[startY][startX] = 1
|
||||
|
||||
parent := make([]int, width*height)
|
||||
for i := range parent {
|
||||
parent[i] = -1
|
||||
}
|
||||
visited := make([]bool, width*height)
|
||||
stackX := make([]int, 1, width*height/2)
|
||||
stackY := make([]int, 1, width*height/2)
|
||||
stackX[0], stackY[0] = startX, startY
|
||||
|
||||
dx := [4]int{0, -1, 1, 0}
|
||||
dy := [4]int{1, 0, 0, -1}
|
||||
for len(stackX) > 0 {
|
||||
last := len(stackX) - 1
|
||||
x, y := stackX[last], stackY[last]
|
||||
dirs := shuffledDirections()
|
||||
|
||||
type node struct {
|
||||
x int
|
||||
y int
|
||||
dirs [4]uint8
|
||||
next int
|
||||
carved := false
|
||||
for _, d := range dirs {
|
||||
dx, dy := 0, 0
|
||||
switch d {
|
||||
case 0:
|
||||
dy = -2
|
||||
case 1:
|
||||
dx = 2
|
||||
case 2:
|
||||
dy = 2
|
||||
default:
|
||||
dx = -2
|
||||
}
|
||||
|
||||
stack := make([]node, 1, (width-2)*(height-2))
|
||||
stack[0] = node{x: startX, y: startY, dirs: shuffledDirections()}
|
||||
startIdx := startY*width + startX
|
||||
endIdx := endY*width + endX
|
||||
visited[startIdx] = true
|
||||
|
||||
found := false
|
||||
for len(stack) > 0 {
|
||||
last := len(stack) - 1
|
||||
top := &stack[last]
|
||||
|
||||
if top.x == endX && top.y == endY {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
|
||||
if top.next == 4 {
|
||||
stack = stack[:last]
|
||||
continue
|
||||
}
|
||||
|
||||
d := top.dirs[top.next]
|
||||
top.next++
|
||||
|
||||
nx := top.x + dx[d]
|
||||
ny := top.y + dy[d]
|
||||
nx, ny := x+dx, y+dy
|
||||
if nx <= 0 || nx >= width-1 || ny <= 0 || ny >= height-1 {
|
||||
continue
|
||||
}
|
||||
|
||||
nIdx := ny*width + nx
|
||||
if visited[nIdx] {
|
||||
if matrix[ny][nx] == 1 {
|
||||
continue
|
||||
}
|
||||
|
||||
visited[nIdx] = true
|
||||
parent[nIdx] = top.y*width + top.x
|
||||
stack = append(stack, node{x: nx, y: ny, dirs: shuffledDirections()})
|
||||
}
|
||||
|
||||
if !found {
|
||||
matrix[startY][startX] = 1
|
||||
return matrix
|
||||
}
|
||||
|
||||
for idx := endIdx; idx != -1; idx = parent[idx] {
|
||||
x := idx % width
|
||||
y := idx / width
|
||||
matrix[y][x] = 1
|
||||
if idx == startIdx {
|
||||
matrix[y+dy/2][x+dx/2] = 1
|
||||
matrix[ny][nx] = 1
|
||||
stackX = append(stackX, nx)
|
||||
stackY = append(stackY, ny)
|
||||
carved = true
|
||||
break
|
||||
}
|
||||
|
||||
if !carved {
|
||||
stackX = stackX[:last]
|
||||
stackY = stackY[:last]
|
||||
}
|
||||
}
|
||||
|
||||
// Entrance on top border at a random connected X.
|
||||
topChoices := make([]int, 0, width/2)
|
||||
for x := 1; x < width-1; x++ {
|
||||
if matrix[1][x] == 1 {
|
||||
topChoices = append(topChoices, x)
|
||||
}
|
||||
}
|
||||
entranceX := topChoices[rand.IntN(len(topChoices))]
|
||||
matrix[0][entranceX] = 1
|
||||
|
||||
// Exit on bottom border at a random connected X.
|
||||
bottomChoices := make([]int, 0, width/2)
|
||||
for x := 1; x < width-1; x++ {
|
||||
if matrix[height-2][x] == 1 {
|
||||
bottomChoices = append(bottomChoices, x)
|
||||
}
|
||||
}
|
||||
exitX := bottomChoices[rand.IntN(len(bottomChoices))]
|
||||
matrix[height-1][exitX] = 1
|
||||
|
||||
return matrix
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user