Java
Java copied to clipboard
Refactor KnightsTour class for improved design and readability
This Pull Request updates the KnightsTour implementation with a cleaner, modern, and more maintainable structure while preserving the original algorithmic behavior.
Key Improvements 1. Design & Architecture
Converted static and shared state into an object-oriented structure
Removed implicit padded board design and added support for any N × N board size
Added constructor-based initialization for clarity and reuse
2. Readability & Maintainability
Improved naming, documentation, and logical separation of responsibilities
Simplified move validation and board handling logic
Structured the code to be easier for learners and contributors to understand
3. Algorithmic & Performance Enhancements
Retains Warnsdorff’s heuristic but implemented more cleanly and consistently
Eliminated unnecessary boundary padding and redundant branching
Improved recursive traversal efficiency and reduced avoidable checks
4. API & Usage Improvements
solve() now returns a boolean result, removing dependency on global state
Added getBoard() method for retrieving the final tour result
Implementation is now reusable, testable, and suitable for integration into applications or visualizers
Reason for Update
The previous implementation was functional but relied heavily on static state, implicit padding, and less maintainable patterns. This updated version provides a clearer, more flexible, and educational approach while preserving the original problem-solving intent.