(terrain: Terrain, solution?: Graph.PathResult<number>)
| 343 | * Print terrain as ASCII art with path overlay. |
| 344 | */ |
| 345 | const printSolution = (terrain: Terrain, solution?: Graph.PathResult<number>): string => { |
| 346 | // Clone the grid to avoid mutating the original |
| 347 | const grid = terrain.grid.map((line) => line.slice()) |
| 348 | const steps = solution === undefined ? [] : solution.path |
| 349 | |
| 350 | for (const [key, index] of steps.entries()) { |
| 351 | const node = Graph.getNode(terrain.graph, index).pipe( |
| 352 | Option.getOrThrowWith(() => new Error("Path coordinates outside of terrain")) |
| 353 | ) |
| 354 | |
| 355 | const marker = key === 0 ? "S" : key === steps.length - 1 ? "E" : "*" |
| 356 | grid[node.y][node.x] = marker |
| 357 | } |
| 358 | |
| 359 | for (let x = 0; x < terrain.width; x++) { |
| 360 | for (let y = 0; y < terrain.height; y++) { |
| 361 | const character = grid[y][x] |
| 362 | const color = colors.colors.get(character) |
| 363 | if (color !== undefined) { |
| 364 | grid[y][x] = color + character + colors.reset |
| 365 | } |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | return grid.map((line) => line.join("")).join("\n") |
| 370 | } |
| 371 | |
| 372 | /** |
| 373 | * ANSI color codes for terminal output. |
no test coverage detected