A web-based TicTacToe game by CrystalSky offering local and online multiplayer and vs AI (Algorythm).
| index.php | ||
| README.md | ||
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
- Clone or download the files to your web directory
- Ensure write permissions to the system temp directory
- Configure your web server to serve the files
- 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
- Click "Multiplayer" from the main menu
- Choose "Create New Game" to host, or "Join Game" to enter an existing game code
- Share the 6-character game code with your opponent
- Wait for your opponent to join, then take turns making moves
- Use the chat feature to communicate during the game
Local Multiplayer
- Select "Local Multiplayer" from the main menu
- Players take turns clicking on the game board
- Player X always goes first
vs AI
- Choose "vs AI" from the main menu
- You play as X, the AI plays as O
- 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 gamejoinGame- Joins an existing game using a codemakeMove- Submits a player's movegetGameState- Retrieves current game statesendMessage- Sends a chat messagegetMessages- Retrieves chat historydisconnect- Handles player disconnectiongetActiveGames- 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