-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e36b6af
commit c128e6c
Showing
3 changed files
with
45 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/apps/path-finder/algorithms/maze-generator/side-winder.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { generateGrid } from '../../helpers/grid'; | ||
import { CellType, MazeAlgoProps } from '../../models/interfaces'; | ||
|
||
export async function generateSideWinderMaze({ | ||
rows, | ||
cols, | ||
entry, | ||
exit, | ||
updateGrid, | ||
updateCells, | ||
}: MazeAlgoProps) { | ||
const grid = generateGrid(rows, cols, CellType.wall); | ||
updateGrid(grid); | ||
|
||
const topRowCells = grid[0].map((_, i) => ({ row: 0, col: i })); | ||
await updateCells(grid, topRowCells); | ||
|
||
for (let row = 2; row < rows; row += 2) { | ||
for (let col = 0; col < cols; col += 2) { | ||
const run = [{ row, col }]; | ||
const runCells = [{ row, col }]; | ||
while (col < cols - 2 && Math.random() < 0.5) { | ||
col += 2; | ||
run.push({ row, col }); | ||
runCells.push({ row, col: col - 1 }, { row, col }); | ||
} | ||
|
||
const northCell = run[Math.floor(Math.random() * run.length)]; | ||
runCells.push({ row: northCell.row - 1, col: northCell.col }); | ||
await updateCells(grid, runCells); | ||
} | ||
} | ||
|
||
updateCells(grid, entry, CellType.entry); | ||
updateCells(grid, exit, CellType.exit); | ||
return grid; | ||
} |