Tic tac toe, unbeatable minimax

šŸŽ® Tic Tac Toe AI

Tic Tac Toe with Player vs Player & Player vs AI mode. AI uses Minimax for perfect play.

šŸŽ® Tic Tac Toe AI screenshot

Documentation

šŸŽ® Tic Tac Toe AI (with Minimax) using Pygame

This project is a Tic Tac Toe game implemented in Python using Pygame, with support for both Player vs Player and Player vs AI modes. The AI uses the Minimax algorithm for perfect play.

Screenshot


šŸš€ Features

  • āœ”ļø Single-player vs AI with difficulty levels:
    • Level 0: Random moves
    • Level 1: Minimax AI (unbeatable)
  • 🧠 Minimax algorithm implementation with recursive backtracking
  • šŸŽ® Player vs Player mode
  • šŸ”„ Reset and game mode toggle with keyboard
  • šŸ–¼ļø Graphical interface using pygame

šŸ› ļø Installation

Requirements

  • Python 3.6+
  • Pygame
  • NumPy

Install dependencies

pip install pygame numpy

ā–¶ļø Run the Game

python main.py

Make sure you have all necessary files including constants.py.


šŸŽ® Controls

ActionKey/Mouse
Make a moveMouse click
Reset the gameR
Toggle game mode (PvP/AI)G
Set AI to random mode0
Set AI to minimax mode1

🧠 AI Logic

The AI uses a classic Minimax algorithm that:

  • Evaluates all possible moves recursively
  • Chooses the move with the highest score (maximizing for AI, minimizing for player)
  • Always plays optimally

šŸ“ File Structure

.
ā”œā”€ā”€ main.py              # Main game logic
ā”œā”€ā”€ constants.py         # UI and game settings (colors, sizes)
ā”œā”€ā”€ images/
│   └── screenshot.png   # Screenshot of the game (optional)
└── README.md

šŸ§‘ā€šŸ’» Author

Made by Hitesh-s0lanki. Feel free to fork and enhance!


šŸ“„ License

This project is licensed under the MIT License. See LICENSE for details.