A web-based implementation of the classic Tower of Hanoi puzzle featuring both 2D and 3D interactive interfaces. Built with Python FastAPI backend and JavaScript/Three.js frontend.
- Dual Interface Options:
- Classic 2D web interface with click-based gameplay
- Immersive 3D interface with drag-and-drop functionality using Three.js
- Interactive Gameplay: Move rings manually with visual feedback
- Auto-Solver: Watch the computer solve the puzzle step by step
- Game Controls: Start, restart, undo moves, and quit functionality
- Configurable Difficulty: Support for 1-10 rings
- Move Validation: Prevents invalid moves (larger rings on smaller ones)
- Visual Feedback: Clear status messages and animated solutions
The classic web interface features colorful rings stacked on pegs with click-to-move gameplay.
The Three.js interface provides an immersive 3D experience with drag-and-drop controls and orbital camera movement.
- Python 3.7+
- Modern web browser with JavaScript enabled
-
Clone or download the project files
-
Install required Python dependencies:
pip install -r requirements.txt
-
Run the FastAPI server:
uvicorn app:app --reload
-
Open your browser and navigate to:
- 2D Interface:
http://localhost:8000/ - 3D Interface:
http://localhost:8000/three-ui.html
- 2D Interface:
This application includes a one-command deployment script for Google Cloud Platform's Cloud Run service.
- Docker installed locally
- Google Cloud CLI installed and authenticated
- GCP project created and configured
# Configure your GCP project
gcloud config set project YOUR_PROJECT_ID
gcloud auth configure-docker
# Deploy with one command
./deploy.shThe script will:
- Build a Docker container (linux/amd64 architecture for Cloud Run)
- Push to Google Container Registry
- Deploy to Cloud Run in us-west1 region
- Output your live service URL
The deployment is configured to stay within Cloud Run's free tier:
- 2 million requests/month
- 360,000 GB-seconds of memory/month
- 180,000 vCPU-seconds/month
- Max 1 instance to prevent scaling costs
Note: The application will cold start after ~15 minutes of inactivity on the free tier.
tower-of-hanoi/
├── app.py # FastAPI web server and API endpoints
├── main.py # Core game logic and Tower of Hanoi solver
├── index.html # 2D interface HTML
├── three-ui.html # 3D interface HTML
├── script.js # 2D interface JavaScript
├── three-ui.js # 3D interface JavaScript with Three.js
├── styles.css # Shared CSS styles
├── requirements.txt # Python dependencies
├── Dockerfile # Container definition for Cloud Run
├── deploy.sh # Automated deployment script
└── .dockerignore # Files excluded from Docker build
- Enter the number of rings (1-10) in the input field
- Click "Start Game" to begin
- Click on a peg to select it as the source
- Click on another peg to move the top ring from source to destination
- Use game controls: Restart, Undo, Solve, or Quit
- Enter the number of rings and click "Start Game"
- Use mouse/touch to orbit around the 3D scene
- Drag the top ring from any peg to move it
- Drop it on the desired destination peg
- Same game controls available as 2D version
The Tower of Hanoi follows these classic rules:
- Only one ring can be moved at a time
- Only the top ring from a peg can be moved
- A larger ring cannot be placed on top of a smaller ring
- The goal is to move all rings from the leftmost peg to the rightmost peg
POST /start?rings=N- Start a new game with N ringsGET /state- Get current board statePOST /move?src=X&dst=Y- Move ring from peg X to peg YPOST /solve- Get complete solution stepsPOST /undo- Undo the last move
- FastAPI: Modern Python web framework for the REST API
- Game Logic: Recursive solver using the classic three-peg algorithm
- State Management: In-memory game state with move history for undo functionality
- Containerization: Docker support for easy deployment to cloud platforms
- 2D Interface: Vanilla JavaScript with DOM manipulation
- 3D Interface: Three.js WebGL library for 3D rendering
- Responsive Design: CSS flexbox layouts for different screen sizes
- Interactive Controls: Mouse/touch support for both interfaces
- Orbit Controls: Mouse/touch camera controls for 3D navigation
- Drag & Drop: Intuitive ring movement with raycasting
- Dynamic Lighting: Ambient and directional lighting for visual appeal
- Responsive Rendering: Automatic canvas resizing and proper aspect ratios
- 2D Interface: All modern browsers
- 3D Interface: Browsers with WebGL support (Chrome, Firefox, Safari, Edge)
- Recommended maximum of 10 rings for optimal performance
- 3D interface may require hardware acceleration for smooth rendering
- Auto-solve animation speed can be adjusted by modifying the timeout values in the JavaScript files
Feel free to submit issues, feature requests, or pull requests to improve the game. Some potential enhancements:
- Sound effects and music
- Different color themes
- Save/load game states
- Multiplayer functionality
- Mobile-optimized touch controls
This project is open source and available under the MIT License.