2
재귀 적 역 추적 알고리즘을 사용하여 미로 생성 프로그램을 작성하려고합니다. 나는 예제 코드를 this article에서 가져 왔고 더 많거나 적게는 자바 스크립트로 번역했습니다. 그러나 모든 행이 생성 된 그리드에서 동일하기 때문에 작동하지 않는 것 같습니다.미로 생성을위한 재귀 역 추적
나는 이런 종류의 것에 대해 아무것도 모른다. 그리고 나는 여기에서 붙어있다. 아무도 내가 뭘 잘못하고 있는지 알 수 있니?
편집는 : jsfiddle
// initialize the grid
var grid = []
, cells = []
// duplicate to avoid overriding
, w = width
, h = height
while (w--) cells.push(0)
while (h--) grid.push(cells)
var N = 1
, S = 2
, E = 4
, W = 8
, dirs = ['N', 'E', 'S', 'W']
, dirsValue = { N: N, E: E, S: S, W: W }
, DX = { E: 1, W: -1, N: 0, S: 0 }
, DY = { E: 0, W: 0, N: -1, S: 1 }
, OPPOSITE = { E: W, W: E, N: S, S: N }
function carve_passages_from(cx, cy, grid) {
var directions = shuffle(dirs)
directions.forEach(function(direction) {
var nx = cx + DX[direction]
, ny = cy + DY[direction]
if (ny >= 0 && ny <= (grid.length - 1) && nx >= 0
&& nx <= (grid.length - 1) && grid[ny][nx] === 0) {
grid[cy][cx] += dirsValue[direction]
grid[ny][nx] += OPPOSITE[direction]
carve_passages_from(nx, ny, grid)
}
})
}
carve_passages_from(0, 0, grid)
return grid
아 맞아. 고마워. – romainberger