-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathGraph.tsx
More file actions
48 lines (43 loc) · 1.34 KB
/
Copy pathGraph.tsx
File metadata and controls
48 lines (43 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
export class Coordinate {
public x: number = 0;
public y: number = 0;
constructor (x: number, y: number) {
this.x = x;
this.y = y;
}
toString() {
return `(${this.x}, ${this.y})`;
}
}
export class Graph {
public width: number = 0;
public height: number = 0;
public obstacles: Map<string, boolean> = new Map();
constructor(fileContent: string) {
this.parseGraph(fileContent);
}
private parseGraph(fileContent: string) {
const lines = fileContent.trim().split('\n');
if (lines.length < 4) {
throw new Error('Invalid map file');
}
const height = Number(lines[1].split(" ")[1]);
if (height !== lines.length - 4) {
throw new Error('Invalid map file, check height');
}
this.height = height;
const width = Number(lines[2].split(" ")[1]);
this.width = width;
const graph = lines.slice(4);
for (let y = 0; y < graph.length; y++) {
if (graph[y].length !== width) {
throw new Error('Invalid map file, check width');
}
for (let x = 0; x < this.width; x++) {
if (graph[y][x] !== '.') {
this.obstacles.set((new Coordinate(x, y)).toString(), true);
}
}
}
}
}