@hayro_o7/labyrinth 0.0.4 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import type { Graph } from './types';
|
|
2
|
-
export declare function generateLabyrinth(width: number, height: number): Graph;
|
|
2
|
+
export declare function generateLabyrinth(width: number, height: number, wallRemovalPercent?: number): Graph;
|
|
@@ -5,7 +5,7 @@ function parseKey(key) {
|
|
|
5
5
|
const [x, y] = key.split(',').map(Number);
|
|
6
6
|
return { x, y };
|
|
7
7
|
}
|
|
8
|
-
export function generateLabyrinth(width, height) {
|
|
8
|
+
export function generateLabyrinth(width, height, wallRemovalPercent = 25) {
|
|
9
9
|
const cells = [];
|
|
10
10
|
for (let y = 0; y < height; y++) {
|
|
11
11
|
cells[y] = [];
|
|
@@ -40,6 +40,10 @@ export function generateLabyrinth(width, height) {
|
|
|
40
40
|
stack.pop();
|
|
41
41
|
}
|
|
42
42
|
}
|
|
43
|
+
// Remove additional walls randomly to create multiple paths
|
|
44
|
+
if (wallRemovalPercent > 0) {
|
|
45
|
+
removeRandomWalls(cells, width, height, wallRemovalPercent);
|
|
46
|
+
}
|
|
43
47
|
return cellsToGraph(cells, width, height);
|
|
44
48
|
}
|
|
45
49
|
function getUnvisitedNeighbors(cell, cells, width, height) {
|
|
@@ -75,6 +79,50 @@ function removeWall(cell1, cell2) {
|
|
|
75
79
|
cell2.walls.bottom = false;
|
|
76
80
|
}
|
|
77
81
|
}
|
|
82
|
+
function removeRandomWalls(cells, width, height, removalPercent) {
|
|
83
|
+
const allWalls = [];
|
|
84
|
+
// Collect all existing walls
|
|
85
|
+
for (let y = 0; y < height; y++) {
|
|
86
|
+
for (let x = 0; x < width; x++) {
|
|
87
|
+
const cell = cells[y][x];
|
|
88
|
+
if (cell.walls.top && y > 0)
|
|
89
|
+
allWalls.push({ cell, direction: 'top' });
|
|
90
|
+
if (cell.walls.right && x < width - 1)
|
|
91
|
+
allWalls.push({ cell, direction: 'right' });
|
|
92
|
+
if (cell.walls.bottom && y < height - 1)
|
|
93
|
+
allWalls.push({ cell, direction: 'bottom' });
|
|
94
|
+
if (cell.walls.left && x > 0)
|
|
95
|
+
allWalls.push({ cell, direction: 'left' });
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
// Calculate how many walls to remove
|
|
99
|
+
const wallsToRemove = Math.floor(allWalls.length * (removalPercent / 100));
|
|
100
|
+
// Shuffle and remove random walls
|
|
101
|
+
for (let i = 0; i < wallsToRemove; i++) {
|
|
102
|
+
const randomIndex = Math.floor(Math.random() * allWalls.length);
|
|
103
|
+
const { cell, direction } = allWalls[randomIndex];
|
|
104
|
+
// Remove wall from both sides
|
|
105
|
+
const { x, y } = cell;
|
|
106
|
+
if (direction === 'top' && y > 0) {
|
|
107
|
+
cell.walls.top = false;
|
|
108
|
+
cells[y - 1][x].walls.bottom = false;
|
|
109
|
+
}
|
|
110
|
+
else if (direction === 'right' && x < width - 1) {
|
|
111
|
+
cell.walls.right = false;
|
|
112
|
+
cells[y][x + 1].walls.left = false;
|
|
113
|
+
}
|
|
114
|
+
else if (direction === 'bottom' && y < height - 1) {
|
|
115
|
+
cell.walls.bottom = false;
|
|
116
|
+
cells[y + 1][x].walls.top = false;
|
|
117
|
+
}
|
|
118
|
+
else if (direction === 'left' && x > 0) {
|
|
119
|
+
cell.walls.left = false;
|
|
120
|
+
cells[y][x - 1].walls.right = false;
|
|
121
|
+
}
|
|
122
|
+
// Remove this wall from the array to avoid removing it twice
|
|
123
|
+
allWalls.splice(randomIndex, 1);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
78
126
|
function cellsToGraph(cells, width, height) {
|
|
79
127
|
const nodes = new Map();
|
|
80
128
|
for (let y = 0; y < height; y++) {
|