|
| 1 | +import { UndirectedGraph } from "graphology"; |
| 2 | +import { bidirectional } from 'graphology-shortest-path/unweighted'; |
| 3 | +import { IGraph } from "./IGraph"; |
| 4 | + |
| 5 | +// Similar to PentaHexGraph but the center node only has 5 edges |
| 6 | +export class PentaHexGraph implements IGraph { |
| 7 | + public readonly size: number; |
| 8 | + public graph: UndirectedGraph; |
| 9 | + |
| 10 | + constructor(size: number) { |
| 11 | + if (size < 2) throw new Error(`PentaHexGraph cannot be less than size 2`); |
| 12 | + this.size = size; |
| 13 | + this.graph = this.buildGraph(); |
| 14 | + } |
| 15 | + |
| 16 | + public coords2algebraic(x: number, y: number): string { |
| 17 | + if (y < 0 || y > this.size) throw new Error(`The x coord must be a circle inside the graph`); |
| 18 | + if (y == 0) return "0-0"; |
| 19 | + let trueX = x; |
| 20 | + do { |
| 21 | + trueX += 5*y; |
| 22 | + } while (trueX < 0); |
| 23 | + return y + "-" + (trueX%(5*y)); |
| 24 | + } |
| 25 | + |
| 26 | + public algebraic2coords(cell: string): [number, number] { |
| 27 | + const pair: string[] = cell.split("-"); |
| 28 | + const y = Number(pair[0]); |
| 29 | + const x = Number(pair[1]); |
| 30 | + if (y === undefined || isNaN(y) || x === undefined || isNaN(x)) { |
| 31 | + throw new Error(`The algebraic coords cannot be parsed`); |
| 32 | + } |
| 33 | + return [x,y]; |
| 34 | + } |
| 35 | + |
| 36 | + private buildGraph(): UndirectedGraph { |
| 37 | + const graph = new UndirectedGraph(); |
| 38 | + for (let y = 0; y < this.size; y++) { |
| 39 | + if (y == 0) { |
| 40 | + graph.addNode(this.coords2algebraic(0,0)); |
| 41 | + } |
| 42 | + for (let x = 0; x < 5*y; x++) { |
| 43 | + graph.addNode(this.coords2algebraic(x,y)); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + for (let y = 0; y < this.size; y++) { |
| 48 | + if (y == 0) { |
| 49 | + const cell = this.coords2algebraic(0,0); |
| 50 | + graph.addEdge(cell, this.coords2algebraic(0,1)); |
| 51 | + graph.addEdge(cell, this.coords2algebraic(1,1)); |
| 52 | + graph.addEdge(cell, this.coords2algebraic(2,1)); |
| 53 | + graph.addEdge(cell, this.coords2algebraic(3,1)); |
| 54 | + graph.addEdge(cell, this.coords2algebraic(4,1)); |
| 55 | + } |
| 56 | + let counter = -1; |
| 57 | + for (let x = 0; x < 5*y; x++) { |
| 58 | + const cell = this.coords2algebraic(x,y); |
| 59 | + graph.addEdge(cell, this.coords2algebraic(x+1, y)); |
| 60 | + if (y+1 >= this.size) continue; |
| 61 | + if (x % y == 0) { |
| 62 | + //Three edges |
| 63 | + graph.addEdge(cell, this.coords2algebraic(x+counter++, y+1)); |
| 64 | + graph.addEdge(cell, this.coords2algebraic(x+counter++, y+1)); |
| 65 | + graph.addEdge(cell, this.coords2algebraic(x+counter--, y+1)); |
| 66 | + } else { |
| 67 | + //Two edges |
| 68 | + graph.addEdge(cell, this.coords2algebraic(x+counter++, y+1)); |
| 69 | + graph.addEdge(cell, this.coords2algebraic(x+counter--, y+1)); |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + return graph; |
| 74 | + } |
| 75 | + |
| 76 | + public listCells(ordered = false): string[] | string[][] { |
| 77 | + if (!ordered) { |
| 78 | + return this.graph.nodes(); |
| 79 | + } else { |
| 80 | + const result: string[][] = []; |
| 81 | + for (let y = 0; y < this.size; y++) { |
| 82 | + const node: string[] = []; |
| 83 | + if (y == 0) { |
| 84 | + node.push(this.coords2algebraic(0,0)); |
| 85 | + } |
| 86 | + for (let x = 0; x < 5*y; x++) { |
| 87 | + node.push(this.coords2algebraic(x,y)); |
| 88 | + } |
| 89 | + result.push(node); |
| 90 | + } |
| 91 | + return result; |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + public neighbours(node: string): string[] { |
| 96 | + return this.graph.neighbors(node); |
| 97 | + } |
| 98 | + |
| 99 | + public path(from: string, to: string): string[] | null { |
| 100 | + return bidirectional(this.graph, from, to); |
| 101 | + } |
| 102 | +} |
0 commit comments