雨云服务器助你轻松搭建内网穿透,解决无公网IP问题。-X黑手网
雨云服务器助你轻松搭建内网穿透,解决无公网IP问题。-X黑手网

HTML#字棋游戏

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <title>井字棋游戏</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            text-align: center;
            margin-top: 50px;
        }
        h1 {
            color: #333;
        }
        .board {
            display: grid;
            grid-template-columns: repeat(3, 100px);
            grid-template-rows: repeat(3, 100px);
            gap: 5px;
            margin: 20px auto;
            width: 310px;
        }
        .cell {
            width: 100px;
            height: 100px;
            background-color: #f0f0f0;
            display: flex;
            justify-content: center;
            align-items: center;
            font-size: 48px;
            cursor: pointer;
            border-radius: 5px;
        }
        .cell:hover {
            background-color: #e0e0e0;
        }
        #status {
            font-size: 24px;
            margin: 20px 0;
            color: #666;
        }
        #reset {
            padding: 10px 20px;
            font-size: 16px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
        }
        #reset:hover {
            background-color: #45a049;
        }
    </style>
</head>
<body>    
    <h1>井字棋游戏</h1>
    <div id="status">当前玩家: X</div>
    <div class="board" id="board">
        <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="reset">重新开始</button>

    <script>
        document.addEventListener('DOMContentLoaded', () => {
            const board = document.getElementById('board');
            const cells = document.querySelectorAll('.cell');
            const status = document.getElementById('status');
            const resetButton = document.getElementById('reset');
            
            let currentPlayer = 'X';
            let gameState = ['', '', '', '', '', '', '', '', ''];
            let gameActive = true;
            
            const winningConditions = [
                [0, 1, 2], [3, 4, 5], [6, 7, 8], // 横线
                [0, 3, 6], [1, 4, 7], [2, 5, 8], // 竖线
                [0, 4, 8], [2, 4, 6]             // 对角线
            ];
            
            function handleCellClick(e) {
                const clickedCell = e.target;
                const clickedCellIndex = parseInt(clickedCell.getAttribute('data-index'));
                
                if (gameState[clickedCellIndex] !== '' || !gameActive) {
                    return;
                }
                
                gameState[clickedCellIndex] = currentPlayer;
                clickedCell.textContent = currentPlayer;
                
                checkResult();
            }
            
            function checkResult() {
                let roundWon = false;
                
                for (let i = 0; i < winningConditions.length; i++) {
                    const [a, b, c] = winningConditions[i];
                    if (gameState[a] === '' || gameState[b] === '' || gameState[c] === '') {
                        continue;
                    }
                    if (gameState[a] === gameState[b] && gameState[b] === gameState[c]) {
                        roundWon = true;
                        break;
                    }
                }
                
                if (roundWon) {
                    status.textContent = `玩家 ${currentPlayer} 获胜!`;
                    gameActive = false;
                    return;
                }
                
                if (!gameState.includes('')) {
                    status.textContent = '平局!';
                    gameActive = false;
                    return;
                }
                
                currentPlayer = currentPlayer === 'X' ? 'O' : 'X';
                status.textContent = `当前玩家: ${currentPlayer}`;
            }
            
            function resetGame() {
                currentPlayer = 'X';
                gameState = ['', '', '', '', '', '', '', '', ''];
                gameActive = true;
                status.textContent = `当前玩家: ${currentPlayer}`;
                
                cells.forEach(cell => {
                    cell.textContent = '';
                });
            }
            
            cells.forEach(cell => {
                cell.addEventListener('click', handleCellClick);
            });
            
            resetButton.addEventListener('click', resetGame);
        });
    </script>
</body>
</html>

 

请登录后发表评论

    没有回复内容