Skip to content

steve-jr/tic-tac-toe-ai

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🎮 Tic Tac Toe

A modern, colorful web-based Tic-Tac-Toe game featuring an unbeatable AI opponent powered by the minimax algorithm. Built with Flask backend and vanilla JavaScript frontend.

Python Flask License

✨ Features

  • 🤖 Unbeatable AI - Minimax algorithm ensures perfect play
  • 🎯 Player Choice - Choose to play as X or O
  • 📊 Statistics Tracking - Win/loss/draw record with win rate
  • 📱 Responsive Design - Works on desktop and mobile devices
  • 🔄 Quick Restart - Multiple restart options after game ends

🚀 Quick Start

Prerequisites

  • Python 3.11 or higher
  • pip (Python package manager)

Local Development

  1. Clone the repository

    git clone https://github.com/steve-jr/tic-tac-toe-ai.git
    cd tic-tac-toe-ai
  2. Create virtual environment

    python -m venv venv
    source venv/bin/activate  # On Windows: venv\Scripts\activate
  3. Install dependencies

    pip install -r requirements.txt
  4. Run the application

    python app.py
  5. Open in browser

    http://localhost:5000
    

🔧 API Endpoints

Endpoint Method Description
/ GET Serve main game page
/state GET Get current game state
/select-player POST Choose X or O
/move POST Make a move
/reset POST Reset game

Sample API Response

{
  "board": [["X", null, "O"], [null, "X", null], ["O", null, null]],
  "currentPlayer": "X",
  "humanPlayer": "X",
  "isHumanTurn": true,
  "isTerminal": false,
  "winner": null,
  "isDraw": false,
  "needsPlayerSelection": false
}

🧠 Technical

Minimax Algorithm

The AI uses the minimax algorithm with alpha-beta pruning to evaluate all possible game states and choose the optimal move. This makes the AI unbeatable - the best you can do is draw! Game state stored in Flask sessions (cookies). Each player gets their own game instance

🤝 Contributing

  1. Fork the repository
  2. Create feature branch (git checkout -b feature/amazing-feature)
  3. Commit changes (git commit -m 'Add amazing feature')
  4. Push to branch (git push origin feature/amazing-feature)
  5. Open Pull Request

📄 License

This project is licensed under the MIT License - see the LICENSE file for details.

🙏 Acknowledgments

  • Minimax algorithm implementation inspired by CS50 AI course at UMich
  • UI design inspired by modern glassmorphism trends
  • Built with Flask and pure JavaScript for simplicity

📞 Support


Made with ❤️ and ☕ by Steve Junior

About

Play Tic Tac Toe against an unbeatable AI

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published