This program visualizes and solves the Knight's tour problem in three ways using Java Swing. Check out the Live Demo on Replit.
"A knight's tour is a sequence of moves of a knight on a chessboard such that the knight visits every square exactly once." - Wikipedia
This version enables a user to control the knight to make a full tour based on the automatically generated suggestion and their intuition.
This version implements Warnsdorff's heuristic to complete a full tour automatically starting from each square on the chessboard.
"The knight is moved so that it always proceeds to the square from which the knight will have the fewest onward moves" - Wikipedia
This version implements the heuristic mentioned above, but when there are two or more choices for which the number of onward moves is equal, the program decides what square to choose by looking ahead to those choices.
- Navigate to the build directory
$ cd out/production/knights-tour
-
Run the program
a. Manual version
$ java Main
b. Heuristic-driven version
$ java Heuristic
c. Optimized heuristic-driven version
$ java OptimizedHeuristic
The result that I have after running the three versions of the program:
Version | Number of Full Tours |
---|---|
Manual | (You can try it yourself!) |
Heuristic | 63 |
Optimized | 64 |