This repository contains two versions of a Connect Four game implemented in Python:
- Connect4 (Two Human Players)
- Connect4 (AI Player) featuring a minimax-based computer opponent.
Both versions retain the core Connect Four mechanics but differ in how the second player’s moves are decided.
Connect Four is a classic two-player game where you drop discs into one of the columns, attempting to line up four of your discs in a row before your opponent does. This repository offers:
- A traditional 2-player mode (Human vs. Human).
- An AI-enhanced mode (Human vs. Computer) using minimax with alpha-beta pruning, leveraging the
random
andmath
libraries.
-
Board Generation
Creates a 6×7 grid by default usinggenerateField()
. You can customize row and column sizes via parameters. -
Turn-Based Input
Players (Player 0 ="R"
, Player 1 ="Y"
) alternate columns to drop their discs. -
Winning/Tie Checks
After each move, checks if 4 in a row is formed (horizontally, vertically, or diagonally). If the board fills up, the result is a tie. -
Simple Board Drawing
Displays columns separated by" | "
with a dashed line after each row.
-
AI Opponent
In this version, Player 1 is replaced with an AI that uses a minimax algorithm for strategic column selection. -
Scoring & Evaluation
The AI’s decision-making:- Considers potential winning moves.
- Evaluates board states based on the number of aligned discs and the center-column advantage.
- Searches multiple moves ahead (controlled by a
depth
parameter).
-
Random Element
Some fallback decisions may involve therandom
library to break ties among equally strong moves. -
Still Allows Board Customization
The same row and column parameters can be adjusted for custom board sizes.
-
Objective
Get four of your discs in a row before your opponent does. The row can be:- Horizontal
- Vertical
- Diagonal (descending or ascending)
-
Turn Order
- Player 0 goes first with discs labeled
"R"
. - Player 1 uses discs labeled
"Y"
. - In the AI version, Player 1 is controlled by the minimax algorithm.
- Player 0 goes first with discs labeled
-
Placement Rules
- Choose a column (1-based index).
- The disc drops in the lowest empty row of that column.
- If the column is full, you’ll be asked to choose another one.
-
Ending Conditions
- A player achieves 4 in a row → that player wins immediately.
- The grid is full with no winner → tie.
-
Clone the Repository:
git clone https://github.com/Sabdikay/Connect4.git cd Connect4
-
Run the Desired Version:
- Two Human Players:
python Connect4.py
- AI Player:
python Connect4(AI player).py
- Two Human Players:
-
Play the Game:
- Input the column when prompted.
- For the AI version, watch as the computer chooses an optimal (or near-optimal) move.
-
Customize Board Dimensions (Optional):
- In either file, you can call
startGame(nrow, ncol)
with custom values:startGame(8, 9) # 8 rows, 9 columns
- In either file, you can call
Connect4/
├── Connect4.py # Two Human Players
├── Connect4(AI player).py # Computer Player using Minimax
├── README.md # This README
-
Connect4.py
Implements the standard 2-player logic. -
Connect4(AI player).py
Contains the AI logic using minimax with helper functions (evaluateField
,minimax
, etc.).
- More Sophisticated AI: Add a deeper search or a weighting system for more advanced strategic play.
- GUI Version: Integrate a library like Pygame or tkinter for a graphical interface.
- Logging & Statistics: Track win rates, average game length, and other useful data.
Enjoy your game of Connect Four, whether you challenge a friend or our AI opponent!