Skip to content

ANSHAM1/Tic-Tac-Toe-AI

Repository files navigation

Tic Tac Toe AI

A Tic-Tac-Toe game written in C++20 featuring an AI opponent powered by the Minimax algorithm with memoization (dynamic programming). The project is designed with clean separation of responsibilities and is structured to support arbitrary board sizes, making it easy to extend and experiment with more advanced game AI techniques.

Features

  • 🎮 Human vs AI gameplay
  • 🧠 Minimax algorithm for optimal decision making
  • ⚡ Memoization (DP) to avoid recomputing previously evaluated game states
  • ✅ Efficient win detection using only the last played move
  • 📏 Configurable board size (N × N, where N >= 3)
  • 🛡️ Robust user input validation
  • 🧩 Modular and object-oriented design

Algorithms Used

Minimax

The AI explores every possible continuation of the game assuming both players make optimal moves.

  • AI (X) acts as the maximizing player.
  • Human (O) acts as the minimizing player.

Scoring:

Result Score
AI Wins +1
Draw 0
Player Wins -1

Memoization

Previously evaluated board states are cached using:

std::unordered_map<std::string, int>

This significantly reduces the number of recursive evaluations by reusing computed results for identical board configurations.

Optimized Win Detection

Instead of checking every row, column, and diagonal after each move, the engine only examines the row, column, and relevant diagonals affected by the most recent move, reducing unnecessary computation.

Technologies

  • C++20
  • STL
  • CMake
  • Object-Oriented Programming
  • Dynamic Programming
  • Backtracking
  • Recursion

Future Improvements

  • Alpha-Beta Pruning
  • Depth-limited search for larger boards
  • Heuristic evaluation for N × N gameplay

Learning Outcomes

This project demonstrates practical implementation of:

  • Object-Oriented Programming
  • Recursive search algorithms
  • Backtracking
  • Dynamic Programming (Memoization)
  • State-space exploration
  • Game AI fundamentals
  • Efficient board representation and state management

Build

mkdir build
cd build
cmake ..
cmake --build .

Run

Execute the generated application and follow the console prompts to play against the AI.

About

A Tic-Tac-Toe AI built in modern C++ using the Minimax algorithm with memoization (dynamic programming), efficient last-move win detection, and scalable board-size support.

Topics

Resources

Stars

Watchers

Forks

Contributors

Languages