Update pathfinding.component.ts

Created new normal case
This commit is contained in:
2026-02-05 08:35:34 +01:00
parent 38bf7edd53
commit f385a154d5

View File

@@ -114,10 +114,9 @@ export class PathfindingComponent implements AfterViewInit {
} }
// Default after size changes: pick one consistent scenario // Default after size changes: pick one consistent scenario
this.edgeCase(); this.normalCase();
} }
// Scenarios (buttons)
normalCase(): void { normalCase(): void {
this.stopAnimations(); this.stopAnimations();
this.initializeGrid(true, 'normal'); this.initializeGrid(true, 'normal');
@@ -250,7 +249,7 @@ export class PathfindingComponent implements AfterViewInit {
this.endNode.isEnd = true; this.endNode.isEnd = true;
if (withWalls) { if (withWalls) {
this.placeDefaultDiagonalWall(); this.placeDefaultDiagonalWall(scenario);
} }
} }
@@ -300,7 +299,37 @@ export class PathfindingComponent implements AfterViewInit {
}; };
} }
private placeDefaultDiagonalWall(): void { private placeDefaultDiagonalWall(scenario: 'normal' | 'edge'): void {
if (scenario === 'edge') {
this.createDiagonalWall();
}
else if (scenario === 'normal') {
this.createVerticalWall();
}
}
private createVerticalWall() {
const height = this.gridRows;
const startCol = Math.floor(this.gridCols / 2);
for (let i = 5; i < (height - 5); i++) {
const row = i;
if (!this.isValidPosition(row, startCol)) {
continue;
}
const node = this.grid[row][startCol];
if (node.isStart || node.isEnd) {
continue;
}
node.isWall = true;
}
}
private createDiagonalWall() {
// Diagonal-ish wall; avoids start/end // Diagonal-ish wall; avoids start/end
const len = Math.min(this.gridRows, this.gridCols); const len = Math.min(this.gridRows, this.gridCols);
const startCol = Math.floor((this.gridCols - len) / 2); const startCol = Math.floor((this.gridCols - len) / 2);
@@ -322,7 +351,7 @@ export class PathfindingComponent implements AfterViewInit {
} }
} }
// Path state // Path state
private clearPath(): void { private clearPath(): void {
for (let row = 0; row < this.gridRows; row++) { for (let row = 0; row < this.gridRows; row++) {
for (let col = 0; col < this.gridCols; col++) { for (let col = 0; col < this.gridCols; col++) {