The knightMoves
function implements an algorithm to find the shortest path a knight can take on a chessboard from an origin square to a destination square using breadth-first tree traversal.
function knightMoves([originX, originY], [destinationX, destinationY])
originX
,originY
: Coordinates of the knight's starting position.destinationX
,destinationY
: Coordinates of the intended destination for the knight.
- Validate input coordinates to ensure they are within the chessboard bounds (0 to 7).
- Initialize a shortest paths array to store the result.
- Create a root node representing the knight's starting position.
- Use breadth-first tree traversal to explore possible moves until the destination is reached.
- Trace the path from the destination back to the origin.
- Output the shortest path and the number of moves.
knightMoves([3, 3], [0, 0]);
knightMoves([0, 0], [7, 7]);
The Node class represents a node in the tree with information about the knight's position and its parent-child relationships.
class Node {
constructor([x, y], parent) {
this.value = [x, y];
this.parent = parent;
this.child0 = null;
this.child1 = null;
this.child2 = null;
this.child3 = null;
this.child4 = null;
this.child5 = null;
this.child6 = null;
this.child7 = null;
}
}
The function logs the result to the console, including the number of moves and the path in JSON format.
You made it in 7 moves! Here's your path:
[0, 0]
[2, 1]
[1, 3]
[3, 2]
[5, 3]
[6, 5]
[7, 7]
Contributions are welcome! Feel free to submit bug reports, feature requests, or pull requests. Please follow the code style and documentation guidelines to ensure a smooth integration of your contributions.
This project is licensed under the MIT License.