ktane-solver
Version:
A library made to solve KTaNE modules
211 lines (184 loc) • 5.18 kB
JavaScript
const MAZE_A = [
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[]
]
const MAZE_B = [
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[]
]
const MAZE_C = [
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[]
]
const MAZE_D = [
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[]
]
const MAZE_E = [
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[]
]
const MAZE_F = [
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[]
]
const MAZE_G = [
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[]
]
const MAZE_H = [
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[]
]
const MAZE_I = [
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[]
]
let maze = []
let path = []
class Maze {
static solve(ind, start, end) {
maze = _choose_maze(ind)
maze[(end[1] - 1) * 2][(end[0] - 1) * 2] = 2
_find((start[0] - 1 ) * 2, (start[1] - 1) * 2)
path = path.filter((element, index) => {
return index % 2 === 0;
})
return path.reverse()
}
}
function _find(col, row, visited = new Set) {
const cellId = row * 11 + col;
if (!maze[row] || !maze[row][col] || visited.has(cellId)) {
return;
}
visited.add(cellId);
if (maze[row][col] == 2) {
return [[col, row]];
}
for (const [addcol, addrow] of [[0, 1],[1, 0],[0, -1],[-1, 0]]) {
const found = _find(col+addcol, row+addrow, visited);
if (found) {
if (addcol == 1 && addrow == 0) path.push("RIGHT")
else if (addcol == 0 && addrow == 1) path.push("DOWN")
else if (addcol == -1 && addrow == 0) path.push("LEFT")
else if (addcol == 0 && addrow == -1) path.push("UP")
return [[col, row], ...found];
}
}
}
function _choose_maze(ind) {
switch (ind.join("")) {
case "12":
case "63":
return MAZE_A
case "52":
case "24":
return MAZE_B
case "44":
case "64":
return MAZE_C
case "11":
case "14":
return MAZE_D
case "46":
case "53":
return MAZE_E
case "51":
case "35":
return MAZE_F
case "21":
case "26":
return MAZE_G
case "41":
case "24":
return MAZE_H
case "15":
case "32":
return MAZE_I
default:
throw "Unexpected indicator position, got " + ind
}
}
export default Maze