使用HTML创建井字棋

井字棋可以通过HTML、CSS和JavaScript的组合来实现。以下是实现井字棋的步骤:

HTML结构

创建一个简单的HTML文件,包含一个3x3的网格,用于放置井字棋的格子。

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>井字棋</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <div class="game-container">
        <div class="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>
        <div class="status">当前玩家: X</div>
        <button class="reset">重新开始</button>
    </div>
    <script src="script.js"></script>
</body>
</html>
 
CSS样式

为井字棋添加样式,使其看起来像一个标准的3x3网格。

css 复制代码
body {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    margin: 0;
    font-family: Arial, sans-serif;
}

.game-container {
    text-align: center;
}

.board {
    display: grid;
    grid-template-columns: repeat(3, 100px);
    grid-template-rows: repeat(3, 100px);
    gap: 5px;
    margin-bottom: 20px;
}

.cell {
    width: 100px;
    height: 100px;
    background-color: #f0f0f0;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 48px;
    cursor: pointer;
}

.status {
    font-size: 24px;
    margin-bottom: 20px;
}

.reset {
    padding: 10px 20px;
    font-size: 16px;
    cursor: pointer;
}
 
JavaScript逻辑

添加JavaScript代码,实现井字棋的游戏逻辑,包括玩家切换、胜负判断和重新开始功能。

javascript 复制代码
const cells = document.querySelectorAll('.cell');
const statusDisplay = document.querySelector('.status');
const resetButton = document.querySelector('.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) {
        statusDisplay.textContent = `玩家 ${currentPlayer} 获胜!`;
        gameActive = false;
        return;
    }

    if (!gameState.includes('')) {
        statusDisplay.textContent = '平局!';
        gameActive = false;
        return;
    }

    currentPlayer = currentPlayer === 'X' ? 'O' : 'X';
    statusDisplay.textContent = `当前玩家: ${currentPlayer}`;
}

function resetGame() {
    currentPlayer = 'X';
    gameState = ['', '', '', '', '', '', '', '', ''];
    gameActive = true;
    statusDisplay.textContent = `当前玩家: ${currentPlayer}`;
    cells.forEach(cell => cell.textContent = '');
}

cells.forEach(cell => cell.addEventListener('click', handleCellClick));
resetButton.addEventListener('click', resetGame);
 
功能说明
  • 玩家切换:每次点击格子后,玩家会在X和O之间切换。
  • 胜负判断:检查是否有玩家连成一条线(横、竖或斜)。
  • 平局判断:如果所有格子被填满且没有玩家获胜,则判定为平局。
  • 重新开始:点击"重新开始"按钮可以重置游戏状态。

将上述代码分别保存为HTML、CSS和JavaScript文件,并在浏览器中打开HTML文件即可运行井字棋游戏。

相关推荐
小李子呢02111 小时前
前端八股CSS(2)---动画的实现方式
前端·javascript
GreenTea3 小时前
从 Claw-Code 看 AI 驱动的大型项目开发:2 人 + 10 个自治 Agent 如何产出 48K 行 Rust 代码
前端·人工智能·后端
渣渣xiong3 小时前
从零开始:前端转型AI agent直到就业第五天-第十一天
前端·人工智能
布局呆星4 小时前
Vue3 | 组件通信学习小结
前端·vue.js
C澒4 小时前
IntelliPro 企业级产研协作平台:前端智能生产模块设计与落地
前端·ai编程
OpenTiny社区5 小时前
重磅预告|OpenTiny 亮相 QCon 北京,共话生成式 UI 最新技术思考
前端·开源·ai编程
前端老实人灬5 小时前
web前端面试题
前端
Moment5 小时前
AI 全栈指南:NestJs 中的 Service Provider 和 Module
前端·后端·面试
IT_陈寒5 小时前
为什么我的JavaScript异步回调总是乱序执行?
前端·人工智能·后端