A modern web-based implementation of the classic 2048 puzzle game with an intelligent AI assistant to help you make optimal moves. The game features a sleek, responsive UI and a FastAPI backend with sophisticated Monte Carlo simulation-based AI.
- Customizable Board: Choose any board size (2x2 to 10x10)
- Flexible Win Conditions: Set win tiles from 64 to 8192
- Undo Functionality: Take back moves when you make mistakes
- Game State Management: Restart or start new games anytime
- Monte Carlo Simulations: AI runs 50 random simulations per move
- Intelligent Move Selection: Chooses moves based on statistical outcomes
- Performance Feedback: Shows AI thinking process and move rationale
- Modern Design: Beautiful gradient backgrounds and smooth animations
- Responsive Layout: Works on desktop, tablet, and mobile devices
- Keyboard Support: Use WASD or arrow keys for quick gameplay
- Visual Feedback: Color-coded tiles and status notifications
- Accessibility: Semantic HTML and proper contrast ratios
- Python 3.7+
- pip (Python package installer)
-
Clone or download the project files:
# Make sure you have these files: # - app.py (FastAPI backend) # - main.py (Game logic) # - index.html (Web interface) # - styles.css (Styling) # - script.js (Frontend logic)
-
Install dependencies:
pip install fastapi uvicorn
-
Run the server:
uvicorn app:app --reload
-
Open your browser and navigate to:
http://localhost:8000
- When you first open the game, you'll see a setup modal
- Choose your preferred:
- Height: Board height (2-10 cells)
- Width: Board width (2-10 cells)
- Win Tile: Target tile value (64-8192)
- Click "Start Game" to begin
- Manual Moves:
- Click the directional buttons (↑ ← ↓ →)
- Use keyboard: WASD or arrow keys
- AI Assistance: Click "LET AI MAKE MOVE" for optimal moves
- Game Actions: Use Undo, Restart, or New Game as needed
- Combine tiles with the same number by moving them together
- Each move spawns a new tile (2 or 4)
- Reach the target tile to win!
- Game ends when no moves are possible
The AI assistant uses a Monte Carlo simulation approach:
- Move Evaluation: For each possible move (up, down, left, right)
- Simulation: Runs 50 random games from that position
- Scoring: Evaluates outcomes based on:
- Win probability (reaching target tile)
- Average tile values achieved
- Game longevity
- Selection: Chooses the move with the highest expected score
This approach balances immediate gains with long-term strategic positioning.
2048-ai-assistant/
├── app.py # FastAPI web server
├── main.py # Core game logic and AI
├── index.html # Main web interface
├── styles.css # UI styling and animations
├── script.js # Frontend game controller
└── README.md # This file
- FastAPI: Modern, fast web framework for Python
- Game Class: Manages game state and user interactions
- Board Class: Handles game logic, moves, and validation
- AI Class: Implements Monte Carlo tree search for move optimization
- Vanilla JavaScript: No frameworks, pure JS for maximum compatibility
- CSS Grid: Responsive board layout that adapts to any size
- Fetch API: Clean REST API communication with backend
- Event-driven: Keyboard and mouse input handling
The game automatically adapts to different screen sizes:
- Desktop: Side-by-side board and controls layout
- Tablet: Stacked layout with optimized button sizes
- Mobile: Full-width design with touch-friendly controls
Edit the tile color classes in styles.css:
.board-cell.tile-2 { background: #eee4da; color: #776e65; }
.board-cell.tile-4 { background: #ede0c8; color: #776e65; }
/* Add more tile colors */Modify the simulation count in main.py:
def get_best_move(self, simulations=50): # Change this numberUpdate the dropdown options in index.html:
<option value="16384">16384</option>Game board doesn't load:
- Check browser console for JavaScript errors
- Ensure FastAPI server is running on port 8000
- Verify all files are in the same directory
AI moves are slow:
- Reduce simulation count in
main.py - Check server performance and memory usage
Keyboard controls don't work:
- Click on the game area to focus
- Check browser console for event listener errors
- Chrome: ✅ Full support
- Firefox: ✅ Full support
- Safari: ✅ Full support
- Edge: ✅ Full support
- IE: ❌ Not supported (lacks modern JavaScript features)
The FastAPI backend provides these endpoints:
GET /- Serve the main HTML pageGET /styles.css- Serve CSS stylesheetGET /script.js- Serve JavaScript codePOST /start_new_game- Initialize new game with settingsPOST /restart_game- Restart with same settingsPOST /make_user_move- Process user movePOST /make_ai_move- Let AI make optimal movePOST /undo- Undo last move
Feel free to contribute improvements:
- Performance: Optimize AI algorithms or UI rendering
- Features: Add new game modes or AI strategies
- Design: Improve visual design or add animations
- Accessibility: Enhance keyboard navigation or screen reader support
This project is open source and available under the MIT License.
- Original 2048 game by Gabriele Cirulli
- Inspired by classic sliding puzzle games
- Built with modern web technologies for optimal performance
Enjoy playing 2048 with your AI assistant! 🎮🤖