Skip to content

Commit 782304a

Browse files
authored
fix #13 (#14)
1 parent e81599e commit 782304a

3 files changed

Lines changed: 40 additions & 12 deletions

File tree

src/main.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from "./objects/grid.ts";
22
export * from "./finders/finder.enum.ts";
3+
export { transpose } from "./utils/grid.utils.ts";

src/sandbox.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
11
import { Grid } from "./objects/grid.ts";
22
import { FinderEnum } from "./finders/finder.enum.ts";
3-
import { drawLayout } from "./utils/grid.utils.ts";
3+
import { drawLayout, transpose } from "./utils/grid.utils.ts";
44

5-
const layout = [
6-
[1, 1, 1, 1, null],
7-
[1, null, null, 1, 1, 1, 1],
8-
[1, null, 1, 1, 1],
9-
[1, null, 1, 1, 1, 1, 1, 1],
10-
[1, null, 1, 1, 1, 1, 1, 1],
11-
] as number[][];
5+
const original = [
6+
[0, 0, 0, 2, 0, 0, 0, 0, 0],
7+
[0, 0, 1, 1, 1, 1, 0, 1, 1],
8+
[0, 1, 1, 1, 1, 1, 1, 1, 1],
9+
[0, 1, 1, 1, 1, 1, 1, 1, 1],
10+
[2, 1, 1, 1, 1, 1],
11+
[0, 1, 1, 1, 1, 1, 1, 1, 1, 1],
12+
[0, 1, 1, 1, 1, 1, 1, 0, 1, 1],
13+
];
14+
15+
const layout = transpose(original); // (original);
1216

1317
const grid = new Grid(layout);
14-
const start = { x: 6, y: 3 };
15-
const end = { x: 6, y: 1 };
18+
const start = { x: 2, y: 6 };
19+
const end = { x: 5, y: 9 };
1620

1721
console.log(start, "->", end);
1822
const path = grid.findPath(start, end, 1, FinderEnum.JUMP_POINT);
1923
console.log(path);
2024

21-
drawLayout(layout, path);
25+
drawLayout(layout, path, start, end);

src/utils/grid.utils.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,36 @@ export const makeSquare = (layout: number[][]): number[][] => {
1515
return squareLayout;
1616
};
1717

18-
export const drawLayout = (layout: number[][], path: PointInterface[]) => {
18+
export const drawLayout = (
19+
layout: number[][],
20+
path: PointInterface[],
21+
start?: PointInterface,
22+
end?: PointInterface,
23+
) => {
1924
const map: any[] = layout.map((row) => row.map((cell) => (cell ? "." : "#")));
2025

2126
path.forEach((point) => {
2227
map[point.y][point.x] = "P";
2328
});
2429

30+
if (start) map[start.y][start.x] = "S";
31+
if (end) map[end.y][end.x] = "X";
32+
2533
const mapString = map.map((row) => row.join(" ")).join("\n");
2634
console.log(mapString);
2735
};
36+
37+
export const transpose = (matrix: number[][]) => {
38+
const maxCols = Math.max(...matrix.map((row) => row.length));
39+
const transposed: number[][] = Array.from({ length: maxCols }, () =>
40+
Array(matrix.length).fill(null),
41+
);
42+
43+
for (let i = 0; i < matrix.length; i++) {
44+
for (let j = 0; j < matrix[i].length; j++) {
45+
transposed[j][i] = matrix[i][j];
46+
}
47+
}
48+
49+
return transposed;
50+
};

0 commit comments

Comments
 (0)