Visualize the classic N-Queens problem using AI search algorithms (Hill Climbing, Simulated Annealing, Local Beam Search) in real time with Pygame.
-
๐ N-Queens Problem: Place one queen per row/column with zero conflicts.
-
๐ Search Algorithms:
- Hill Climbing with random restarts
- Simulated Annealing with customizable temperature schedule
- Local Beam Search with user-defined beam width
-
โฑ๏ธ Performance Metrics: Iterations, restarts, and solve time printed in console.
-
๐จ Graphical Board: Pygame-based animation of algorithm progress.
-
โ๏ธ Configurable: Board size, algorithm parameters, timer interval.
- Language: Python 3.7+
- Graphics: Pygame
- Computation: NumPy
-
Clone the repository
git clone https://github.com/asadsandhu/n-queens-ai.git cd n-queens-visualizer -
Create & activate virtual environment (recommended)
python3 -m venv venv source venv/bin/activate # macOS/Linux venv\\Scripts\\activate # Windows
-
Install dependencies
pip install -r requirements.txt
python n_queens_visualizer.py-
Select Algorithm: Uncomment your choice in
main():# hill_climbing(initial_board) simulated_annealing(initial_board, initial_temp=100.0, cooling_rate=0.95) # local_beam_search(k, initial_states)
-
Adjust Parameters: Modify
BOARD_SIZE,initial_temp,cooling_rate, or beam widthkat top of script. -
Watch: The board updates every 500ms showing queen placements and conflict counts.
-
BOARD_SIZE: Change number of queens/rows (modify related constants).
-
Timer Interval: Adjust
pygame.time.set_timer(pygame.USEREVENT, interval_ms)for speed. -
Algorithm Params:
initial_temp,cooling_ratefor simulated annealingk,initial_statescount for local beam search
n-queens-visualizer/
โโโ README.md # Project overview & instructions
โโโ n_queens_visualizer.py # Main visualization & AI logic
โโโ queen.png # Queen sprite image
โโโ requirements.txt # pygame, numpy
โโโ .gitignore # __pycache__, venv/
โโโ assets/
โโโ (optional) GIFs/screenshots
- โญ Star the repo
- ๐ด Fork
- ๐ก Create a feature branch
- ๐ Commit & push
- ๐ Open a Pull Request
Please follow PEP 8 style and include examples/tests for new algorithms.
Distributed under the MIT License. See LICENSE for details.