A modern web-based implementation of the classic Chomp game with an intelligent computer opponent. Built with FastAPI backend and vanilla JavaScript frontend.
Chomp is a strategic board game played on a rectangular grid of chocolate squares. The top-left square (0,0) is poisoned, and players take turns removing chocolate squares. When a player removes a square, all squares to the right and below it are also removed. The player who is forced to eat the poison square loses the game!
- Interactive Web Interface: Clean, responsive design with visual feedback
- Intelligent Computer Opponent: Strategic AI that considers board positions and optimal moves
- Turn-Based Gameplay: Clear indication of whose turn it is
- Move History: Undo functionality to take back moves
- Customizable Board Size: Play on any size grid you prefer
- Real-time Game State: Dynamic board updates and game status
- Modal Notifications: User-friendly messages and game over alerts
- Python 3.7+
- pip (Python package manager)
-
Clone or download the project files
# Make sure you have all these files in your project directory: # - main.py # - app.py # - index.html # - styles.css # - script.js
-
Install FastAPI and Uvicorn
pip install fastapi uvicorn
-
Run the server
uvicorn app:app --reload
-
Open your browser Navigate to
http://localhost:8000to start playing!
-
Set Up the Game
- Enter the number of rows and columns for your board
- Click "Start New Game"
-
Make Your Move
- Click on any chocolate square (brown squares marked with 'C')
- Remember: clicking a square removes it and all squares to the right and below
- You cannot click the poison square (red square marked with 'P')
-
Strategic Tips
- Try to leave your opponent in a losing position
- The player who is forced to take the poison square loses
- Think several moves ahead!
-
Game Controls
- Computer Move: Let the AI make a strategic move
- Undo Move: Take back the last move
- New Game: Start over with a new board size
- Quit: Return to the main menu
chomp-solver/
├── main.py # Core game logic and console interface
├── app.py # FastAPI web server and API endpoints
├── index.html # Web interface structure
├── styles.css # Custom styling and responsive design
├── script.js # Frontend game logic and API communication
└── README.md # This file
- FastAPI: Modern web framework for the API
- Game Class: Handles all game logic, moves, and state management
- Strategic AI: Computer opponent with intelligent move selection
- Vanilla JavaScript: No frameworks, pure DOM manipulation
- Tailwind CSS: Utility-first CSS framework for styling
- Responsive Design: Works on desktop and mobile devices
- Real-time Updates: Dynamic board rendering and game state
GET /- Serve the main game interfacePOST /start- Initialize a new gamePOST /remove- Make a move (remove squares)POST /computer_move- Let computer make a movePOST /undo- Undo the last moveGET /get_board_state- Get current game state
- Setup: The game is played on a rectangular grid of chocolate squares
- Poison Square: The top-left square (0,0) is poisoned
- Turn Taking: Players alternate making moves
- Move Mechanics: Removing a square also removes all squares to its right and below
- Winning: The player who forces their opponent to take the poison square wins
- Losing: The player who is left with only the poison square loses
The computer opponent uses a strategic approach:
- Prioritizes moves that leave an odd number of squares
- Avoids creating 1×N or N×1 board shapes (when possible)
- Uses randomization when multiple equally good moves exist
- Considers long-term positioning rather than just immediate gains
You can easily customize the game:
- Board Appearance: Modify
styles.cssto change colors and styling - AI Difficulty: Adjust the computer strategy in
main.py - Game Rules: Extend the
Gameclass for variants - UI Elements: Update
index.htmlandscript.jsfor interface changes
Server won't start?
- Make sure you have FastAPI and Uvicorn installed:
pip install fastapi uvicorn - Check that no other service is using port 8000
Game not loading?
- Ensure all files are in the same directory
- Check the browser console for JavaScript errors
- Verify the server is running at
http://localhost:8000
Moves not working?
- Make sure you're clicking on chocolate squares (not removed or poison squares)
- Check that the game hasn't ended
- Try refreshing the page if the game state seems stuck
This project demonstrates:
- Web API Development with FastAPI
- Game Logic Implementation in Python
- Frontend-Backend Communication via REST API
- Responsive Web Design with CSS
- Strategic AI Programming for game opponents
- State Management in web applications
Feel free to fork this project and make improvements! Some ideas:
- Add multiplayer support
- Implement different AI difficulty levels
- Add sound effects and animations
- Create additional game variants
- Improve mobile responsiveness
This project is open source and available under the MIT License.
Enjoy playing Chomp! 🍫