-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboard.js
71 lines (63 loc) · 2.06 KB
/
board.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
import BoardSquare from './boardsquare.js';
export default (() => {
const toIndex = (pos) => ({
x: pos[0],
y: pos[1],
});
const createBoard = (width = 8) => {
const board = Array(width).fill(null)
.map(() => Array(width).fill(null));
return board.map(((y, row) => y.map((_, col) => BoardSquare(toIndex([row, col])))));
};
const withinBounds = (index, bounds = 8) => !!(
index.x < bounds
&& index.x >= 0
&& index.y < bounds
&& index.y >= 0
);
const createMoveTree = (board, root) => {
const STEPS = [
[1, -2], [1, 2],
[2, -1], [2, 1],
[-1, -2], [-1, 2],
[-2, -1], [-2, 1],
];
return STEPS.reduce((tree, move) => {
const thisMove = toIndex([root.x + move[0], root.y + move[1]]);
if (withinBounds(thisMove)) tree.push(board[thisMove.x][thisMove.y]);
return tree;
}, []);
};
const backtrack = (board, origin, path = []) => {
path.push(origin.previous);
if (board[origin.index.x][origin.index.y].distance === 1) return path;
return backtrack(board, board[origin.previous.x][origin.previous.y], path);
};
const BFSRec = (board, queue, target) => {
if (queue.length === 0) return null;
const current = queue.shift();
const adjacent = createMoveTree(board, current.index);
if (JSON.stringify(current.index) === JSON.stringify(target)) {
return backtrack(board, current, [current.index]);
}
adjacent.forEach((node) => {
if (node.distance < current.distance && !node.previous) {
node.distance = current.distance + 1;
node.previous = current.index;
queue.push(node);
}
});
return BFSRec(board, queue, target);
};
const knightMoves = (origin, target) => {
const indexOrigin = toIndex(origin);
const targetOrigin = toIndex(target);
const board = createBoard();
const originNode = board[indexOrigin.x][indexOrigin.y];
originNode.distance += 1;
const queue = [originNode];
const path = BFSRec(board, queue, targetOrigin);
return path;
};
return { knightMoves };
})();