loopGridMaze = {
random1
const cellList = [...Array(gridSize.w * gridSize.h).keys()]
shuffle(cellList)
const ltg = new simpleListToGenerator(cellList)
const maze = new GridMaze(gridSize.w, gridSize.h)
const debugCell = new Map()
return genmaze({
cells: ltg.generator(),
getNeighbors(cell){
return maze.getNeighbors(cell)
},
neighborFilter(cell, neighbors) {
return neighbors.map(list=>{
let result = list.map(it=> false)
if (list.length > 1) {
if (Math.random() < loopChance) {
let pairs = []
for (let i = 0; i < list.length-1; i++){
for (let j = i; j < list.length; j++){
const isVerticalPair = Math.abs(list[i]-list[j]) === gridSize.w * 2
const isHorizontalPair = Math.abs(list[i]-list[j]) === 2
if (isVerticalPair || isHorizontalPair){
pairs.push([i, j])
}
}
}
if (pairs.length > 0) {
const amount = 1
for (let i = 0; i < amount; i++) {
const idx = Math.floor(Math.random()*pairs.length)
const p = pairs[idx]
result[p[0]] = true
result[p[1]] = true
pairs.splice(idx, 1)
}
debugCell.set(cell, 'darkred')
} else {
const diagonal = list[1]+list[0]-cell
if(!(maze.isConnected(list[0], diagonal) && maze.isConnected(list[1], diagonal))){
result[0] = true
result[1] = true
debugCell.set(cell, 'darkblue')
} else {
result[Math.floor(Math.random()*result.length)] = true
debugCell.set(cell, 'teal')
}
}
} else {
result[Math.floor(Math.random()*result.length)] = true
debugCell.set(cell, 'darkgreen')
}
} else {
result[Math.floor(Math.random()*result.length)] = true
debugCell.set(cell, 'gray')
}
return result
})
},
setConnected(a, b){
maze.setConnected(a, b)
},
getState(){
return {
cells: cellList.slice(0, ltg.index),
openEdges: [...maze.connected.values()],
debugCell,
}
}
})
}