functional version
This commit is contained in:
parent
da797d7719
commit
84982699bf
6 changed files with 2086 additions and 0 deletions
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
node_modules
|
||||
.eslint*
|
||||
.prettier*
|
||||
1945
package-lock.json
generated
Normal file
1945
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load diff
22
package.json
Normal file
22
package.json
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
{
|
||||
"name": "js-knights-travails",
|
||||
"version": "1.0.0",
|
||||
"description": "Knights Travails made in ES6 JS",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "ssh://_gitea@git.alexanderk.net:3022/ak/js-knights-travails.git"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"devDependencies": {
|
||||
"eslint": "^8.44.0",
|
||||
"eslint-config-prettier": "^8.8.0",
|
||||
"prettier": "^3.0.0"
|
||||
},
|
||||
"type" : "module"
|
||||
}
|
||||
32
src/gameBoard.js
Normal file
32
src/gameBoard.js
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
import { square } from "./square.js";
|
||||
|
||||
// game board containing squares
|
||||
export const gameBoard = (size) => {
|
||||
// makes board
|
||||
const make = () => {
|
||||
const arr = [];
|
||||
for (let x = 0; x <= size; x++) {
|
||||
const yarr = [];
|
||||
for (let y = 0; y <= size; y++) {
|
||||
yarr.push(square(x, y));
|
||||
}
|
||||
arr.push(yarr);
|
||||
}
|
||||
return arr.flat();
|
||||
};
|
||||
// assigns it to variable
|
||||
const board = make();
|
||||
// finds specified square
|
||||
const find = (x, y) => {
|
||||
for (let i = 0; i < board.length; i++) {
|
||||
if (board[i].x === x && board[i].y === y) {
|
||||
return board[i];
|
||||
}
|
||||
}
|
||||
return null;
|
||||
};
|
||||
return {
|
||||
board,
|
||||
find,
|
||||
};
|
||||
};
|
||||
75
src/knightMoves.js
Normal file
75
src/knightMoves.js
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
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;
|
||||
};
|
||||
9
src/square.js
Normal file
9
src/square.js
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
// simple square with a visited boolean and "search level" for operations
|
||||
export const square = (x, y, visited = false, level = null, prev = null) => {
|
||||
return {
|
||||
x,
|
||||
y,
|
||||
visited,
|
||||
level,
|
||||
};
|
||||
};
|
||||
Loading…
Add table
Reference in a new issue