This project implements a console-based Connect 4 game with multiple bot difficulty levels. The development was divided into four sprints:
- Two-player game
- Easy-level bot
- Medium-level bot
- Hard-level bot โ near-perfect AI using Minimax, iterative deepening, transposition tables, and opening tables.
Connect 4 is a turn-based strategy game on a 7ร6 board. Players take turns dropping pieces into columns, aiming to connect four in a row horizontally, vertically, or diagonally.
Features implemented:
- Full board display with colored pieces (
A= red,B= blue). - Move validation, win/draw detection.
- Bot difficulty selection: Easy, Medium, Hard.
- Console-based interaction.
| File | Description |
|---|---|
main.c |
Game loop, input handling, mode selection |
board.c / board.h |
Board management and scoring |
utils.c / utils.h |
Helper functions |
bot_easy.c / bot_easy.h |
Easy bot logic (random moves) |
bot_medium.c / bot_medium.h |
Medium bot logic (tactical) |
minimax.c / minimax.h |
Minimax algorithm |
hashing.c / hashing.h |
Zobrist hashing for transposition table |
evaluation.c / evaluation.h |
Alpha-beta evaluation function |
transposition.c / transposition.h |
Transposition table management |
Makefile |
Build automation |
- Console-based two-player game.
- Players alternate turns using
alternatePlayers()and select columns usinggetColumnFromUser(). - Board printed after each move (
printBoard()). - Win/draw detection (
checkWin(),checkDraw()).
- Bot selects random valid moves.
- Ensured bot never chooses invalid columns.
- Console outputs bot moves.
- Attempts to win immediately if possible.
- Blocks opponent's immediate win.
- Prefers central columns for strategic advantage.
- Falls back to random moves if no other option.
- Complexity: O(1) per move due to small board size (7ร6).
Core Features:
- Minimax Algorithm with Alpha-Beta Pruning โ depth = 11.
- Iterative Deepening โ builds transposition table at each depth.
- Transposition Table with Zobrist Hashing โ stores previously evaluated positions.
- Opening Table โ optimal first 1โ3 moves.
- Evaluation Function โ scores positions based on patterns, center preference, immediate threats.
- Threat-Driven Move Ordering โ prioritizes winning/blocking moves.
- Null-Move Pruning โ skips some moves to reduce computation time.
Performance:
- Against humans: essentially unbeatable.
- Against other bots: likely to force draw or win.
Unit Testing:
- Tested board operations (add/remove pieces, win/draw).
- Verified evaluation function and move ordering logic.
Integration Testing:
- Human vs Human gameplay.
- Bot vs Bot gameplay (Easy, Medium, Hard).
- Tested edge cases and invalid input.
- Checked console output correctness.
Debugging Tools:
- GDB: Step through minimax and threat detection.
- Valgrind: Ensured no memory leaks.
| Sprint | Improvement |
|---|---|
| 1 | Two-player game with full board and win detection |
| 2 | Easy bot with random valid moves |
| 3 | Medium bot: blocks opponent, prioritizes center, tactical moves |
| 4 | Hard bot: minimax, alpha-beta, iterative deepening, transposition table, opening table, threat-aware evaluation |
- Hard bot is fast, tactical, and practically unbeatable.
- Opening table ensures strong early-game strategy.
- Hard bot may slow at depth 11 if transposition table is cold.
- Opening table only covers first 2โ3 plies.
- Console interface limits visualization.
- Not multi-threaded.
apk update
apk add build-base
git clone <repo-url>Set tty1 for auto-update if needed.
Architecture: Client-server over 127.0.0.1 using TCP.
- Server: Waits for connection, maintains board, sends updates.
- Client: Connects, sends moves, receives updated board.
Typical functions: socket(), bind(), listen(), accept(), connect(), send(), recv().
- Project progressed from easy โ medium โ hard bot.
- Hard bot combines iterative deepening, transposition tables, opening table, threat-aware evaluation.
- Depth 11 ensures dominance against human players and other bots.
Future Enhancements:
- Dynamic depth adjustment.
- Multithreading.
- Full-width endgame solver.
- Science Buddies (2024) โ Minimax Algorithm with Alpha-Beta Pruning in Connect 4
- Koushik Chandra Maji (2024) โ AI: Minimax AlphaBeta Connect4 in Java
- Part 4 โ Alpha-beta algorithm (2017) โ Gamesolver.org
- Sawwan, A. (2021) โ Artificial Intelligence-Based Connect 4 Player Using Python
- Aberent โ Transposition Table & Zobrist Hashing
- GeeksforGeeks (2023) โ Minimax Algorithm in Game Theory โ Zobrist Hashing