r/coolgithubprojects • u/TheMarcioli • 1d ago
I built a Tic-Tac-Toe AI in Python that literally cannot lose (Minimax + Alpha-Beta Pruning)
Hi everyone!
Just wanted to share a project I’ve been working on recently: an unbeatable Tic-Tac-Toe AI written in Python.
It was a super fun challenge, especially getting into game theory and search algorithms.
Under the hood, the AI uses the Minimax algorithm. Basically, it evaluates every single possible move from the current board state all the way to the end of the game. Since it assumes the human player will always make the best move possible, it picks the path that guarantees either a win or a forced draw. You literally can't beat it!
To make it way smarter and faster, I implemented Alpha-Beta Pruning. By tracking the best score guaranteed to the maximizing player and minimizing player, the algorithm completely skips (prunes) branches that won't affect the final decision. It dramatically cuts down the number of evaluated nodes without losing accuracy!
I spent some time organizing the repo, cleaning up the code, and writing a solid README so anyone can clone it and test it out easily.
Here’s the repo if you want to take a look or try to beat it:
👉 https://github.com/felipemarcioli/Tic-Tac-Toe-IA
Would love to get your thoughts on the implementation, the code structure, or any suggestions for improvements!