<!DOCTYPE html>
<html>
<head>
    <title>Tic Tac Toe</title>
    <style>
        #gameboard {
            width: 300px;
            margin: 0 auto;
        }
        #gameboard td {
            width: 100px;
            height: 100px;
            border: 1px solid #000;
            text-align: center;
            font-size: 75px;
        }
    </style>
</head>
<body>
    <h1>Tic Tac Toe</h1>
    <div id="gameboard">
        <table>
            <tr>
                <td id="cell-0"></td>
                <td id="cell-1"></td>
                <td id="cell-2"></td>
            </tr>
            <tr>
                <td id="cell-3"></td>
                <td id="cell-4"></td>
                <td id="cell-5"></td>
            </tr>
            <tr>
                <td id="cell-6"></td>
                <td id="cell-7"></td>
                <td id="cell-8"></td>
            </tr>
        </table>
    </div>
    <script>
        const cellElements = document.querySelectorAll('#gameboard td');
        let playerTurn = 'X';
<pre><code>    function handleCellClick(e) {
        e.target.innerHTML = playerTurn;
        togglePlayerTurn();
    }

    function togglePlayerTurn() {
        playerTurn = playerTurn === 'X' ? 'O' : 'X';
    }

    cellElements.forEach(function(cell) {
        cell.addEventListener('click', handleCellClick);
    })
&lt;/script&gt;
</code></pre>
</body>
</html>
Play Tic Tac Toe Online - Free HTML Game

原文地址: https://www.cveoy.top/t/topic/lnj7 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录