WP Manifestindependent plugin directory
manifest / utilities / tic-tac-toe-wordpress-plugin

Tic Tac Toe Plugin

WordPress Tic Tac Toe plugin with 3 AI difficulty levels - Technical assessment

★ 0stars

Install

Download source zip No release zip yet

This is the repository archive. It installs, but the folder name will carry the branch suffix and updates will not flow.

Install straight from GitHub with WP-CLI:

wp plugin install https://github.com/rivka771/tic-tac-toe-wordpress-plugin/archive/refs/heads/main.zip
No built-in updater. New versions won't appear under Dashboard → Updates. Install new releases yourself, or let Git Updater track the repository.

A professional WordPress plugin implementing a single-player Tic Tac Toe game with three difficulty levels, developed as a technical assessment.

Author

Rivka Fus

Overview

This plugin provides a complete Tic Tac Toe game implementation for WordPress, featuring an intelligent AI opponent with three difficulty levels, player statistics tracking, and a clean REST API interface.

Features Implemented

Part 1: Plugin and Game Creation

  • ✅ Standard WordPress plugin structure with separate installer
  • ✅ PHP-based game logic with full session management
  • ✅ Single-player mode against computer AI
  • ✅ Three distinct difficulty levels:
    • Easy: Random move selection - perfect for beginners
    • Medium: Tactical AI that tries to win and blocks player moves
    • Hard: Minimax algorithm implementation - optimal play (never loses)

Part 2: Database Management

  • ✅ Uses WordPress native wp_postmeta table (no custom tables)
  • ✅ Custom post types for game sessions and player data
  • ✅ Complete CRUD operations using WordPress functions
  • ✅ Player statistics tracking:
    • Total points (10 per win)
    • Games played, won, lost, and tied
    • Win rate calculation

Part 3: REST API

  • ✅ POST /wp-json/tic-tac-toe/v1/session - Initialize new game
  • ✅ POST /wp-json/tic-tac-toe/v1/move - Play move and get updated board
  • ✅ GET /wp-json/tic-tac-toe/v1/session/{id} - Retrieve session state
  • ✅ Full input validation and sanitization
  • ✅ Proper error handling with WP_Error

Additional Features

Beyond the core requirements, the plugin includes:

  • Admin Dashboard: View comprehensive player statistics
  • Shortcode Support: Easy integration with [tic_tac_toe]
  • Dynamic Settings: Players can change name and difficulty without page refresh
  • Responsive Design: Works seamlessly on mobile, tablet, and desktop
  • Security First: All inputs validated, outputs escaped, SQL queries prepared

Installation

Method 1: Manual Installation

  1. Download the plugin files
  2. Upload the tic-tac-toe-plugin folder to /wp-content/plugins/
  3. Activate the plugin through the WordPress admin panel
  4. The plugin will automatically set up required data structures

Method 2: WordPress Admin

  1. Go to Plugins → Add New
  2. Click "Upload Plugin"
  3. Choose the plugin ZIP file
  4. Click "Install Now" then "Activate"

Usage

Frontend - Shortcode

Add the game to any page or post using the shortcode:

[tic_tac_toe]

With custom parameters:

[tic_tac_toe player_name="John" difficulty="hard"]

Parameters:

  • player_name - Default player name (default: "Player")
  • difficulty - Default difficulty level: easy, medium, or hard (default: "medium")

Admin Dashboard

  1. Navigate to Tic Tac Toe → Players in WordPress admin
  2. View comprehensive statistics for all players:
    • Total points
    • Games played, won, lost, tied
    • Win rate percentage

REST API

Initialize Game Session

curl -X POST https://yoursite.com/wp-json/tic-tac-toe/v1/session \
  -H "Content-Type: application/json" \
  -d '{
    "mode": "single",
    "player_name": "John",
    "difficulty": "hard"
  }'

Response:

{
  "session_id": "550e8400-e29b-41d4-a716-446655440000",
  "message": "Session created successfully"
}

Play Move

curl -X POST https://yoursite.com/wp-json/tic-tac-toe/v1/move \
  -H "Content-Type: application/json" \
  -d '{
    "session_id": "550e8400-e29b-41d4-a716-446655440000",
    "x": 0,
    "y": 0
  }'

Response:

{
  "board": [
    {"x": 0, "y": 0, "value": "X"},
    {"x": 1, "y": 0, "value": "O"},
    {"x": 2, "y": 0, "value": ""},
    ...
  ],
  "winner": ""
}

🏗️ Technical Architecture

File Structure

tic-tac-toe-plugin/
├── assets/
│   ├── css/
│   │   ├── admin.css          # Admin dashboard styles
│   │   └── frontend.css       # Frontend game styles
│   └── js/
│       ├── admin.js           # Admin JavaScript
│       └── frontend.js        # Frontend game logic
├── includes/
│   ├── class-admin.php        # Admin interface management
│   ├── class-api.php          # REST API endpoints
│   ├── class-database.php     # Database operations
│   ├── class-frontend.php     # Frontend shortcode
│   └── class-game-logic.php   # Game logic and AI
├── installer.php              # Separate installer
├── tic-tac-toe-plugin.php     # Main plugin file
└── README.md                  # This file

Design Decisions

Why Custom Post Types?

  • Assignment requires using wp_postmeta
  • Leverages WordPress built-in systems
  • No custom database tables needed
  • Easy backup and migration
  • Follows WordPress best practices

Why Minimax for Hard Mode?

  • Guarantees optimal play (never loses)
  • Classic game theory algorithm
  • Demonstrates advanced programming skills
  • Instant performance on 3×3 board

Why Separate Classes?

  • Separation of Concerns: Each class has a single responsibility
  • Maintainability: Easy to update and extend
  • Testability: Individual components can be tested
  • WordPress Standards: Follows recommended plugin architecture

Security Features

  • ✅ Direct Access Prevention: All files check ABSPATH
  • ✅ SQL Injection Protection: All queries use $wpdb->prepare()
  • ✅ XSS Prevention: All output escaped with esc_html(), esc_attr()
  • ✅ Input Sanitization: All inputs cleaned with sanitize_text_field()
  • ✅ REST API Validation: Complete validation callbacks on all endpoints
  • ✅ Nonce Verification: AJAX requests include nonce tokens

Algorithm Highlights

Minimax Implementation

The Hard difficulty uses the classic Minimax algorithm:

  1. Recursively explores all possible game outcomes
  2. Assigns scores to terminal states:
    • Computer win: +10 (minus depth for faster wins)
    • Human win: -10 (plus depth to delay losses)
    • Tie: 0
  3. Maximizes computer score, minimizes human score
  4. Returns optimal move based on complete game tree analysis

Performance: On a 3×3 board, the algorithm evaluates at most 362,880 states but due to pruning and early game termination, responses are instant (<1ms).

Testing

Tested with:

  • ✅ WordPress 6.0+
  • ✅ PHP 7.2+
  • ✅ WP_DEBUG mode enabled (no errors)
  • ✅ Multiple browsers (Chrome, Firefox, Safari, Edge)
  • ✅ Mobile devices (responsive design) Database Schema

Custom Post Types

tic_tac_toe_session

Stores game session data in wp_postmeta:

  • tic_tac_toe_session - Serialized session data (board, players, status)
  • tic_tac_toe_session_id - UUID for quick lookups

tic_tac_toe_player

Stores player statistics in wp_postmeta:

  • tic_tac_toe_player_name - Player name
  • tic_tac_toe_player_points - Total points earned
  • tic_tac_toe_player_games_played - Total games
  • tic_tac_toe_player_games_won - Wins
  • tic_tac_toe_player_games_lost - Losses
  • tic_tac_toe_player_games_tied - Ties

Future Enhancement Ideas

While the current implementation meets all requirements, potential enhancements include:

  • Per-difficulty statistics tracking
  • Multiplayer mode (two human players)
  • Game history and replay functionality
  • Integration with WordPress user accounts
  • Difficulty-based scoring (Easy: 5pts, Medium: 10pts, Hard: 20pts)
  • Leaderboard system
  • Tournament mode
  • Internationalization (i18n) support

Development Notes

Time Spent: Approximately 6-8 hours

  • Initial setup and architecture: 1 hour
  • Core game logic and AI: 2-3 hours
  • Database integration: 1 hour
  • REST API implementation: 1 hour
  • Frontend/Admin interfaces: 1-2 hours
  • Testing and refinement: 1 hour

WordPress Standards Compliance:

  • Follows WordPress Coding Standards
  • Uses WordPress functions exclusively
  • No direct SQL queries (except with $wpdb->prepare())
  • Proper hook usage throughout

Contact

For questions or clarifications:

  • Developer: Rivka Fus
  • Email: r45771@gmail.com
  • Phone: 0583264771

Note: This plugin was developed as part of a technical assessment for a WordPress Developer position. It demonstrates proficiency in WordPress development, PHP programming, algorithm implementation, and adherence to coding standards.