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,19 +1,5 @@
import { gameBoard } from "./gameBoard.js"; 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 // formatter for final output
const format = (moves) => { 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
@ -39,6 +25,21 @@ export const knightMoves = (origin, destination) => {
} }
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];