An automated bot for playing Connect4 online, with two implementation options:
- DOM-based Bot (Browser Extension): Interacts with the game through the DOM
- OCR-based Bot (Python): Uses screen capture and mouse control
- Powerful Connect4 AI using minimax algorithm with alpha-beta pruning
- Automatic board state detection
- Player color detection
- Works on various Connect4 game websites
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
The DOM-based implementation works as a browser extension that:
- Detects the Connect4 board in the DOM
- Reads the board state from DOM elements
- Calculates the best move using the AI algorithm
- Makes the move by triggering appropriate DOM events
- Build the extension:
npm install
npm run build- Load the extension in Chrome:
- Open chrome://extensions
- Enable Developer mode
- Click "Load unpacked" and select the
distdirectory
The OCR-based implementation:
- Captures the screen to find the Connect4 board
- Uses color detection to identify pieces
- Calculates the best move using the AI algorithm
- Controls the mouse to click on the desired column
- Python 3.7+
- OpenCV
- PyTesseract
- PyAutoGUI
- NumPy
- Flask
- Pillow
cd py-bot
pip install -r requirements.txtcd py-bot
./run_bot.shBoth implementations use the same minimax algorithm with alpha-beta pruning to calculate the best moves. The AI:
- Evaluates the board state
- Looks ahead multiple moves
- Handles immediate wins and blocks
- Prefers center columns for better positioning
MIT