From 93a2c34203bc552b05a23eb4872978261d77d7c6 Mon Sep 17 00:00:00 2001 From: Daniel Legt Date: Wed, 25 Feb 2026 21:50:58 +0200 Subject: [PATCH] Improv --- main.go | 6 +-- mazer/maze.go | 118 ++++++++++++++++++++++++-------------------------- 2 files changed, 59 insertions(+), 65 deletions(-) diff --git a/main.go b/main.go index 212d104..a36c3ad 100644 --- a/main.go +++ b/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) diff --git a/mazer/maze.go b/mazer/maze.go index 71aa67f..dff63c7 100644 --- a/mazer/maze.go +++ b/mazer/maze.go @@ -14,84 +14,78 @@ 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 + nx, ny := x+dx, y+dy + if nx <= 0 || nx >= width-1 || ny <= 0 || ny >= height-1 { + continue + } + if matrix[ny][nx] == 1 { + continue + } - found := false - for len(stack) > 0 { - last := len(stack) - 1 - top := &stack[last] - - if top.x == endX && top.y == endY { - found = true + matrix[y+dy/2][x+dx/2] = 1 + matrix[ny][nx] = 1 + stackX = append(stackX, nx) + stackY = append(stackY, ny) + carved = 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] - if nx <= 0 || nx >= width-1 || ny <= 0 || ny >= height-1 { - continue - } - - nIdx := ny*width + nx - if visited[nIdx] { - 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 { - 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 }