Skip to content

Latest commit

 

History

6 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Chomp Solver

A modern web-based implementation of the classic Chomp game with an intelligent computer opponent. Built with FastAPI backend and vanilla JavaScript frontend.

🎮 What is Chomp?

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!

✨ Features

  • 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

🚀 Quick Start

Prerequisites

  • Python 3.7+
  • pip (Python package manager)

Installation

  1. 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
  2. Install FastAPI and Uvicorn

    pip install fastapi uvicorn
  3. Run the server

    uvicorn app:app --reload
  4. Open your browser Navigate to http://localhost:8000 to start playing!

🎯 How to Play

  1. Set Up the Game

    • Enter the number of rows and columns for your board
    • Click "Start New Game"
  2. 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')
  3. 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!
  4. 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

🏗️ Project Structure

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

🔧 Technical Details

Backend (Python)

  • 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

Frontend (JavaScript/HTML/CSS)

  • 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

API Endpoints

  • GET / - Serve the main game interface
  • POST /start - Initialize a new game
  • POST /remove - Make a move (remove squares)
  • POST /computer_move - Let computer make a move
  • POST /undo - Undo the last move
  • GET /get_board_state - Get current game state

🎲 Game Rules

  1. Setup: The game is played on a rectangular grid of chocolate squares
  2. Poison Square: The top-left square (0,0) is poisoned
  3. Turn Taking: Players alternate making moves
  4. Move Mechanics: Removing a square also removes all squares to its right and below
  5. Winning: The player who forces their opponent to take the poison square wins
  6. Losing: The player who is left with only the poison square loses

🤖 AI Strategy

The computer opponent uses a strategic approach:

  1. Prioritizes moves that leave an odd number of squares
  2. Avoids creating 1×N or N×1 board shapes (when possible)
  3. Uses randomization when multiple equally good moves exist
  4. Considers long-term positioning rather than just immediate gains

🎨 Customization

You can easily customize the game:

  • Board Appearance: Modify styles.css to change colors and styling
  • AI Difficulty: Adjust the computer strategy in main.py
  • Game Rules: Extend the Game class for variants
  • UI Elements: Update index.html and script.js for interface changes

🐛 Troubleshooting

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

🎓 Learning Opportunities

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

🤝 Contributing

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

📝 License

This project is open source and available under the MIT License.


Enjoy playing Chomp! 🍫

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages