Skip to content

Powerful Connect4 AI using minimax algorithm with alpha-beta pruning

spmfte/connect4-bot

Repository files navigation

Connect4 Bot

An automated bot for playing Connect4 online, with two implementation options:

  1. DOM-based Bot (Browser Extension): Interacts with the game through the DOM
  2. OCR-based Bot (Python): Uses screen capture and mouse control

Features

  • Powerful Connect4 AI using minimax algorithm with alpha-beta pruning
  • Automatic board state detection
  • Player color detection
  • Works on various Connect4 game websites

Project Structure

connect4-bot/
├── src/                   # DOM-based bot JavaScript source files
│   ├── algorithm.js       # AI algorithm implementation
│   ├── board.js           # Board state detection
│   ├── communication.js   # Extension communication
│   ├── content.js         # Main bot logic
│   ├── init.js            # Bot initialization
│   ├── observers.js       # DOM observation
│   └── ui.js              # UI interaction
├── py-bot/                # OCR-based bot Python implementation
│   ├── connect4_ocr_bot.py    # Main Python bot
│   ├── connect4_extension.js  # Bridge to browser
│   ├── requirements.txt       # Python dependencies
│   ├── test_ocr.py            # Test OCR functionality
│   ├── run_bot.sh             # Run script
│   └── README.md              # OCR bot documentation
├── manifest.json          # Chrome extension manifest
├── popup.html             # Extension popup
├── popup.js               # Popup JavaScript
├── background.js          # Extension background script
└── README.md              # This file

DOM-based Bot (Extension)

The DOM-based implementation works as a browser extension that:

  1. Detects the Connect4 board in the DOM
  2. Reads the board state from DOM elements
  3. Calculates the best move using the AI algorithm
  4. Makes the move by triggering appropriate DOM events

Installation

  1. Build the extension:
npm install
npm run build
  1. Load the extension in Chrome:
    • Open chrome://extensions
    • Enable Developer mode
    • Click "Load unpacked" and select the dist directory

OCR-based Bot (Python)

The OCR-based implementation:

  1. Captures the screen to find the Connect4 board
  2. Uses color detection to identify pieces
  3. Calculates the best move using the AI algorithm
  4. Controls the mouse to click on the desired column

Requirements

  • Python 3.7+
  • OpenCV
  • PyTesseract
  • PyAutoGUI
  • NumPy
  • Flask
  • Pillow

Installation

cd py-bot
pip install -r requirements.txt

Usage

cd py-bot
./run_bot.sh

AI Algorithm

Both implementations use the same minimax algorithm with alpha-beta pruning to calculate the best moves. The AI:

  1. Evaluates the board state
  2. Looks ahead multiple moves
  3. Handles immediate wins and blocks
  4. Prefers center columns for better positioning

License

MIT

About

Powerful Connect4 AI using minimax algorithm with alpha-beta pruning

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published