Kros的博客 Kros的博客
首页
  • CSS
  • 工具
  • Vue
  • js
  • Vue3
  • 算法
  • 折腾笔记
一言
  • 分类
  • 标签
  • 归档
码云

Kros

凡心所向,素履以往,生如逆旅,一苇以航
首页
  • CSS
  • 工具
  • Vue
  • js
  • Vue3
  • 算法
  • 折腾笔记
一言
  • 分类
  • 标签
  • 归档
码云
  • 拼图小游戏

  • 井字棋

    • 简介
    • 普通模式
    • 进阶模式
  • 贪吃蛇

  • 扫雷

  • 项目
  • 井字棋
kros
2025-09-01

进阶模式

tictactoe-advance

在普通模式的基础上添加棋子数量限制即可,修改代码如下:

  • 定义数组缓存双方已下棋子,达到4颗时清除第一颗
  • 最先下的棋子添加动画标识
<script setup>
import { ref, computed } from 'vue'
const chessboard = ref([])
const player = ref('X')

const chessX = ref([])
const chessO = ref([])

const itemClass = computed(() => {
  return (item, idx) => {
    return { 
      'chesspiece--x': item === 'X', 
      'chesspiece--o': item === 'O', 
      'chesspiece--none': item === '',
      'chesspiece--x-anim': player.value === 'X' && item === 'X' && chessX.value.length == 3 && chessX.value[0] === idx,
      'chesspiece--o-anim': player.value === 'O' && item === 'O' && chessO.value.length == 3 && chessO.value[0] === idx,
    }
  }
})

// 落子
const playChess = (index) => {
  if (chessboard.value[index] === '') {
    chessboard.value[index] = player.value
    if (player.value === 'X') {
      chessX.value.push(index)
      if (chessX.value.length > 3) {
        const last = chessX.value.shift()
        chessboard.value[last] = ''
      }
    } else {
      chessO.value.push(index)
      if (chessO.value.length > 3) {
        const last = chessO.value.shift()
        chessboard.value[last] = ''
      }
    }
    player.value = player.value === 'X' ? 'O' : 'X'
    const winner = isWin()
    if (winner) {
      alert(`Player ${winner} wins!`)
      initBoard()
    }
  }
}

// 检查游戏是否结束
const isWin = () => {
  // 固定八种获胜组合
  const winningCombinations = [
    [0, 1, 2],
    [3, 4, 5],
    [6, 7, 8],
    [0, 3, 6],
    [1, 4, 7],
    [2, 5, 8],
    [0, 4, 8],
    [2, 4, 6]
  ]
  for (const combination of winningCombinations) {
    const [a, b, c] = combination
    if (chessboard.value[a] && chessboard.value[a] === chessboard.value[b] && chessboard.value[a] === chessboard.value[c]) {
      return chessboard.value[a]
    }
  }
  return false
}

// 初始化棋盘
const initBoard = () => {
  chessboard.value = Array(9).fill('')
  player.value = 'X'
}

initBoard()

</script>

<template>
  <div class="tictactoe">
    <div class="chessboard">
      <div
        v-for="(item, index) in chessboard"
        :key="index"
        class="chesspiece"
        :class="itemClass(item, index)"
        @click="playChess(index)"
        :data-player="player"
      >
        
      </div>
    </div>
  </div>
</template>

<style scoped lang="scss">
.tictactoe {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f5f5f5;
  .chessboard {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 10px;
    .chesspiece {
      display: flex;
      align-items: center;
      justify-content: center;
      width: 120px;
      height: 120px;
      border-radius: 10px;
      background: #78bec5;
      transition: all .3s;
      cursor: pointer;
      
      font-size: 70px;
      font-weight: bolder;
      color: white;
      text-shadow: 1px 1px 4px #ccc;
      &--none {
        &:hover {          
          background: #4f6d70;
        }
      }
      &--none[data-player="X"]:hover::after {
        content: '✖';
        color: rgb(201, 201, 201);
      }
      &--none[data-player="O"]:hover::after {
        content: '〇';
        color: rgb(201, 201, 201);
      }
      &--x {
        background: #DC685A;
        cursor: default;
        &::after {
          content: '✖';
          font-size: 70px;
          font-weight: bolder;
          color: white;
          text-shadow: 1px 1px 4px #ccc;
        }
      }
      &--o {
        background: #ECAF4F;
        cursor: default;
        &::after {
          content: '〇';
          font-size: 70px;
          font-weight: bolder;
          color: white;
          text-shadow: 1px 1px 4px #ccc;
        }
      }
      &--x-anim, &--o-anim {
        animation: chessAnim 1.5s ease-in-out infinite alternate;
      }
    }
  }
  .chessboard--x {
    .chesspiece {
      &:hover::after {
        content: '✖';
        font-size: 70px;
        font-weight: bolder;
        color: rgb(192, 192, 192);
      }
    }
  }
  .chessboard--o {
    .chesspiece {
      &:hover::after {
        content: '〇';
        font-size: 70px;
        font-weight: bolder;
        color: rgb(192, 192, 192);
      }
    }
  }
}

@keyframes chessAnim {
  0% {
    opacity: 0.3;
  }
  100% {
    opacity: 1;
  }
}
</style>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
上次更新: 2025/09/05, 8:09:00
普通模式
实现思考

← 普通模式 实现思考→

最近更新
01
Find the next perfect square
09-05
02
Regex validate PIN code
09-05
03
Find the odd int
09-05
更多文章>
Theme by Vdoing | Copyright © 2020-2025 kros king
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式
icon-heart-o icon-heart icon-infinity icon-pause icon-play link next prev