Skip to content

A web-powered Tic-Tac-Toe game built with Python, Pygame, and Flask. This project integrates a classic game with a backend for tracking scores, multiplayer functionality, and enhanced game logic. Learn about event handling, animations, and real-time interactions while building this fun and interactive game.

Notifications You must be signed in to change notification settings

Nuraj250/TicTacFlask

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

15 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐ŸŽฎ Web-Powered Tic-Tac-Toe Game

This is a fully-featured Tic-Tac-Toe game built with Flask, Pygame, PostgreSQL/Firebase, and WebSockets.
It supports multiplayer matchmaking, AI opponents, chat system, leaderboards, animations, and sound effects.


๐Ÿš€ Features

โœ… Classic Tic-Tac-Toe Gameplay (Pygame-based UI)
โœ… Multiplayer Mode (Real-time matchmaking via Flask-SocketIO)
โœ… Single-Player Mode (AI Opponent) (Minimax Algorithm)
โœ… ELO-Based Ranking System
โœ… In-Game Chat System
โœ… Flashing Win Animation & Sound Effects
โœ… Match History & Leaderboard


๐Ÿ“š Project Structure

tic-tac-toe-web/
โ”œโ”€โ”€ backend/                     # Flask Backend
โ”‚   โ”œโ”€โ”€ models/                   # Database Models
โ”‚   โ”œโ”€โ”€ routes/                   # API Endpoints
โ”‚   โ”œโ”€โ”€ services/                 # Business Logic
โ”‚   โ”œโ”€โ”€ utils/                    # Utilities
โ”‚   โ”œโ”€โ”€ database/                 # Database Setup
โ”‚   โ”œโ”€โ”€ scripts/                  # Deployment & Server Management
โ”‚   โ”œโ”€โ”€ tests/                    # Unit & Integration Tests
โ”‚   โ”œโ”€โ”€ config.py                 # Configuration
โ”‚   โ”œโ”€โ”€ app.py                    # Main Flask Application
โ”‚   โ”œโ”€โ”€ requirements.txt          # Dependencies
โ”‚   โ”œโ”€โ”€ .env                      # Environment Variables
โ”‚   โ”œโ”€โ”€ README.md                 # Backend Documentation
โ”œโ”€โ”€ frontend/                     # Pygame Frontend
โ”‚   โ”œโ”€โ”€ assets/                    # Game Assets (Images, Sounds)
โ”‚   โ”œโ”€โ”€ components/                # UI Components
โ”‚   โ”œโ”€โ”€ services/                  # API & WebSocket Services
โ”‚   โ”œโ”€โ”€ main.py                    # Main Pygame Application
โ”‚   โ”œโ”€โ”€ settings.py                # Game Configuration
โ”‚   โ”œโ”€โ”€ README.md                  # Frontend Documentation
โ”œโ”€โ”€ README.md                      # Full Project Documentation

๐Ÿ“ฆ Installation & Setup

1๏ธโƒฃ Clone the Repository

git clone /~https://github.com/Nuraj250/tic-tac-toe-web.git
cd tic-tac-toe-web

2๏ธโƒฃ Backend Setup

Create Virtual Environment & Install Dependencies

cd backend
python -m venv venv
source venv/bin/activate  # Windows: venv\Scripts\activate
pip install -r requirements.txt

Configure Environment Variables

Create a .env file in backend/:

SECRET_KEY=your_secret_key
JWT_SECRET_KEY=your_jwt_secret
DATABASE_URL=postgresql://user:password@localhost/tic_tac_toe
SOCKETIO_MESSAGE_QUEUE=redis://

Initialize Database

python database/init_db.py

Start the Flask API

bash scripts/start_server.sh

or manually:

python app.py

3๏ธโƒฃ Frontend Setup

Install Dependencies

cd frontend
pip install pygame requests socketio-client

Run the Pygame Frontend

bash ../backend/scripts/start_game.sh

or manually:

python main.py

๐ŸŽฎ Gameplay Instructions

Game Controls

  • Click on a cell to place your move.
  • Wait for your turn (if multiplayer).
  • Press "H" to view match history.
  • Click "Restart" after a win to start a new game.

Multiplayer Mode

  1. The game automatically pairs two players.
  2. Wait in the lobby until an opponent joins.
  3. Moves are synced in real-time via WebSockets.

Single-Player Mode (vs AI)

  1. Click on a cell to place "X".
  2. AI will automatically respond as "O".
  3. Try to win against the AI!

๐Ÿ“ก API Endpoints

๐Ÿ” Authentication

Method Endpoint Description
POST /api/auth/register Register a new user
POST /api/auth/login Login & get JWT token

๐ŸŽฎ Game Logic

Method Endpoint Description
POST /api/game/play_move Submit a move
GET /api/match/{id} Get match history

๐Ÿค– AI Opponent

Method Endpoint Description
POST /api/ai-move Get AI move suggestion

๐Ÿ† Leaderboard

Method Endpoint Description
GET /api/leaderboard Get top players

๐Ÿ’ฌ Chat System

Method Endpoint Description
POST /api/chat/send-message Send chat message

๐Ÿ”Š Sound & Animations

Animations

  • Moves are animated with a fade effect.
  • Winning row flashes yellow before showing the restart button.

Sound Effects

Action Sound File Description
Move Placement move_sound.wav Plays when a move is made
Click Sound click_sound.wav Plays when clicking UI elements
Win Sound win_sound.wav Plays when a player wins

๐Ÿ“œ Deployment

1๏ธโƒฃ Deploy on Heroku

heroku login
heroku create tic-tac-toe-web
git push heroku main

2๏ธโƒฃ Deploy on AWS (EC2)

scp -i your-key.pem backend/* ec2-user@your-instance:/home/ec2-user/
ssh -i your-key.pem ec2-user@your-instance

3๏ธโƒฃ Deploy on DigitalOcean

Use Docker:

docker build -t tic-tac-toe-backend .
docker run -p 5000:5000 tic-tac-toe-backend

๐Ÿ’ก Troubleshooting

Game Won't Start?

  • Ensure Flask backend is running:
bash backend/scripts/start_server.sh
  • Check API_BASE_URL in frontend/settings.py.

Database Connection Issues

  • Ensure PostgreSQL is running:
sudo service postgresql start
  • Verify database URL in .env.

Flask-SocketIO Not Working?

  • Install Redis:
sudo apt install redis-server
  • Start Redis:
redis-server

๐Ÿ License

This project is licensed under the MIT License.


๐Ÿ’ Connect

๐Ÿ’ป Nuraj
๐Ÿš€ Happy Coding! ๐ŸŽฎ๐Ÿ”ฅ

About

A web-powered Tic-Tac-Toe game built with Python, Pygame, and Flask. This project integrates a classic game with a backend for tracking scores, multiplayer functionality, and enhanced game logic. Learn about event handling, animations, and real-time interactions while building this fun and interactive game.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published