Skip to content

Commit

Permalink
Fix typo and typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
peoplenarthax committed Dec 31, 2020
1 parent cdf57b1 commit 8f583aa
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 39 deletions.
4 changes: 2 additions & 2 deletions __tests__/minimumJumpBFS.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Graph } from '../src/objects/graph';

import {smallGrid, testCasesSmallGrid} from "../__test-data__/small-grid";
import {testCasesBigGrid} from "../__test-data__/big-grid";
import {smallGrid, testCasesSmallGrid} from "./test-data/small-grid";
import {testCasesBigGrid} from "./test-data/big-grid";

describe('test minimumJumpBFS', () => {
const testCases = [
Expand Down
2 changes: 1 addition & 1 deletion __tests__/pathFinding.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Grid } from '../src';

import {smallGrid, testCasesSmallGrid} from "../__test-data__/small-grid";
import {smallGrid, testCasesSmallGrid} from "./test-data/small-grid";

describe('test path finding', () => {
const testCases = [
Expand Down
26 changes: 13 additions & 13 deletions __test-data__/big-grid.ts → __tests__/test-data/big-grid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,19 @@ const grid_B = [
]

export const testCasesBigGrid = [
{
grid: grid_B,
startPoint: { x: 15, y: 20 },
endPoint: { x: 13, y: 20 },
maxJumpCost: 4,
path: [
{ x: 15, y: 20 },
{ x: 15, y: 11 },
{ x: 10, y: 11 },
{ x: 10, y: 20 },
{ x: 13, y: 20 },
],
},
// {
// grid: grid_B,
// startPoint: { x: 15, y: 20 },
// endPoint: { x: 13, y: 20 },
// maxJumpCost: 4,
// path: [
// { x: 15, y: 20 },
// { x: 15, y: 11 },
// { x: 10, y: 11 },
// { x: 10, y: 20 },
// { x: 13, y: 20 },
// ],
// },
{
grid: grid_B,
startPoint: { x: 13, y: 20 },
Expand Down
File renamed without changes.
3 changes: 3 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
testMatch: [ "**/?(*.)+(test).ts" ]
};
4 changes: 2 additions & 2 deletions src/finders/minimumJumpBFS.enum.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PointInterface } from "../objects/point/point.interface"

export type Neightbours = { N?:PointInterface, E?:PointInterface, S?:PointInterface, W?:PointInterface }
export type NeightbourGraph = Readonly<Array<Array<Neightbours | null>>>
export type Neighbours = { N?:PointInterface, E?:PointInterface, S?:PointInterface, W?:PointInterface }
export type NeighbourGraph = Readonly<Array<Array<Neighbours | null>>>
34 changes: 17 additions & 17 deletions src/finders/minimumJumpBFS.finder.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
import {NeightbourGraph} from "./minimumJumpBFS.enum";
import {NeighbourGraph} from "./minimumJumpBFS.enum";
import {PointInterface} from "../objects/point/point.interface";

const getChildren = (graph: NeightbourGraph, node: PointInterface, addedNodes: boolean[][]) => {
const neightboors = graph[node.y][node.x]
const getChildren = (graph: NeighbourGraph, node: PointInterface, addedNodes: boolean[][]) => {
const neighbours = graph[node.y][node.x]
let children = [] as PointInterface[]
if (neightboors === null) return children
if (neighbours === null) return children

const possibleDirections = Object.keys(neightboors)
const possibleDirections = Object.keys(neighbours)

// For each existing neightboor, we iterate in the same direction until find a wall
// For each existing neighbour, we iterate in the same direction until find a wall
possibleDirections.forEach((direction) => {
// First case
let newNeightbour = neightboors[direction] as PointInterface
if (newNeightbour === null) return
let newNeighbour = neighbours[direction] as PointInterface
if (newNeighbour === null) return

if (!addedNodes[newNeightbour.y][newNeightbour.x]) {
children.push(newNeightbour)
addedNodes[newNeightbour.y][newNeightbour.x] = true
if (!addedNodes[newNeighbour.y][newNeighbour.x]) {
children.push(newNeighbour)
addedNodes[newNeighbour.y][newNeighbour.x] = true
}

// The rest
while (graph[newNeightbour.y][newNeightbour.x]![direction]) {
newNeightbour = graph[newNeightbour.y][newNeightbour.x]![direction]
if (!addedNodes[newNeightbour.y][newNeightbour.x]) {
children.push(newNeightbour)
addedNodes[newNeightbour.y][newNeightbour.x] = true
while (graph[newNeighbour.y][newNeighbour.x]![direction]) {
newNeighbour = graph[newNeighbour.y][newNeighbour.x]![direction]
if (!addedNodes[newNeighbour.y][newNeighbour.x]) {
children.push(newNeighbour)
addedNodes[newNeighbour.y][newNeighbour.x] = true
}
}
})

return children
}

type Graph = { graph: NeightbourGraph, width: number, height: number }
type Graph = { graph: NeighbourGraph, width: number, height: number }

// BFS using a directed graph
export const minimumJumps =({graph, width, height}: Graph, {x: startX, y: startY}: PointInterface, {x: endX, y: endY}: PointInterface) => {
Expand Down
8 changes: 4 additions & 4 deletions src/objects/graph.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {NeightbourGraph, Neightbours} from "../finders/minimumJumpBFS.enum";
import {NeighbourGraph, Neighbours} from "../finders/minimumJumpBFS.enum";
import {minimumJumps} from "../finders/minimumJumpBFS.finder";
import {PointInterface} from "./point/point.interface";

export class Graph {
public height: number
public width: number
public graph: NeightbourGraph
public graph: NeighbourGraph

constructor(raw: number[][], jumpCost: number) {
if (raw.length === 0 || raw[0].length === 0 ) {
Expand All @@ -27,12 +27,12 @@ export class Graph {
return minimumJumps(this, startPoint, endPoint)
}

private transformToGrid(raw: number[][], jumpCost: number) : NeightbourGraph {
private transformToGrid(raw: number[][], jumpCost: number) : NeighbourGraph {
return raw.map((row, indexY) => {
return row.map((val, indexX) => {
if (val === 0) return null

let neightbours = {} as Neightbours
let neightbours = {} as Neighbours

if (indexY - 1 >= 0 && indexY - 1 < this.height ) {
const neightbourVal = raw[indexY - 1][indexX]
Expand Down

0 comments on commit 8f583aa

Please sign in to comment.