This is a text-based Connect 4 game implemented in Python, featuring an intelligent AI opponent. The AI leverages the Minimax algorithm with Alpha-Beta Pruning to analyze the game state and make optimal moves. It prioritizes winning moves while blocking the human player's threats, ensuring a challenging and competitive gameplay experience.
- The game presents a clear and structured UI, displaying the current game board after each move.
- The human player can input their moves by selecting a column number.
- The AI responds with an intelligent move.
- The AI uses the Minimax algorithm with Alpha-Beta pruning to search the best possible move.
- It maximizes its chances of winning while blocking the opponent's potential victories.
- The AI evaluates the board by:
- Prioritizing four-in-a-row wins.
- Blocking the human's three-in-a-row threats.
- Maximizing its own board position strategically.
- The program automatically checks for a winner after each move.
- If the board is full without a winner, it declares a stalemate.
- After the game ends, the user can choose to play again without restarting the program.
The AI's decision-making is powered by the Minimax algorithm with Alpha-Beta pruning to improve efficiency.
- The algorithm simulates all possible future board states up to a certain depth.
- It assigns a score to each board state based on the evaluation function:
- Winning Move: +1000
- Blocking Opponent's Win: +100
- Three-in-a-row (AI): +10
- Three-in-a-row (Human): -50
- Other strategic placements.
- The AI selects the move with the highest score.
- Depth-Limited Search: The algorithm searches up to 4 moves ahead for efficiency.
- πΉ You can modify this by changing the
depthglobal variable in the code.
- πΉ You can modify this by changing the
- Alpha-Beta Pruning: Eliminates unnecessary moves to reduce computation time.
- Run the Python script to start the game.
- Choose the board size:
- Rows: Between 5 and 7.
- Columns: Between 6 and 8.
- Choose who starts first (Human or AI).
- The game board is displayed, and the player selects a column (1-
column count). - The AI makes its move, and the board updates.
- The game continues until:
- A player wins (four in a row).
- The board is full (stalemate).
- The game announces the result and asks if you want to play again.