Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Tic-Tac-Toe AI (Minimax Algorithm)

Overview

This project implements a Tic-Tac-Toe game with an AI opponent using the Minimax algorithm. The graphical interface is built using Pygame, allowing a user to play against a computer that makes optimal decisions.

The AI evaluates possible future board states and selects moves that maximize its chances of winning while minimizing the opponent’s chances.


Features

  • Play Tic-Tac-Toe against an AI opponent

  • Choose to play as X or O

  • AI uses the Minimax algorithm for decision making

  • Detects:

    • Wins
    • Losses
    • Draws
  • Interactive graphical interface using Pygame

  • Restart option after game completion


Project Structure

project/
│
├── runner.py          # Main game interface using Pygame
├── tictactoe.py       # Game logic and Minimax AI implementation
├── OpenSans-Regular.ttf
└── README.md

runner.py

Handles:

  • Game window
  • Player input
  • Rendering board and moves
  • AI turn management

tictactoe.py

Contains the core game logic:

  • Board representation
  • Determining valid moves
  • Checking winners
  • Minimax algorithm for AI decision making

Requirements

Install the required library:

pip install pygame

Python version:

Python 3.8+

How to Run

  1. Navigate to the project directory.

  2. Run the main game file:

python runner.py
  1. Choose whether to play as X or O.

  2. Click on the grid to place your move.


Game Rules

  • The game is played on a 3 × 3 board.
  • Players take turns placing X and O.
  • The first player to align three marks in a row, column, or diagonal wins.
  • If all spaces are filled and no player wins, the game ends in a draw.

Minimax Algorithm

The AI uses the Minimax algorithm, a decision-making algorithm used in game theory.

How it works

  1. Generate all possible moves from the current board state.

  2. Simulate each move recursively.

  3. Assign scores to terminal states:

    • X win → +1
    • O win → −1
    • Draw → 0
  4. Choose the move that maximizes the AI's chance of winning.

The algorithm ensures the AI always selects the optimal move.


Example Gameplay

  1. Player selects X.
  2. Player clicks a square.
  3. AI evaluates all possible board states using Minimax.
  4. AI makes the best move automatically.

Concepts Demonstrated

This project demonstrates several Artificial Intelligence and programming concepts:

  • Minimax algorithm
  • Game state evaluation
  • Recursive search
  • Adversarial search
  • Python object-oriented programming
  • GUI development using Pygame

Possible Improvements

Future enhancements may include:

  • Alpha-Beta pruning for faster search
  • Improved UI design
  • Difficulty levels
  • Move animations
  • Score tracking

Author

Developed as part of an Artificial Intelligence / Game AI programming exercise.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages