A web-based TicTacToe game by CrystalSky offering local and online multiplayer and vs AI (Algorythm).
Find a file
2026-01-25 16:01:45 +01:00
index.php Add index.php 2026-01-25 15:40:42 +01:00
README.md Added README.md with some content 2026-01-25 16:01:45 +01:00

TicTacToe

A modern, feature-rich TicTacToe game built with PHP and JavaScript, offering multiple game modes including online multiplayer, local multiplayer, and AI opponents.

Important

It works on our site, but may not on yours.

Features

Game Modes

  • Online Multiplayer - Play with friends remotely using unique game codes
  • Local Multiplayer - Play on the same device with a friend
  • vs AI - Challenge an intelligent computer opponent using minimax algorithm

Real-time Chat

  • Integrated chat system for online multiplayer games
  • Message history and player identification
  • Safety features and user guidelines

Security & Performance

  • Input sanitization and validation
  • File locking for race condition prevention
  • Session management with automatic cleanup
  • Rate limiting and session caps

Requirements

  • PHP 7.4 or higher
  • Web server (Apache, Nginx, or built-in PHP server)
  • Modern web browser with JavaScript enabled
  • Write permissions to temp directory for session storage

Installation

  1. Clone or download the files to your web directory
  2. Ensure write permissions to the system temp directory
  3. Configure your web server to serve the files
  4. Access the game through your browser

Quick Start with PHP Built-in Server

cd /path/to/tictactoe
php -S localhost:8000

Then visit http://localhost:8000 in your browser.

Configuration

The game includes several configurable constants at the top of index.php:

define('SESSION_TIMEOUT', 180);        // Session timeout in seconds (3 minutes)
define('MAX_SESSIONS', 1000);          // Maximum concurrent games
define('SESSIONS_FILE', sys_get_temp_dir() . '/tictactoe_sessions.json');
define('LOCK_FILE', sys_get_temp_dir() . '/tictactoe_sessions.lock');

How to Play

Online Multiplayer

  1. Click "Multiplayer" from the main menu
  2. Choose "Create New Game" to host, or "Join Game" to enter an existing game code
  3. Share the 6-character game code with your opponent
  4. Wait for your opponent to join, then take turns making moves
  5. Use the chat feature to communicate during the game

Local Multiplayer

  1. Select "Local Multiplayer" from the main menu
  2. Players take turns clicking on the game board
  3. Player X always goes first

vs AI

  1. Choose "vs AI" from the main menu
  2. You play as X, the AI plays as O
  3. The AI uses the minimax algorithm for optimal gameplay

Game Rules

  • The game is played on a 3×3 grid
  • Players take turns placing their symbol (X or O) on empty squares
  • The first player to get 3 of their symbols in a row (horizontally, vertically, or diagonally) wins
  • If all 9 squares are filled without a winner, the game is a draw

API Endpoints

The game handles AJAX requests with the following actions:

  • createGame - Creates a new multiplayer game
  • joinGame - Joins an existing game using a code
  • makeMove - Submits a player's move
  • getGameState - Retrieves current game state
  • sendMessage - Sends a chat message
  • getMessages - Retrieves chat history
  • disconnect - Handles player disconnection
  • getActiveGames - Returns count of active games

File Structure

tictactoe/
├── index.php          # Main game file (PHP + HTML + CSS + JS)
├── README.md          # This documentation
└── stylesheet.css     # External stylesheet (if used)

Session Management

The game uses file-based session storage to track multiplayer games:

  • Sessions are stored in JSON format in the system temp directory
  • Automatic cleanup of expired and abandoned games
  • File locking prevents race conditions
  • Maximum session limits prevent resource exhaustion

Security Features

  • Input Validation: All user inputs are sanitized and validated
  • XSS Protection: HTML escaping for user-generated content
  • CSRF Protection: Same-origin policy for AJAX requests
  • Rate Limiting: Implicit limits through session management
  • Safe Chat: Message length limits and content filtering

Browser Compatibility

  • Chrome 60+
  • Firefox 55+
  • Safari 12+
  • Edge 79+

Troubleshooting

Common Issues

"Server busy, try again"

  • Check if the maximum session limit has been reached
  • Verify write permissions to the temp directory "Game not found"
  • Ensure the game code is entered correctly (6 characters, alphanumeric)
  • Check if the game has expired due to timeout Chat messages not sending
  • Verify the game is in multiplayer mode
  • Check message length (max 200 characters)

Debug Mode

To enable debug information, check the server error logs for detailed messages about session management and game state.

Credits

Created by CrystalSky