Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

6 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Connect Four Solver

A sophisticated Connect Four game implementation with an optimal AI opponent using the minimax algorithm with alpha-beta pruning. Play in your browser with a beautiful, responsive interface or via command line.

Connect Four Demo Python FastAPI AI

Features

๐ŸŽฎ Game Features

  • Customizable Board: Choose your board dimensions (4-10 width/height)
  • Flexible Win Conditions: Set how many tokens needed to win (3-6)
  • Two Play Modes: Human vs Human or Human vs AI
  • Optimal AI: Uses minimax algorithm with alpha-beta pruning
  • Move Management: Undo moves and reset games
  • Win Detection: Automatic detection of wins and ties

๐ŸŽจ Interface Features

  • Beautiful UI: Modern, responsive web interface
  • Visual Feedback: Smooth animations and hover effects
  • Mobile Friendly: Works on desktop, tablet, and mobile
  • Real-time Status: Live game status and player indicators
  • Error Handling: Clear error messages and loading states

๐Ÿง  AI Features

  • Minimax Algorithm: Optimal move calculation
  • Alpha-Beta Pruning: Efficient tree traversal
  • Depth-Limited Search: Configurable search depth
  • Strategic Evaluation: Advanced board position scoring
  • Immediate Tactics: Prioritizes wins and blocks opponent wins

Installation

Prerequisites

  • Python 3.7 or higher
  • pip (Python package manager)

Setup

  1. Clone or download the project files:

    git clone <repository-url>
    cd connect-four-solver
  2. Install dependencies:

    pip install fastapi uvicorn
  3. Run the web server:

    uvicorn app:app --reload
  4. Open your browser and navigate to:

    http://localhost:8000
    

File Structure

connect-four-solver/
โ”œโ”€โ”€ main.py          # Core game logic and AI implementation
โ”œโ”€โ”€ app.py           # FastAPI web server and API endpoints
โ”œโ”€โ”€ index.html       # Web interface HTML
โ”œโ”€โ”€ styles.css       # Modern CSS styling
โ”œโ”€โ”€ script.js        # Frontend JavaScript logic
โ””โ”€โ”€ README.md        # This file

How to Play

Web Interface

  1. Game Setup:

    • Adjust board height (4-10, default: 6)
    • Adjust board width (4-10, default: 7)
    • Set tokens needed to win (3-6, default: 4)
    • Click "Create New Board"
  2. Making Moves:

    • Click column headers to drop tokens
    • Red tokens (๐Ÿ”ด) for Player 1
    • Yellow tokens (๐ŸŸก) for Player 2
  3. Game Controls:

    • ๐Ÿค– AI Move: Let the computer make optimal move
    • โ†ถ Undo Move: Take back the last move
    • ๐Ÿ”„ Reset Game: Clear board, keep settings
    • ๐ŸŽฎ New Game: Create fresh game with new settings

Command Line Interface

Run the standalone version:

python main.py

Follow the prompts to:

  • Set board dimensions and win conditions
  • Choose between manual moves and AI suggestions
  • Undo moves and restart games

Game Rules

  • Objective: Connect your tokens in a row (horizontal, vertical, or diagonal)
  • Turns: Players alternate dropping tokens into columns
  • Gravity: Tokens fall to the lowest available position in each column
  • Win: First player to connect the required number of tokens wins
  • Tie: Game ends in a tie if the board fills with no winner

AI Algorithm

The AI uses a sophisticated minimax algorithm with several optimizations:

Core Algorithm

  • Minimax: Explores all possible future game states
  • Alpha-Beta Pruning: Eliminates unnecessary branches for efficiency
  • Depth-Limited Search: Searches 8 moves ahead by default
  • Evaluation Function: Scores board positions strategically

AI Strategy

  1. Immediate Win: Always takes winning moves
  2. Block Opponent: Prevents opponent from winning
  3. Strategic Positioning: Builds towards future wins
  4. Center Preference: Favors central columns for flexibility

Performance

  • Search Depth: 8 levels (configurable)
  • Pruning Efficiency: ~90% branch elimination
  • Response Time: Usually under 1 second
  • Optimality: Plays perfectly within search depth

API Endpoints

The FastAPI server provides these endpoints:

Endpoint Method Description
/ GET Serve web interface
/get_board GET Get current board state
/create_board POST Create new game board
/make_move POST Make a move in specified column
/get_best_move GET Get AI's optimal move
/undo_move POST Undo the last move
/reset POST Reset current game

Configuration

Board Settings

  • Height: 4-10 rows (default: 6)
  • Width: 4-10 columns (default: 7)
  • Win Length: 3-6 tokens (default: 4)

AI Settings

You can modify these in main.py:

  • Search Depth: self.max_depth = 8
  • Evaluation Weights: Modify evaluate_window() scoring

Development

Adding Features

  • New AI Strategies: Modify the evaluate_board() function
  • UI Improvements: Update styles.css and index.html
  • Game Variants: Extend the Board class in main.py

Testing

  • Manual Testing: Use the web interface
  • AI Testing: Play against the AI at different difficulties
  • Edge Cases: Test with different board sizes and win conditions

Technical Details

Backend (Python)

  • FastAPI: Modern, fast web framework
  • Pydantic: Data validation and serialization
  • Object-Oriented: Clean, maintainable code structure

Frontend (JavaScript/HTML/CSS)

  • Vanilla JavaScript: No external dependencies
  • Responsive Design: Works on all screen sizes
  • Modern CSS: Gradients, animations, and flexbox
  • Async/Await: Clean asynchronous API calls

Algorithm Complexity

  • Time: O(b^d) where b=branching factor, d=depth
  • Space: O(d) with iterative deepening
  • Optimized: Alpha-beta pruning reduces effective branching

Troubleshooting

Common Issues

  1. "Failed to make move: Invalid move"

    • Column might be full
    • Game might already be over
    • Try refreshing the page
  2. Server won't start

    • Check if port 8000 is available
    • Install required dependencies: pip install fastapi uvicorn
  3. AI takes too long

    • Reduce max_depth in main.py
    • Smaller boards search faster
  4. Interface not loading

    • Ensure all files are in the same directory
    • Check browser console for errors

Performance Tips

  • Use smaller boards for faster AI responses
  • Reduce search depth for quicker moves
  • Clear browser cache if interface issues persist

Contributing

Feel free to contribute improvements:

  • Bug fixes: Report issues and submit fixes
  • Features: Add new game modes or AI improvements
  • UI/UX: Enhance the visual design
  • Documentation: Improve this README

License

This project is open source. Feel free to use, modify, and distribute.

Acknowledgments

  • Minimax algorithm implementation inspired by classic game theory
  • Web interface design follows modern UI/UX principles
  • FastAPI framework enables clean, fast API development

Enjoy playing Connect Four against the optimal AI! ๐ŸŽฎ๐Ÿค–

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages