A beautiful, interactive web-based N-Queens puzzle solver with a modern UI. This application allows users to manually place queens on a chessboard and then solve the remaining puzzle using an intelligent backtracking algorithm.
- 🎯 Interactive Chess Board: Click on squares to place or remove queens
- 🧠 Smart Solver: Continues from your current board state instead of starting over
- 📏 Customizable Board Size: Support for boards from 1×1 to 12×12
- 🎨 Modern UI: Beautiful gradient design with smooth animations
- 📱 Responsive Design: Works on desktop and mobile devices
- ⚡ Real-time Validation: Prevents invalid queen placements
- 🔄 Partial Solutions: Solve from any valid starting position
The N-Queens problem is a classic computer science puzzle where you must place N queens on an N×N chessboard such that no two queens attack each other. Queens can attack horizontally, vertically, and diagonally.
This solver uses a backtracking algorithm that:
- Validates your current queen placements
- Continues solving from your partial solution
- Finds a complete solution or reports if none exists
n-queens-solver/
├── main.py # Core N-Queens solving logic
├── app.py # FastAPI backend server
├── index.html # Frontend web interface
└── README.md # This file
- Python 3.7 or higher
- pip (Python package installer)
-
Clone or download the project files
-
Install required dependencies:
pip install fastapi uvicorn
-
Start the server:
uvicorn app:app --reload --host 0.0.0.0 --port 8000
-
Open your browser and navigate to:
http://localhost:8000
- Board Size: Enter a number (1-12) and click "Create Board"
- Place Queens: Click on any square to place or remove a queen
- Clear Board: Remove all queens and start fresh
- Solve: Find a solution continuing from your current setup
- Manual Placement: Click squares to place queens manually
- Validation: The system prevents invalid placements (queens that would attack each other)
- Partial Solving: Place some queens yourself, then let the algorithm complete the puzzle
- Visual Feedback: Hover effects and status messages guide your interaction
- Create an 8×8 board
- Place a queen at position (0,1) by clicking that square
- Place another queen at (2,3)
- Click "Solve" to complete the remaining positions
- The solver will find a valid solution that includes your placed queens
The FastAPI backend provides these endpoints:
GET /- Serves the web interfacePOST /create_board?size={n}- Create a new n×n boardGET /get_board- Get current board statePOST /set_queen?x={row}&y={col}- Place a queen (if valid)POST /remove_queen?x={row}&y={col}- Remove a queenPOST /solve- Solve from current board state
The solver uses an enhanced backtracking algorithm:
- Validation: Checks if current board state is valid
- Row-by-row solving: Places one queen per row
- Constraint checking: Ensures no queens attack each other
- Backtracking: Undoes moves when no valid placement exists
- Optimization: Skips rows that already contain user-placed queens
The is_safe() method checks:
- Column conflicts (vertical attacks)
- Diagonal conflicts (both diagonals)
- Row conflicts (handled by algorithm structure)
- Pure HTML/CSS/JavaScript
- Modern CSS with gradients and animations
- Responsive design with media queries
- Fetch API for backend communication
- FastAPI framework for REST API
- CORS enabled for development
- Global board state management
- Error handling with HTTP status codes
- Object-oriented Board class
- Recursive backtracking solver
- Board validation methods
- Multiple solution finding capability
- Chrome 60+
- Firefox 55+
- Safari 12+
- Edge 79+
- Board sizes above 12×12 may have performance issues
- No solution exists for 2×2 and 3×3 boards (this is mathematically correct)
- Single-threaded solving (one request at a time)
Feel free to contribute improvements:
- Fork the project
- Create a feature branch
- Make your changes
- Test thoroughly
- Submit a pull request
- Multiple solution display
- Solution animation
- Undo/Redo functionality
- Save/Load board states
- Performance optimizations for larger boards
- Solution step-by-step visualization
This project is open source and available under the MIT License.
Server won't start:
- Ensure Python 3.7+ is installed
- Install FastAPI:
pip install fastapi uvicorn - Check if port 8000 is available
Frontend won't load:
- Verify the server is running on
http://localhost:8000 - Check browser console for JavaScript errors
- Ensure CORS is properly configured
Solver seems slow:
- Large board sizes (10+) naturally take longer
- Complex partial solutions may require more computation
- Consider reducing board size for faster results
Queens disappear after clicking solve:
- This is normal - the solver replaces your partial solution with a complete one
- Use "Clear Board" to start fresh if needed
For questions, suggestions, or bug reports, please create an issue in the project repository.