MCPcopy Create free account
hub / github.com/Effect-TS/effect / printSolution

Function printSolution

packages/effect/test/Pathfinding.test.ts:345–370  ·  view source on GitHub ↗
(terrain: Terrain, solution?: Graph.PathResult<number>)

Source from the content-addressed store, hash-verified

343 * Print terrain as ASCII art with path overlay.
344 */
345const 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.

Callers 1

assertPathFunction · 0.85

Calls 4

joinMethod · 0.80
pipeMethod · 0.65
getMethod · 0.65
mapMethod · 0.45

Tested by

no test coverage detected