<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Multiplayer Tic Tac Toe</title>
    <style>
        /* Reset and basic styles */
        * {
            box-sizing: border-box;
            margin: 0;
            padding: 0;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(135deg, #ece9e6, #ffffff);
            display: flex;
            flex-direction: column;
            align-items: center;
            min-height: 100vh;
            padding: 20px;
        }

        h1 {
            margin-bottom: 20px;
            color: #333;
        }

        /* Room Interface */
        #room-interface {
            display: flex;
            flex-direction: column;
            align-items: center;
            margin-bottom: 20px;
        }

        #room-interface input {
            padding: 10px;
            margin: 5px 0;
            width: 200px;
            border: 1px solid #ccc;
            border-radius: 5px;
        }

        #room-interface button {
            padding: 10px 20px;
            margin-top: 10px;
            background-color: #4CAF50;
            border: none;
            color: white;
            border-radius: 5px;
            cursor: pointer;
            transition: background-color 0.3s ease;
        }

        #room-interface button:hover {
            background-color: #45a049;
        }

        /* Status Message */
        #status {
            margin-bottom: 20px;
            font-size: 1.2em;
            color: #555;
        }

        /* Game Grid */
        #game {
            display: grid;
            grid-template-columns: repeat(3, 100px);
            grid-gap: 10px;
            margin-bottom: 20px;
        }

        .cell {
            width: 100px;
            height: 100px;
            background-color: #fafafa;
            border: 2px solid #ddd;
            border-radius: 10px;
            font-size: 2.5em;
            display: flex;
            align-items: center;
            justify-content: center;
            cursor: pointer;
            transition: background-color 0.3s ease, transform 0.1s ease;
        }

        .cell:hover {
            background-color: #f0f0f0;
        }

        .cell.disabled {
            cursor: not-allowed;
            background-color: #e0e0e0;
            color: #999;
        }

        /* Restart Button */
        #restart {
            padding: 10px 20px;
            font-size: 1em;
            background-color: #2196F3;
            border: none;
            color: white;
            border-radius: 5px;
            cursor: pointer;
            transition: background-color 0.3s ease;
        }

        #restart:hover {
            background-color: #0b7dda;
        }

        /* Responsive Design */
        @media (max-width: 500px) {
            #game {
                grid-template-columns: repeat(3, 80px);
                grid-gap: 8px;
            }

            .cell {
                width: 80px;
                height: 80px;
                font-size: 2em;
            }

            #room-interface input {
                width: 160px;
            }
        }
    </style>
</head>
<body>

<h1>Multiplayer Tic Tac Toe</h1>

<div id="room-interface">
    <button id="create-room">Create Room</button>
    <div style="margin: 10px 0; font-weight: bold;">OR</div>
    <input type="text" id="room-code-input" placeholder="Enter Room Code" maxlength="6">
    <button id="join-room">Join Room</button>
</div>

<div id="current-room" style="display: none; margin-bottom: 10px; font-size: 1em; color: #333;">
    Room Code: <span id="current-room-code"></span>
</div>

<div id="status">Please create or join a room to start the game.</div>

<div id="game" style="display: none;">
    <!-- 9 cells -->
    <div class="cell" data-index="0"></div>
    <div class="cell" data-index="1"></div>
    <div class="cell" data-index="2"></div>
    <div class="cell" data-index="3"></div>
    <div class="cell" data-index="4"></div>
    <div class="cell" data-index="5"></div>
    <div class="cell" data-index="6"></div>
    <div class="cell" data-index="7"></div>
    <div class="cell" data-index="8"></div>
</div>
<button id="restart" disabled>Restart</button>

<script>
    const createRoomButton = document.getElementById('create-room');
    const joinRoomButton = document.getElementById('join-room');
    const roomCodeInput = document.getElementById('room-code-input');
    const currentRoomDiv = document.getElementById('current-room');
    const currentRoomCodeSpan = document.getElementById('current-room-code');

    const statusDiv = document.getElementById('status');
    const gameDiv = document.getElementById('game');
    const cells = document.querySelectorAll('.cell');
    const restartButton = document.getElementById('restart');

    let socket;
    let mySymbol;
    let myTurn = false;
    let gameActive = false;
    let roomCode = null;

    function connect() {
        // Replace 'localhost' and port '6789' with your server's address and port if different
        socket = new WebSocket('ws://localhost:6789');

        socket.onopen = () => {
            console.log('Connected to server');
        };

        socket.onmessage = (event) => {
            const message = JSON.parse(event.data);
            switch(message.type) {
                case 'created':
                    roomCode = message.room;
                    currentRoomCodeSpan.textContent = roomCode;
                    currentRoomDiv.style.display = 'block';
                    statusDiv.textContent = 'Waiting for an opponent to join...';
                    gameDiv.style.display = 'none';
                    break;
                case 'start':
                    mySymbol = message.symbol;
                    roomCode = message.room;
                    currentRoomCodeSpan.textContent = roomCode;
                    currentRoomDiv.style.display = 'block';
                    gameDiv.style.display = 'grid';
                    gameActive = true;
                    myTurn = mySymbol === 'X'; // X always starts
                    updateStatus();
                    break;
                case 'turn':
                    const symbol = message.symbol;
                    myTurn = (symbol === mySymbol);
                    updateStatus();
                    break;
                case 'move':
                    updateBoard(message.index, message.symbol);
                    break;
                case 'end':
                    gameActive = false;
                    statusDiv.textContent = message.result;
                    restartButton.disabled = false;
                    break;
                case 'reset':
                    resetGame();
                    break;
                case 'error':
                    alert(message.message);
                    break;
                default:
                    console.log('Unknown message type:', message.type);
            }
        };

        socket.onclose = () => {
            statusDiv.textContent = 'Disconnected from server.';
            gameActive = false;
            gameDiv.style.display = 'none';
            currentRoomDiv.style.display = 'none';
        };

        socket.onerror = (error) => {
            console.error('WebSocket error:', error);
        };
    }

    function updateStatus() {
        if (!gameActive) return;
        if (myTurn) {
            statusDiv.textContent = 'Your turn (' + mySymbol + ')';
        } else {
            statusDiv.textContent = 'Opponent\'s turn';
        }
    }

    function updateBoard(index, symbol) {
        const cell = document.querySelector(`.cell[data-index='${index}']`);
        if (cell && cell.textContent === '') {
            cell.textContent = symbol;
            cell.classList.add('disabled');
        }
    }

    function resetGame() {
        cells.forEach(cell => {
            cell.textContent = '';
            cell.classList.remove('disabled');
        });
        restartButton.disabled = true;
        statusDiv.textContent = 'Game reset. Waiting for opponent...';
        myTurn = mySymbol === 'X';
        gameActive = true;
        updateStatus();
    }

    createRoomButton.addEventListener('click', () => {
        socket.send(JSON.stringify({ type: 'create' }));
        createRoomButton.disabled = true;
        joinRoomButton.disabled = true;
        roomCodeInput.disabled = true;
    });

    joinRoomButton.addEventListener('click', () => {
        const code = roomCodeInput.value.trim().toUpperCase();
        if (code.length !== 6) {
            alert('Please enter a valid 6-character room code.');
            return;
        }
        socket.send(JSON.stringify({ type: 'join', room: code }));
        createRoomButton.disabled = true;
        joinRoomButton.disabled = true;
        roomCodeInput.disabled = true;
    });

    cells.forEach(cell => {
        cell.addEventListener('click', () => {
            if (!gameActive || !myTurn || cell.textContent !== '') return;
            const index = cell.getAttribute('data-index');
            socket.send(JSON.stringify({ type: 'move', index: parseInt(index), room: roomCode }));
            cell.textContent = mySymbol;
            cell.classList.add('disabled');
            myTurn = false;
            updateStatus();
        });
    });

    restartButton.addEventListener('click', () => {
        socket.send(JSON.stringify({ type: 'reset', room: roomCode }));
        restartButton.disabled = true;
    });

    window.addEventListener('load', () => {
        connect();
    });
</script>

</body>
</html>

add animations dark theme