|
| 1 | +import { Cell, initialiseAllWalls } from '../utils/PathfindingUtils'; |
| 2 | + |
| 3 | +type Maze = Cell[][]; |
| 4 | + |
| 5 | +// Initialize edges as walls |
| 6 | +function initialiseEdges(maze: Maze): void { |
| 7 | + for (let row = 0; row < maze.length; row++) { |
| 8 | + maze[row][maze[row].length - 1].wall = true; |
| 9 | + maze[row][0].wall = true; |
| 10 | + } |
| 11 | + |
| 12 | + for (let col = 0; col < maze[0].length; col++) { |
| 13 | + maze[0][col].wall = true; |
| 14 | + maze[maze.length - 1][col].wall = true; |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +// Add a horizontal wall with a single passage |
| 19 | +function addHorizontalWall(maze: Maze, y: number, xStart: number, xEnd: number, passageX: number): void { |
| 20 | + for (let x = xStart; x <= xEnd; x++) { |
| 21 | + if (x !== passageX) { |
| 22 | + maze[y][x].wall = true; |
| 23 | + } else { |
| 24 | + maze[y][x].wall = false; |
| 25 | + } |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +// Add a vertical wall with a single passage |
| 30 | +function addVerticalWall(maze: Maze, x: number, yStart: number, yEnd: number, passageY: number): void { |
| 31 | + for (let y = yStart; y <= yEnd; y++) { |
| 32 | + if (y !== passageY) { |
| 33 | + maze[y][x].wall = true; |
| 34 | + } else { |
| 35 | + maze[y][x].wall = false; |
| 36 | + } |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +// Recursive function to divide the maze |
| 41 | +function recursiveDivision(maze: Maze, xStart: number, yStart: number, width: number, height: number, chooseOrientation: (width: number, height: number) => boolean): void { |
| 42 | + if (width <= 2 || height <= 2) { |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + const horizontal = chooseOrientation(width, height); |
| 47 | + if (horizontal) { |
| 48 | + const y = Math.floor(randomNumber(yStart, yStart + height - 1) / 2) * 2; |
| 49 | + const passageX = Math.floor(randomNumber(xStart, xStart + width - 1) / 2) * 2 + 1; |
| 50 | + addHorizontalWall(maze, y, xStart, xStart + width - 1, passageX); |
| 51 | + |
| 52 | + recursiveDivision(maze, xStart, yStart, width, y - yStart, chooseOrientation); |
| 53 | + recursiveDivision(maze, xStart, y + 1, width, yStart + height - y - 1, chooseOrientation); |
| 54 | + } else { |
| 55 | + const x = Math.floor(randomNumber(xStart, xStart + width - 1) / 2) * 2; |
| 56 | + const passageY = Math.floor(randomNumber(yStart, yStart + height - 1) / 2) * 2 + 1; |
| 57 | + addVerticalWall(maze, x, yStart, yStart + height - 1, passageY); |
| 58 | + |
| 59 | + recursiveDivision(maze, xStart, yStart, x - xStart, height, chooseOrientation); |
| 60 | + recursiveDivision(maze, x + 1, yStart, xStart + width - x - 1, height, chooseOrientation); |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +// Function to create a biased chooseOrientation function |
| 65 | +function createUseBiasOrientationFunction(bias: number) { |
| 66 | + return function (): boolean { |
| 67 | + return Math.random() > bias; |
| 68 | + }; |
| 69 | +} |
| 70 | + |
| 71 | +function nonBiasedOrientationFunction(width: number, height: number): boolean { |
| 72 | + if (width < height) return true; |
| 73 | + if (height < width) return false; |
| 74 | + return Math.random() < 0.5; |
| 75 | +} |
| 76 | + |
| 77 | +// Generate a random number within a range |
| 78 | +function randomNumber(min: number, max: number): number { |
| 79 | + return Math.floor(Math.random() * (max - min + 1) + min); |
| 80 | +} |
| 81 | + |
| 82 | +// Main function to generate the maze using recursive division |
| 83 | +export function recursiveDivisionMazeGeneration(maze: Maze, startCell: Cell, endCell: Cell, setMaze: (arr: Maze) => void, bias: number = 0.5): void { |
| 84 | + if (maze.length < 3 || maze[0].length < 3) { |
| 85 | + return; |
| 86 | + } |
| 87 | + initialiseAllWalls(maze, false); |
| 88 | + initialiseEdges(maze); |
| 89 | + const chooseOrientation = bias === 0.5 ? nonBiasedOrientationFunction : createUseBiasOrientationFunction(bias); |
| 90 | + recursiveDivision(maze, 1, 1, maze[0].length - 2, maze.length - 2, chooseOrientation); |
| 91 | + maze[startCell.row][startCell.col].wall = false; |
| 92 | + maze[endCell.row][endCell.col].wall = false; |
| 93 | + setMaze([...maze]); |
| 94 | +} |
0 commit comments