My current assignment involves writing a program to determine the shortest path between a knight and a target tile on a chessboard. The task requires me to represent the knight's possible moves in a tree structure and utilize recursion to find paths to the target location. I've encountered challenges with the recursive call in the `getMoves` function, particularly regarding avoiding repeating visited tiles to prevent an infinite loop.
While browsing solutions on StackOverflow, I noticed various methods involving graphs and simpler implementations. However, I have opted to use trees and Breadth-First Search (BFS) as the primary approach due to my ongoing study focus and the specific requirement of this exercise. The code consists of three classes: `Board`, `Knight`, and `TreeMoves` where the available moves are stored. The BFS algorithm resides within the `TreeMoves`, iterating through each tree level recursively to check for a node matching the target position. Essentially, the process entails generating all possible knight moves, running BFS to identify the target location, and recursively expanding the tree to explore further positions, excluding previously visited ones.
Although I considered creating a more concise version for the community, I'm concerned it might cause confusion at this point. The existing code performs well when the target is within immediate reach of the knight's available moves. However, if the target is slightly moved away from these initial positions, it leads to the infinite loop issue mentioned earlier.
I would appreciate any guidance or hints on identifying potential mistakes in my implementation.
Cheers!
<!-- JavaScript code snippets omitted for conciseness -->