Jim Collado Get in touch

Connect 4 AI

An intelligent agent for Connect 4, built with Python, Minimax, and Alpha-Beta Pruning.

Project thumbnail image of two robots playing a board game

What We Did

For my Intelligent Systems class, I worked with two other students to create an AI player for Connect 4 using the Minimax Algorithm. We optimized performance with Alpha-Beta Pruning. The AI evaluates moves recursively, applying heuristics to balance offense and defense. Results showed our agent consistently outperformed other strategies—even holding its own against human players. (Learn more about Connect 4)

Our Implementation

Snippet of our implementation

We started with a human vs. human version of Connect 4 using Pygame. Then we replaced one player with a basic AI making random moves, and finally implemented Minimax. The challenge was designing an evaluation function to score game states and guide the AI’s decisions.

Evaluation Function

The evaluation function assigns scores to board states by checking connected pieces, blocking opponent threats, and rewarding moves that build toward four-in-a-row. While effective, computation became slow as tree depth increased.

To make deeper searches practical, we optimized Minimax with Alpha-Beta Pruning, reducing redundant calculations and improving efficiency.

Alpha-Beta Pruning

Alpha-Beta pruning allowed us to prune entire branches of the search tree that would never affect the outcome. With it, our agent could reliably search to depth 8 without unacceptable delays. This upgrade made the AI noticeably stronger and more competitive.

Bitboards

Visualization of the Connect 4 board using the Bitboard Method

We explored bitboards to further optimize performance. Representing the board in binary allowed faster checks with bitwise operations instead of nested loops. Although our bitboard implementation was incomplete, it highlighted exciting future potential for AI efficiency.

Conclusion

Developing the Connect 4 AI was challenging and rewarding. By combining Minimax, Alpha-Beta pruning, and custom evaluation functions, we built an agent capable of competitive play. This project deepened our understanding of algorithms, heuristics, and optimizations in AI game design.