formatter function moved out for flow/legibility
This commit is contained in:
parent
84982699bf
commit
e8e5521371
1 changed files with 27 additions and 26 deletions
|
|
@ -1,19 +1,5 @@
|
|||
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
|
||||
|
|
@ -39,6 +25,21 @@ export const knightMoves = (origin, destination) => {
|
|||
}
|
||||
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
|
||||
let current;
|
||||
const queue = [originSquare];
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue