Skip to content

Commit f92f7b6

Browse files
author
Rory Sullivan
committed
Refactoring
1 parent f599dba commit f92f7b6

File tree

13 files changed

+120
-129
lines changed

13 files changed

+120
-129
lines changed
Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
1+
import GraphNode from './GraphNode';
2+
13
/**
24
* Makes a graph of nodes from our grid matrix. Grid matrix values of 0
35
* represent a normal node, values of 1 represent a wall.
46
*/
5-
class Graph extends Array {
7+
export default class Graph extends Array {
68
constructor(grid) {
79
super();
810
for (let i = 0; i < grid.length; i += 1) {
911
const row = grid[i];
1012
const graphRow = [];
1113
for (let j = 0; j < row.length; j += 1) {
1214
const a = row[j];
13-
const node = new Node(i, j, this);
15+
const node = new GraphNode(i, j, this);
1416

1517
if (a === 1) {
1618
node.isWall = true;
@@ -31,26 +33,3 @@ class Graph extends Array {
3133
}
3234
}
3335
}
34-
35-
/**
36-
* Calculates the distance between two spaces on our hexagonal grid.
37-
*/
38-
function hexDistanceBetween(x, y) {
39-
const dis1 = Math.abs(x[1] - y[1]);
40-
41-
let penalty;
42-
43-
if (
44-
(x[1] % 2 === 0 && y[1] % 2 === 1 && x[0] < y[0]) ||
45-
(x[1] % 2 === 1 && y[1] % 2 === 0 && x[0] > y[0])
46-
) {
47-
penalty = 1;
48-
} else {
49-
penalty = 0;
50-
}
51-
const dis2 = Math.abs(x[0] - y[0]) + Math.floor(dis1 / 2) + penalty;
52-
53-
const dis = Math.max(dis1, dis2);
54-
55-
return dis;
56-
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* A class for creating nodes in a graph.
33
*/
4-
class Node {
4+
export default class GraphNode {
55
constructor(row, col, parentGraph) {
66
this.graph = parentGraph; // The graph containing this node
77
this.row = row;

algorithms/aStar.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
/* global Graph, hexDistanceBetween */
1+
import Graph from './Graph';
2+
import hexDistanceBetween from './hexDistanceBetween';
23

3-
class AStar {
4+
export default class AStar {
45
constructor(grid, startNode, endNode) {
56
this.graph = new Graph(grid);
67
this.endNode = endNode;

algorithms/biasedAStar.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
/* global Graph, hexDistanceBetween */
1+
import Graph from './Graph';
2+
import hexDistanceBetween from './hexDistanceBetween';
23

3-
class BiasedAStar {
4+
export default class BiasedAStar {
45
constructor(grid, startNode, endNode) {
56
this.graph = new Graph(grid);
67
this.endNode = endNode;

algorithms/dijkstra.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* array representing the start node position and an array representing the end
1212
* node position.
1313
*/
14-
class Dijkstra {
14+
export default class Dijkstra {
1515
constructor(grid, startNode, endNode) {
1616
this.graph = new Graph(grid);
1717
this.path = [];

algorithms/hexDistanceBetween.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Calculates the distance between two spaces on our hexagonal grid.
3+
*/
4+
export default function hexDistanceBetween(x, y) {
5+
const dis1 = Math.abs(x[1] - y[1]);
6+
7+
let penalty;
8+
9+
if (
10+
(x[1] % 2 === 0 && y[1] % 2 === 1 && x[0] < y[0]) ||
11+
(x[1] % 2 === 1 && y[1] % 2 === 0 && x[0] > y[0])
12+
) {
13+
penalty = 1;
14+
} else {
15+
penalty = 0;
16+
}
17+
const dis2 = Math.abs(x[0] - y[0]) + Math.floor(dis1 / 2) + penalty;
18+
19+
const dis = Math.max(dis1, dis2);
20+
21+
return dis;
22+
}

index.html

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,8 @@
1111
<link rel="icon" type="image/png" sizes="32x32" href="./icons/favicon-32x32.png" />
1212
<link rel="icon" type="image/png" sizes="16x16" href="./icons/favicon-16x16.png" />
1313

14-
<script src="./script.js"></script>
15-
<script src="./algorithms/general.js"></script>
16-
<script src="./algorithms/dijkstra.js"></script>
17-
<script src="./algorithms/aStar.js"></script>
18-
<script src="./algorithms/biasedAStar.js"></script>
19-
<script src="./mazes.js"></script>
14+
<script src="./main.js"></script>
15+
2016
</head>
2117

2218
<body onload="setup();">

script.js renamed to main.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
1-
/**
2-
* TODO: Description
3-
*/
1+
import Dijkstra from './algorithms/Dijkstra';
2+
import AStar from './algorithms/AStar';
3+
import BiasedAStar from './algorithms/BiasedAStar';
44

5-
/* global Dijkstra, AStar, BiasedAStar */
6-
7-
/**
8-
* Global variables
9-
* */
105
// Hexagons
116
const hexSize = 16; // Should be divisible by 2
127
const hexYDim = Math.floor(hexSize * (Math.sqrt(3) / 2)); // y-axis dimension

mazes.js

Lines changed: 0 additions & 84 deletions
This file was deleted.

mazes/horizontalMaze.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
export default function horizontalMaze() {
2+
let startJ = 0;
3+
let endJ = n - 1;
4+
5+
for (let i = 1; i < m; i += 2) {
6+
for (let j = startJ; j < endJ; j += 1) {
7+
if (
8+
!(i === start[0] && j === start[1]) &&
9+
!(i === end[0] && j === end[1])
10+
) {
11+
grid[i][j] = 1;
12+
hexes[i][j].fill = 'black';
13+
}
14+
}
15+
startJ = (startJ + 1) % 2;
16+
endJ = n - 1 + startJ;
17+
}
18+
animate();
19+
}

0 commit comments

Comments
 (0)