formatter function moved out for flow/legibility

This commit is contained in:
ak 2023-07-17 13:52:47 -07:00
parent 84982699bf
commit e8e5521371

View file

@ -1,21 +1,7 @@
import { gameBoard } from "./gameBoard.js"; import { gameBoard } from "./gameBoard.js";
export const knightMoves = (origin, destination) => { // formatter for final output
// make board - 8 x 8 const format = (moves) => {
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 // fix the output array by traversing provided array of BFS search results
// get last square (the destination) // get last square (the destination)
let square = moves[moves.length - 1]; let square = moves[moves.length - 1];
@ -38,7 +24,22 @@ export const knightMoves = (origin, destination) => {
output = `${output} [${arr[i]}]\n`; output = `${output} [${arr[i]}]\n`;
} }
return console.log(output); return console.log(output);
}; };
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];
// initialize looping variables // initialize looping variables
let current; let current;
const queue = [originSquare]; const queue = [originSquare];