This is an implementation of the Connect Four game with an AI opponent using Python and Pygame. The AI uses the Minimax algorithm with alpha-beta pruning and an evaluation function for strategic play. The game features a graphical user interface and includes an early stopping mechanism based on time limits. The board fills from the bottom up, as per standard Connect Four rules.
- Python 3.8+
- Pygame (
pip install pygame)
- Clone the repository:
git clone <repository-url>
cd connect-four- Install dependencies:
pip install pygame- Run the game:
python main.py- The human player (Red) goes first, clicking a column to drop a token.
- The AI (Yellow) responds automatically.
- The goal is to connect four tokens horizontally, vertically, or diagonally.
- Close the window to exit the game.
- Minimax Algorithm: Used to select the best move for the AI.
- Alpha-Beta Pruning: Optimizes the search by pruning unnecessary branches.
- Evaluation Function: Scores board positions based on potential wins and strategic positions.
- Early Stopping: Limits AI thinking time to 2 seconds per move.
- Graphical Interface: Built with Pygame, showing a grid and colored tokens.
board.py: Board creation, piece placement, and win condition logic.minimaxAI.py: AI logic including minimax, alpha-beta pruning, and evaluation function.main.py: Game loop and Pygame user interface.README.md: Project documentation.
The project is maintained in a Git repository. To contribute:
- Fork the repository.
- Create a feature branch (
git checkout -b feature-name). - Commit changes (
git commit -m "Description"). - Push to the branch (
git push origin feature-name). - Create a pull request.
MIT License