js-knights-travails/src/knightMoves.js
2023-07-17 13:50:38 -07:00

75 lines
2.8 KiB
JavaScript

import { gameBoard } from "./gameBoard.js";
export const knightMoves = (origin, destination) => {
// make board - 8 x 8
const board = gameBoard(8);
// initialize origin square -- origin[0] is x, origin[1] is y
const originSquare = board.find(origin[0], origin[1]);
// exit out if the origin square is off the game board
if (!originSquare) return console.log("No such starting square possible!");
// otherwise, continue initialization
originSquare.visited = true;
// set search iteration level to '0'
originSquare.level = 0;
// possible move combinations for x and y - linked at index values - moves clockwise
const possibleX = [1, 2, 2, 1, -1, -2, -2, -1];
const possibleY = [2, 1, -1, -2, -2, -1, 1, 2];
// formatter for final output
const format = (moves) => {
// fix the output array by traversing provided array of BFS search results
// get last square (the destination)
let square = moves[moves.length - 1];
// loop through previous squares until there are no more previous squares (the origin)
// get array of path - made changeable for later
let arr = [];
while (square != null) {
arr.unshift(square);
square = square.prev;
}
// format the output string in the Odin-approved way
let output = `=> You made it in ${
arr.length - 1
} moves! Here's your path:\n`;
// loop through returned array of square objects to return array of positions and append them to the output
for (let i = 0; i < arr.length; i++) {
// change each array index to be the position of the current square object
arr[i] = new Array(arr[i].x, arr[i].y);
// then add it to the output string
output = `${output} [${arr[i]}]\n`;
}
return console.log(output);
};
// initialize looping variables
let current;
const queue = [originSquare];
const searchMoves = [];
// while there are squares in the queue
while (queue.length > 0) {
// get first queued square and remove it from queue
current = queue.shift();
// add to search moves array
searchMoves.push(current);
// if values match, return
if (current.x === destination[0] && current.y === destination[1])
return format(searchMoves);
// otherwise - eight possible moves can be made
for (let i = 0; i < 8; i++) {
// assigns values to possible move combinations at index
const x = current.x + possibleX[i];
const y = current.y + possibleY[i];
// then checks them
const found = board.find(x, y);
if (found) {
if (!found.visited) {
// initializes square and pushes to queue
found.visited = true;
found.level = current.level + 1;
found.prev = current;
queue.push(found);
}
}
}
}
// if not found, return infinity
return Infinity;
};