当前位置:首页 > VUE

数字拼图vue实现

2026-01-18 15:31:00VUE

数字拼图游戏的基本原理

数字拼图通常由N×N的方格组成,其中包含1到N²-1的数字和一个空白格。玩家通过移动数字块与空白格交换位置,最终将所有数字按顺序排列完成拼图。

Vue实现的核心步骤

初始化游戏状态

使用Vue的data属性存储游戏状态,包括拼图数组、空白格位置和游戏尺寸。

data() {
  return {
    puzzle: [],         // 拼图数组
    emptyPos: { x: 0, y: 0 },  // 空白格坐标
    size: 3             // 默认3x3
  };
}

生成随机拼图

通过Fisher-Yates洗牌算法打乱初始有序数组,确保拼图有解。

数字拼图vue实现

methods: {
  shuffleArray() {
    const total = this.size * this.size - 1;
    let arr = Array.from({ length: total }, (_, i) => i + 1);
    for (let i = arr.length - 1; i > 0; i--) {
      const j = Math.floor(Math.random() * (i + 1));
      [arr[i], arr[j]] = [arr[j], arr[i]];
    }
    arr.push(0); // 0表示空白格
    this.puzzle = this.chunkArray(arr, this.size);
  },
  chunkArray(arr, chunkSize) {
    return Array.from({ length: chunkSize }, (_, i) =>
      arr.slice(i * chunkSize, (i + 1) * chunkSize)
    );
  }
}

处理玩家移动

监听点击事件,判断点击的数字块是否与空白格相邻,若相邻则交换位置。

handleClick(row, col) {
  const { x, y } = this.emptyPos;
  const isAdjacent = (Math.abs(row - x) === 1 && col === y) || 
                     (Math.abs(col - y) === 1 && row === x);
  if (isAdjacent) {
    [this.puzzle[x][y], this.puzzle[row][col]] = 
      [this.puzzle[row][col], this.puzzle[x][y]];
    this.emptyPos = { x: row, y: col };
    this.checkWin();
  }
}

胜利条件检测

遍历拼图数组,检查数字是否按顺序排列且空白格在末尾。

数字拼图vue实现

checkWin() {
  const flatPuzzle = this.puzzle.flat();
  for (let i = 0; i < flatPuzzle.length - 1; i++) {
    if (flatPuzzle[i] !== i + 1) return;
  }
  alert('Congratulations! You solved the puzzle!');
}

模板渲染

使用v-for动态渲染拼图格子,并通过CSS控制布局。

<template>
  <div class="puzzle-container">
    <div v-for="(row, i) in puzzle" :key="i" class="puzzle-row">
      <div 
        v-for="(num, j) in row" 
        :key="j" 
        class="puzzle-cell"
        @click="handleClick(i, j)"
      >
        {{ num !== 0 ? num : '' }}
      </div>
    </div>
  </div>
</template>

CSS样式

定义网格布局和交互效果。

.puzzle-container {
  display: inline-block;
  border: 2px solid #333;
}
.puzzle-row {
  display: flex;
}
.puzzle-cell {
  width: 60px;
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #f0f0f0;
  margin: 2px;
  cursor: pointer;
  font-size: 20px;
}
.puzzle-cell:hover {
  background-color: #ddd;
}

扩展功能建议

  • 难度调整:通过size参数支持4x4或5x5拼图。
  • 动画效果:使用Vue的过渡动画优化移动效果。
  • 计时功能:记录玩家完成拼图的时间。
  • 本地存储:通过localStorage保存最佳成绩。

标签: 拼图数字
分享给朋友:

相关文章

vue 实现数字跳动

vue 实现数字跳动

Vue 实现数字跳动效果 数字跳动效果可以通过 Vue 的过渡动画或第三方库实现。以下是几种常见方法: 使用 Vue Transition 和 CSS 动画 通过 Vue 的 <transi…

vue 实现数字求和

vue 实现数字求和

Vue 实现数字求和的方法 在 Vue 中实现数字求和可以通过多种方式完成,以下是几种常见的实现方法: 使用计算属性(Computed) 计算属性适合处理响应式数据的求和逻辑,当依赖的数据变化时,会…

vue实现数字递增滚动

vue实现数字递增滚动

Vue 实现数字递增滚动效果 在 Vue 中实现数字递增滚动效果可以通过多种方式完成,以下提供两种常见方法: 使用定时器和计算属性 <template> <div>{{…

vue实现数字动态滚动

vue实现数字动态滚动

数字动态滚动实现方法 在Vue中实现数字动态滚动效果,可以通过以下方式完成: 使用transition组件结合CSS动画 通过Vue的transition组件和CSS的transition或anim…

vue实现数字过渡动画

vue实现数字过渡动画

Vue 数字过渡动画实现方法 使用 transition 和动态数据绑定 通过 Vue 的 transition 组件结合数据绑定实现数字变化时的过渡效果。定义数字变量,使用计算属性或方法动态更新数值…

vue-cli实现数字

vue-cli实现数字

使用 Vue-CLI 实现数字功能 在 Vue-CLI 项目中实现数字功能可以通过多种方式完成,包括数据绑定、计算属性、方法调用等。以下是几种常见的实现方法: 数据绑定实现数字显示 在 Vue 组件…