当前位置:首页 > VUE

vue实现走棋盘

2026-02-19 03:55:52VUE

Vue 实现走棋盘功能

走棋盘功能通常用于棋盘类游戏或路径规划演示,以下是基于 Vue 3 的实现方案:

核心代码结构

<template>
  <div class="chessboard">
    <div v-for="(row, rowIndex) in board" :key="rowIndex" class="row">
      <div 
        v-for="(cell, colIndex) in row" 
        :key="colIndex"
        class="cell"
        :class="{ 
          'active': isActive(rowIndex, colIndex),
          'path': isInPath(rowIndex, colIndex)
        }"
        @click="moveTo(rowIndex, colIndex)"
      >
        {{ cell }}
      </div>
    </div>
  </div>
</template>

数据初始化

<script setup>
import { ref } from 'vue'

const boardSize = 8
const board = ref(Array(boardSize).fill().map(() => 
  Array(boardSize).fill(0)
))
const currentPosition = ref([0, 0])
const path = ref([])

// 初始化棋子位置
board.value[0][0] = '♟'
</script>

移动逻辑实现

const moveTo = (row, col) => {
  // 清除旧位置
  board.value[currentPosition.value[0]][currentPosition.value[1]] = 0

  // 更新新位置
  board.value[row][col] = '♟'
  currentPosition.value = [row, col]

  // 记录路径
  path.value.push([row, col])
}

const isActive = (row, col) => {
  return currentPosition.value[0] === row && 
         currentPosition.value[1] === col
}

const isInPath = (row, col) => {
  return path.value.some(pos => pos[0] === row && pos[1] === col)
}

样式设计

<style>
.chessboard {
  display: inline-block;
  border: 2px solid #333;
}

.row {
  display: flex;
}

.cell {
  width: 50px;
  height: 50px;
  border: 1px solid #ccc;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #fff;
}

.cell:nth-child(odd) {
  background-color: #f0d9b5;
}

.cell:nth-child(even) {
  background-color: #b58863;
}

.active {
  background-color: #ff0000 !important;
}

.path {
  background-color: #aaffaa !important;
}
</style>

高级功能扩展

1. 移动限制

const isValidMove = (row, col) => {
  const [currentRow, currentCol] = currentPosition.value
  const rowDiff = Math.abs(row - currentRow)
  const colDiff = Math.abs(col - currentCol)

  // 例如只允许走日字形
  return (rowDiff === 2 && colDiff === 1) || 
         (rowDiff === 1 && colDiff === 2)
}

const moveTo = (row, col) => {
  if (!isValidMove(row, col)) return
  // ...原有移动逻辑
}

2. 动画效果

.cell {
  transition: background-color 0.3s ease;
}

.move-animation {
  animation: move 0.5s ease;
}

@keyframes move {
  from { transform: scale(0.8); }
  to { transform: scale(1); }
}

3. 键盘控制

vue实现走棋盘

onMounted(() => {
  window.addEventListener('keydown', handleKeyPress)
})

onUnmounted(() => {
  window.removeEventListener('keydown', handleKeyPress)
})

const handleKeyPress = (e) => {
  const [row, col] = currentPosition.value
  switch(e.key) {
    case 'ArrowUp': 
      if(row > 0) moveTo(row - 1, col)
      break
    case 'ArrowDown':
      if(row < boardSize - 1) moveTo(row + 1, col)
      break
    // 其他方向键处理...
  }
}

完整组件示例

<template>
  <div>
    <div class="chessboard">
      <!-- 棋盘渲染 -->
    </div>
    <div class="controls">
      <button @click="reset">重置</button>
      <span>当前位置: ({{ currentPosition[0] }}, {{ currentPosition[1] }})</span>
    </div>
  </div>
</template>

<script setup>
// 组合式API代码
const reset = () => {
  board.value = Array(boardSize).fill().map(() => 
    Array(boardSize).fill(0)
  )
  board.value[0][0] = '♟'
  currentPosition.value = [0, 0]
  path.value = []
}
</script>

以上实现可根据具体需求进行调整,如改变棋盘大小、棋子样式或移动规则等。对于更复杂的棋盘游戏,可以结合Vuex或Pinia进行状态管理。

标签: 棋盘vue
分享给朋友:

相关文章

vue组件的实现

vue组件的实现

Vue 组件的实现方式 Vue 组件可以通过多种方式实现,以下是常见的几种方法: 单文件组件 (SFC) 使用 .vue 文件,包含模板、脚本和样式三部分: <template>…

vue实现菜单栏锚点

vue实现菜单栏锚点

实现锚点菜单的基本思路 在Vue中实现菜单栏锚点功能,主要涉及两个方面:创建可跳转的锚点位置,以及菜单项的点击滚动控制。通过监听滚动事件可以高亮当前可见区域的对应菜单项。 创建页面锚点位置 在需要跳…

vue实现div

vue实现div

Vue 实现 DIV 元素的方法 在 Vue 中实现 DIV 元素可以通过多种方式,以下是几种常见的实现方法: 使用模板语法 在 Vue 的模板中直接使用 HTML 的 <div> 标签…

简单实现vue

简单实现vue

Vue 的基本实现 创建一个简单的 Vue 应用需要引入 Vue 库,并编写基本的模板和数据绑定逻辑。 <!DOCTYPE html> <html lang="en"> &l…

vue实现标题

vue实现标题

Vue 实现标题的方法 在Vue中实现标题可以通过多种方式,以下是几种常见的方法: 方法一:使用模板语法 在Vue组件的模板中直接使用HTML的<h1>到<h6>标签来定义…

vue nexttick 实现

vue nexttick 实现

Vue nextTick 实现原理 Vue 的 nextTick 是一个用于在下次 DOM 更新循环结束之后执行延迟回调的方法。它主要用于确保在 DOM 更新完成后执行某些操作。 核心实现机制 ne…