五子棋html

复制代码
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>五子棋游戏</title>
<style>
  body {
    display: flex;
    flex-direction: column;
    align-items: center;
    background: #f0d9b5;
    font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
    margin: 0;
    padding: 20px;
  }
  h1 {
    margin-bottom: 10px;
  }
  #board {
    display: grid;
    grid-template-columns: repeat(15, 30px);
    grid-template-rows: repeat(15, 30px);
    gap: 1px;
    background-color: #333;
    border: 2px solid #333;
  }
  .cell {
    width: 30px;
    height: 30px;
    background-color: #f0d9b5;
    border-radius: 3px;
    cursor: pointer;
    position: relative;
  }
  .cell:hover {
    background-color: #e0cfa3;
  }
  .stone {
    position: absolute;
    top: 50%;
    left: 50%;
    width: 24px;
    height: 24px;
    border-radius: 50%;
    transform: translate(-50%, -50%);
  }
  .black {
    background-color: black;
  }
  .white {
    background-color: white;
    border: 1px solid black;
  }
  #scoreboard {
    margin-top: 20px;
    font-size: 18px;
  }
  #resetBtn {
    margin-top: 15px;
    padding: 8px 16px;
    font-size: 16px;
    cursor: pointer;
  }
</style>
</head>
<body>
  <h1>五子棋游戏</h1>
  <div id="board"></div>
  <div id="scoreboard">
    黑方胜利次数: <span id="blackWins">0</span> &nbsp;&nbsp; 白方胜利次数: <span id="whiteWins">0</span>
  </div>
  <button id="resetBtn">重新开始</button>

  <script>
    const boardSize = 15;
    const board = document.getElementById('board');
    const blackWinsSpan = document.getElementById('blackWins');
    const whiteWinsSpan = document.getElementById('whiteWins');
    const resetBtn = document.getElementById('resetBtn');

    let currentPlayer = 'black';
    let boardState = Array(boardSize).fill(null).map(() => Array(boardSize).fill(null));
    let blackWins = 0;
    let whiteWins = 0;
    let gameOver = false;

    function createBoard() {
      board.innerHTML = '';
      for (let row = 0; row < boardSize; row++) {
        for (let col = 0; col < boardSize; col++) {
          const cell = document.createElement('div');
          cell.classList.add('cell');
          cell.dataset.row = row;
          cell.dataset.col = col;
          cell.addEventListener('click', onCellClick);
          board.appendChild(cell);
        }
      }
    }

    function onCellClick(e) {
      if (gameOver) return;
      const row = parseInt(e.target.dataset.row);
      const col = parseInt(e.target.dataset.col);
      if (boardState[row][col] !== null) return;

      boardState[row][col] = currentPlayer;
      const stone = document.createElement('div');
      stone.classList.add('stone', currentPlayer);
      e.target.appendChild(stone);

      if (checkWin(row, col, currentPlayer)) {
        gameOver = true;
        if (currentPlayer === 'black') {
          blackWins++;
          blackWinsSpan.textContent = blackWins;
          setTimeout(() => alert('黑方获胜!'), 0);
        } else {
          whiteWins++;
          whiteWinsSpan.textContent = whiteWins;
          setTimeout(() => alert('白方获胜!'), 0);
        }
        return;
      }

      currentPlayer = currentPlayer === 'black' ? 'white' : 'black';
    }

    function checkWin(row, col, player) {
      return (
        checkDirection(row, col, player, 1, 0) || // 横向
        checkDirection(row, col, player, 0, 1) || // 纵向
        checkDirection(row, col, player, 1, 1) || // 斜向 \
        checkDirection(row, col, player, 1, -1)   // 斜向 /
      );
    }

    function checkDirection(row, col, player, deltaRow, deltaCol) {
      let count = 1;
      // 向一个方向计数
      let r = row + deltaRow;
      let c = col + deltaCol;
      while (r >= 0 && r < boardSize && c >= 0 && c < boardSize && boardState[r][c] === player) {
        count++;
        r += deltaRow;
        c += deltaCol;
      }
      // 向相反方向计数
      r = row - deltaRow;
      c = col - deltaCol;
      while (r >= 0 && r < boardSize && c >= 0 && c < boardSize && boardState[r][c] === player) {
        count++;
        r -= deltaRow;
        c -= deltaCol;
      }
      return count >= 5;
    }

    resetBtn.addEventListener('click', () => {
      gameOver = false;
      boardState = Array(boardSize).fill(null).map(() => Array(boardSize).fill(null));
      currentPlayer = 'black';
      createBoard();
    });

    createBoard();
  </script>
</body>
</html>
相关推荐
小叶lr2 分钟前
jenkins打包前端样式丢失/与本地不一致问题
运维·前端·jenkins
浩星8 分钟前
electron系列1:Electron不是玩具,为什么桌面应用需要它?
前端·javascript·electron
ZC跨境爬虫26 分钟前
Scrapy工作空间搭建与目录结构解析:从初始化到基础配置全流程
前端·爬虫·python·scrapy·自动化
小村儿29 分钟前
连载04-最重要的Skill---一起吃透 Claude Code,告别 AI coding 迷茫
前端·后端·ai编程
_院长大人_1 小时前
Vue + ECharts 实现价格趋势分析图
前端·vue.js·echarts
IT_陈寒1 小时前
Vite的alias配置把我整不会了,原来是这个坑
前端·人工智能·后端
万物得其道者成2 小时前
Cursor 提效实战:我的前端 Prompt、审查 SKILL、MCP 接入完整方法
前端·prompt
自然 醒2 小时前
uni-app开发微信小程序,如何使用towxml去渲染md格式和html标签格式的内容?
微信小程序·uni-app·html
酒鼎2 小时前
学习笔记(12-02)事件循环 - 实战案例 —⭐
前端·javascript
Bigger2 小时前
第一章:我是如何剖析 Claude Code 整体架构与启动流程的
前端·aigc·claude