当前位置:首页 > VUE

Vue的扫雷实现

2026-01-19 04:33:42VUE

Vue 扫雷实现步骤

准备工作
确保已安装 Vue CLI 或通过 CDN 引入 Vue。创建一个 Vue 项目或单文件组件,初始化游戏所需的数据结构(如雷区矩阵、雷数量、游戏状态等)。

数据结构设计
使用二维数组表示雷区,每个格子包含属性:isMine(是否为雷)、revealed(是否揭开)、flagged(是否标记)、adjacentMines(周围雷数)。例如:

data() {
  return {
    grid: Array(10).fill().map(() => 
      Array(10).fill().map(() => ({
        isMine: false,
        revealed: false,
        flagged: false,
        adjacentMines: 0
      }))
    ),
    mineCount: 15,
    gameOver: false
  };
}

初始化雷区
随机布置雷并计算周围雷数。通过双重循环和随机数生成雷的位置,避免重复。初始化后遍历非雷格子,统计周围8个方向的雷数:

Vue的扫雷实现

methods: {
  initGrid() {
    // 布置雷
    let minesPlaced = 0;
    while (minesPlaced < this.mineCount) {
      const x = Math.floor(Math.random() * 10);
      const y = Math.floor(Math.random() * 10);
      if (!this.grid[x][y].isMine) {
        this.grid[x][y].isMine = true;
        minesPlaced++;
      }
    }
    // 计算周围雷数
    for (let x = 0; x < 10; x++) {
      for (let y = 0; y < 10; y++) {
        if (!this.grid[x][y].isMine) {
          this.grid[x][y].adjacentMines = this.countAdjacentMines(x, y);
        }
      }
    }
  },
  countAdjacentMines(x, y) {
    let count = 0;
    for (let dx = -1; dx <= 1; dx++) {
      for (let dy = -1; dy <= 1; dy++) {
        const nx = x + dx, ny = y + dy;
        if (nx >= 0 && nx < 10 && ny >= 0 && ny < 10 && this.grid[nx][ny].isMine) {
          count++;
        }
      }
    }
    return count;
  }
}

游戏交互逻辑
实现左键揭开格子和右键标记功能。揭开时递归处理空白区域(类似 Flood Fill 算法),遇到雷则结束游戏:

methods: {
  revealCell(x, y) {
    if (this.gameOver || this.grid[x][y].revealed || this.grid[x][y].flagged) return;
    this.grid[x][y].revealed = true;
    if (this.grid[x][y].isMine) {
      this.gameOver = true;
      alert('游戏结束');
      return;
    }
    if (this.grid[x][y].adjacentMines === 0) {
      this.revealAdjacentCells(x, y); // 递归揭开周围空白格子
    }
  },
  toggleFlag(x, y) {
    if (!this.grid[x][y].revealed) {
      this.grid[x][y].flagged = !this.grid[x][y].flagged;
    }
  }
}

界面渲染
使用 v-for 渲染雷区,绑定点击事件和右键事件。根据格子状态显示不同内容(数字、雷、旗帜):

Vue的扫雷实现

<template>
  <div class="grid">
    <div v-for="(row, x) in grid" :key="x" class="row">
      <div 
        v-for="(cell, y) in row" 
        :key="y" 
        class="cell"
        :class="{ revealed: cell.revealed, mine: cell.isMine && cell.revealed }"
        @click="revealCell(x, y)"
        @contextmenu.prevent="toggleFlag(x, y)"
      >
        <span v-if="cell.revealed">
          {{ cell.isMine ? '💣' : cell.adjacentMines || '' }}
        </span>
        <span v-else-if="cell.flagged">🚩</span>
      </div>
    </div>
  </div>
</template>

样式设计
添加基础样式控制格子外观和游戏状态反馈。例如:

.grid {
  display: inline-block;
  border: 2px solid #333;
}
.row {
  display: flex;
}
.cell {
  width: 30px;
  height: 30px;
  border: 1px solid #ccc;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  background-color: #eee;
}
.cell.revealed {
  background-color: #fff;
}
.cell.mine {
  background-color: #f00;
}

游戏状态检查
添加胜利条件判断(所有非雷格子被揭开):

methods: {
  checkWin() {
    return this.grid.every(row => 
      row.every(cell => 
        cell.revealed || cell.isMine
      )
    );
  }
}

revealCell 方法中调用此方法,若返回 true 则触发胜利逻辑。

标签: Vue
分享给朋友:

相关文章

用Vue实现

用Vue实现

以下是使用 Vue 实现常见功能的几种方法: 数据绑定与响应式更新 使用 v-model 实现双向数据绑定: <template> <input v-model="messa…

Vue论坛实现

Vue论坛实现

Vue论坛实现步骤 项目初始化 使用Vue CLI或Vite创建Vue 3项目。安装必要依赖如vue-router、axios、pinia/vuex。 npm create vue@latest…

Vue实现ping

Vue实现ping

Vue实现ping功能的方法 在Vue中实现ping功能,可以通过调用操作系统的ping命令或使用JavaScript的WebSocket等方式。以下是几种常见的方法: 使用Node.js的chi…

Vue界面实现

Vue界面实现

Vue 界面实现基础 Vue.js 是一个渐进式 JavaScript 框架,用于构建用户界面。其核心思想是数据驱动和组件化开发,适合构建单页应用(SPA)或复杂的前端界面。 环境准备 确保已安装…

Vue实现画布

Vue实现画布

Vue 实现画布的方法 在 Vue 中实现画布功能可以通过 HTML5 的 <canvas> 元素结合 Vue 的响应式特性来完成。以下是几种常见的实现方式: 使用原生 <canv…

Vue 实现登出

Vue 实现登出

Vue 实现登出的方法 在 Vue 应用中实现登出功能通常涉及清除用户认证信息、重定向到登录页面或主页,并可能调用后端 API 注销会话。以下是几种常见的实现方式: 清除本地存储的认证信息 大多数…