재귀가 아닌 스택으로 구현 된 재귀 Backtracker 버전을 사용하여 미로를 생성하는 루아 스크립트를 작성합니다. 현재 미로가 땋아 나오고 있으며 내 논리에서 이것이 어디에서 일어나고 있는지 파악할 수 없습니다.내 Lua 미로 작성기가 엮어지고 있습니다
local function buildMazeInternal(x,y,maze)
local stack = {}
local directions = {'North','East','South','West'}
table.insert(stack,{x=x,y=y})
while #stack > 0 do
local index = 1
local nextX = x
local nextY = y
local braid = false
for i = #directions, 2, -1 do -- backwards
local r = calc:Roll(1,i) -- select a random number between 1 and i
directions[i], directions[r] = directions[r], directions[i] -- swap the randomly selected item to position i
end
while index <= #directions and nextX == x and nextY == y do
if directions[index] == 'North' and y > 1 and not maze[y-1][x].Visited then
maze[y][x].North = true
maze[y-1][x].South = true
nextY = y-1
elseif directions[index] == 'East' and x < width and not maze[y][x+1].Visited then
maze[y][x].East = true
maze[y][x+1].West = true
nextX = x+1
elseif directions[index] == 'South' and y < height and not maze[y+1][x].Visited then
maze[y][x].South = true
maze[y+1][x].North = true
nextY = y+1
elseif directions[index] == 'West' and x > 1 and not maze[y][x-1].Visited then
maze[y][x].West = true
maze[y][x-1].East = true
nextX = x-1
else
index = index + 1
end
end
if nextX ~= x or nextY ~= y then
x = nextX
y = nextY
maze[y][x].Visited = true
table.insert(stack,{x=x,y=y})
else
x = stack[#stack].x
y = stack[#stack].y
table.remove(stack)
end
end
end
내가 뭔가를 바라 보는거야 알고 있지만 내가 그것을 잡을 수없는 것 :이 기능은 아래의 2 차원 구조 (테이블의 테이블) 인 미로를 생성하기위한 출발점으로 x와 y에 소요 하위. calc:Roll(1,100)
메서드는 내 응용 프로그램에서 .net 메서드로 롤링 주사위를 시뮬레이트하는 데 사용됩니다.이 경우 1 * 100 양면 다이는 응용 프로그램 외부에서 사용하기 위해 math.Random(1,100)
에 대한 호출로 바꿀 수 있습니다.
"꼰"이란 무엇입니까? – kikito
미로에 막 다른 골목이 있거나 미로를 해결하지 못하는 경로를 의미합니다. – Lance