-
Notifications
You must be signed in to change notification settings - Fork 3
/
mazeSolver.js
198 lines (165 loc) · 6.1 KB
/
mazeSolver.js
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/**
* Maze solver - solves a two-dimensional maze using brute force
*/
'use strict';
module.exports = function() {
var legend = {
wall: '#',
path: '.',
startPoint: 'A',
endPoint: 'B',
traveledPath: '*'
};
//get map statistics from data including the physical map, start point, end point, and size
function getMap(data) {
var line;
var lines = data.split('\n');
var map = {};
var totalSize = 0;
for (var y = 0; y < lines.length; y++) {
//ignore characters that aren't whitespace or in the legend
lines[y] = line = lines[y].replace(new RegExp('[^' + '\\' + legend.wall + '\\' + legend.path + '\\' + legend.startPoint + '\\' + legend.endPoint + '\\s]'), '');
totalSize += line.length;
//discover the start and end points
for (var x = 0; x < line.length; x++) {
if (line[x] === legend.startPoint) {
map.startPoint = {
x: x,
y: y
};
} else if (line[x] === legend.endPoint) {
map.endPoint = {
x: x,
y: y
};
}
}
}
//map stats
map.maze = lines;
map.totalSize = totalSize;
if (map.startPoint && map.endPoint) {
return map;
}
throw Error('Maze must have a start point (' + legend.startPoint + ') and end point (' + legend.endPoint + ')');
}
//get point from history array
function getPointFromHistory(history, point) {
var xy = point.x + ',' + point.y;
return history[xy];
}
//set point in history array
function setPointInHistory(history, point) {
var xy = point.x + ',' + point.y;
point.traveled = 1;
history[xy] = point;
}
//find available moves from the current point, exclude points we've visited this iteration
function findAvailableMoves(maze, excludePoints, currentPoint) {
var availableMoves = [];
var trialMoves = [];
var move;
var historyPoint;
//try up
trialMoves.push({
x: currentPoint.x,
y: currentPoint.y - 1
});
//try down
trialMoves.push({
x: currentPoint.x,
y: currentPoint.y + 1
});
//try right
trialMoves.push({
x: currentPoint.x + 1,
y: currentPoint.y
});
//try left
trialMoves.push({
x: currentPoint.x - 1,
y: currentPoint.y
});
for (var i = 0; i < trialMoves.length; i++) {
move = trialMoves[i];
if (maze[move.y] && maze[move.y][move.x]) {
historyPoint = getPointFromHistory(excludePoints, move);
if (!historyPoint) {
if (maze[move.y][move.x] === legend.path || maze[move.y][move.x] === legend.endPoint) {
availableMoves.push(move);
}
}
}
}
return availableMoves;
}
return {
/**
* Maze legend
* @type {object}
*/
legend: legend,
/**
* Set a new legend
* @param {object} legend
*/
setLegend: function(legend)
{
this.legend = legend;
},
/**
* Provide solution for two-dimensional maze using brute force
* @param {[string]} map file
* @return {[object]} solution with symbol denoting the success path
*/
getSolution: function(data) {
var map = getMap(data);
var history = [];
var availableMoves;
var currentTravels = 0;
var startTime = new Date();
//sort by preferred moves (preferred moves include the least visited points)
function sortByMostPreferredMoves(moveA, moveB) {
var pointA = getPointFromHistory(history, moveA);
var pointB = getPointFromHistory(history, moveB);
return (pointA ? pointA.traveled : 0) - (pointB ? pointB.traveled : 0);
}
do {
//represents an attempt iteration
var currentPoint = map.startPoint;
var currentPath = {};
do {
//keep going until we run out of available moves in this iteration
if (map.maze[currentPoint.y][currentPoint.x] !== legend.endPoint) {
setPointInHistory(currentPath, currentPoint);
//keep track of our history so we know to evade points that we've visited before
var point = getPointFromHistory(history, currentPoint);
if (!point) {
setPointInHistory(history, currentPoint);
} else {
currentTravels = ++point.traveled;
}
//find an available move to make from the current point
availableMoves = findAvailableMoves(map.maze, currentPath, currentPoint);
if (availableMoves.length > 0) {
availableMoves.sort(sortByMostPreferredMoves);
currentPoint = availableMoves[0];
}
} else {
//return the solution
return {
maze: map.maze,
size: map.size,
elapsedTime: new Date() - startTime,
solutionPath: currentPath
};
}
}
while (availableMoves && availableMoves.length > 0);
}
while (currentTravels < map.totalSize);
//no solution is available to get to the end point, error out
throw Error('No solution available!');
}
};
}();