A React application for visualizing solutions to the classic N-Queens problem with an animated backtracking algorithm.
Live demo: daniel-beachy.github.io/nqueens
The N-Queens puzzle is the problem of placing N chess queens on an N×N chessboard so that no two queens threaten each other. Thus, a solution requires that no two queens share the same row, column, or diagonal.
- Interactive chessboard visualization
- Backtracking algorithm visualization
- Adjustable board size (1-20)
- Three animation speed options:
- Slow (snail)
- Normal (rabbit)
- Fast (rocket)
- Estimated animation time display
- Responsive design that works on various screen sizes
- Set the board size by entering a number between 1-20 in the input field.
- Select your preferred animation speed using the animal icons:
- 🐌 Slow: Good for understanding each step
- 🐇 Normal: Balanced speed
- 🚀 Fast: Quick visualization
- Click "Solve" to start the algorithm visualization.
- Click "Clear" to reset the board and try again.
This application uses a backtracking algorithm to find a solution to the N-Queens problem:
- Start in the top row
- If all queens are placed, return the solution
- Try each column in the current row
- For each column:
- Record the attempt so it can be animated
- If the square is not attacked, place a queen and recursively try the next row
- If that leads to a solution, return the solution
- If not, backtrack and try the next column
- If no column works, return an empty board (no solution exists)
Attacked squares are detected in constant time by keeping three Sets of
occupied columns, positive diagonals (row + col) and negative diagonals
(row - col).
- Node.js and npm
# Clone the repository
git clone https://github.com/daniel-beachy/nqueens.git
cd nqueens
# Install dependencies
npm install
# Start the development server
npm startThe application will be available at http://localhost:3000
npm run buildThis creates an optimized production build in the build folder.
Deployment is automatic. Every push to main triggers
.github/workflows/deploy.yml, which builds the
app and publishes it to GitHub Pages. No manual step is required.
- React
- React Bootstrap
- SCSS (dart-sass)
Created by Daniel Beachy.
This project was bootstrapped with Create React App.