Here is a single, clean, complete README.md with everything included, including the requirements.txt section, fully integrated and formatted.
A fully automated Connect 4 assistant that:
- Uses computer vision (OpenCV) to detect the live physical board
- Uses a minimax AI with alpha–beta pruning to pick the best move
- Detects cheating, illegal moves, and when the first player ignores the AI
- Displays everything in a polished Tkinter GUI, including AI move highlights
- Includes an optional GUI for prerecorded videos to test the system without a camera
Main file to run the live game: connect4_gui.py
Install all dependencies via:
pip install -r requirements.txtnumpy
opencv-python
Pillow(tkinter comes bundled with Python and does not need to be installed.)
| File | Purpose |
|---|---|
connect4_solver.py |
Core Connect 4 logic + minimax AI |
read_board.py |
Computer vision pipeline for detecting board state |
test_connect-4.py |
Offline AI tests using synthetic boards |
connect4_gui.py |
Main live webcam GUI |
This module contains the Connect 4 rules and the AI strategy.
EMPTY = 0
RED = 1
YEL = 2
make_board()– Creates a 6×7 empty boardprint_board()– Pretty prints the board with column numbersget_valid_locations()– Returns playable columnsis_valid_location()– Checks if a column has spaceget_next_open_row()– Finds the lowest empty celldrop_piece_inplace()– Modifies the board directlydrop_piece_copy()– Returns a modified copy (used by minimax)
-
is_winner(board, piece)– Checks for horizontal, vertical, or diagonal 4-in-a-row -
is_terminal(board)– True if:- Someone has won
- No moves remain
-
evaluate_window(window, ai_piece)– Scores 4-cell windows -
score_position(board, ai_piece)– Scores entire board based on:- Center column priority
- Threats
- 2/3/4 in-a-row opportunities
minimax(...)– Full depth-limited minimax searchchoose_best_move(board, ai_piece=YEL, depth=5)– Picks best column
This module uses OpenCV to turn camera/video frames into a 6×7 numeric board.
-
Blob detection via
SimpleBlobDetector -
Supports:
- Webcam index (
0,1, etc.) - Video file path (
"test_video.mp4")
- Webcam index (
-
Capture frame
-
Detect circular blobs (Connect 4 holes/pieces)
-
Use
cv2.findCirclesGridto order blobs into a 7×6 grid -
Read average color of each blob
-
Classify color into:
0empty1red2yellow
-
Return board as a 6×7 NumPy array
Includes a debug mode that overlays blobs and prints board state continuously.
This script runs the AI against predefined board scenarios.
It:
-
Converts CV-format boards → AI-format
-
Runs
choose_best_move() -
Prints:
- Test name
- Board
- AI’s chosen move
- Pass/fail if an expected answer is given
Run via:
python test_connect-4.pyThis is the primary file to run the full game:
python connect4_gui.py-
Live webcam feed or pre-recorded videos
- Pre-recorded videos may appear slow due to the added computation of OpenCV live decoding the video file
-
Real-time detection of 42 circles (6 rows × 7 columns)
-
Stability filtering (must appear identical for several frames)
-
Move detection:
- Detects exactly one new piece
- No disappearing or recoloring allowed
-
Cheating detection:
- Same player moves twice
- Multiple changes between frames
- Removing/adding extra pieces
-
Winner detection
-
AI move recommendation
-
Green highlight above the recommended column
-
Timer + turn counter
-
"New Game" menu option
-
_update_video()runs at ~30 FPS:-
Reads frame
-
Runs CV detection
-
Updates stable board
-
Checks for:
- New piece
- Cheating
- Winner
-
Gets AI recommendation
-
Draws highlight circle
-
Updates Tkinter image
-
pip install -r requirements.txtpython connect4_gui.pyComment/Uncomment line 483 to run using video stream or live board. Test videos are in the Test Videos folder
pytest -q