Table of Contents Link to heading
- Summary
- Files Overview
- Heuristic Score Calculation
- Minimax Algorithm
- GUI Features
- Dependencies
- Demo Video
Summary Link to heading
This project implements an AI agent for Connect Four using the Minimax algorithm with alpha-beta pruning. The game features a graphical user interface (GUI) where users can play against the AI, adjust the search depth, and toggle pruning.
Files Overview Link to heading
- GUI.java: Handles the game interface, user input, and display. It interacts with the Minimax AI to determine the AI’s moves.
- Minimax.java: Contains the Minimax algorithm implementation, including heuristic evaluation and alpha-beta pruning.
Heuristic Score Calculation Link to heading
The heuristic function evaluates the board state by analyzing all possible sequences of four consecutive cells (horizontal, vertical, diagonal). For each sequence:
- Count AI agent
a
and opponento
pieces in the sequence. - Score adjustment:
- If the sequence contains only AI pieces:
score += 10^(number of AI pieces)
- If the sequence contains only opponent pieces:
score -= 10^(number of opponent pieces)
- Mixed/empty sequences do not affect the score.
- If the sequence contains only AI pieces:
This prioritizes creating/blocking potential wins and rewards the AI for controlling more cells in a sequence.
Minimax Algorithm Link to heading
Key Parts Link to heading
-
State Class: Represents a game state with:
boardState
: 42-character string (a
,o
,#
for empty).depth
: Current depth in the search tree.maxOrMin
: Indicates if it’s the AI’s (maximizer) or opponent’s (minimizer) turn.
-
Algorithm Flow:
- Maximizer: Selects the move with the highest heuristic value from successors.
- Minimizer: Selects the move with the lowest heuristic value from successors.
- Depth Limiting: Stops recursion at a user-defined depth (default: 2) and returns the heuristic score.
-
Alpha-Beta Pruning: Optimizes Minimax by pruning branches that cannot influence the final decision, reducing computation time.
Methods Link to heading
value(State state)
: Entry point for Minimax, delegates tomaximizer()
orminimizer()
.abValue(State state, int alpha, int beta)
: Alpha-beta variant ofvalue()
.getSuccessors(State state)
: Generates valid next states for all columns.
GUI Features Link to heading
- Interactive Board: Click “Drop” buttons to place pieces.
- Settings:
- Depth Adjustment: Controls how many moves ahead should the AI plans (2–7).
- Pruning Toggle: Enables/disables alpha-beta pruning for faster decisions.
- Win Detection: Displays scores and results when the board is full.
Dependencies Link to heading
- Java Swing for GUI components.
- Image icons for board visuals (provided in the
src/
directory).