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

Kros

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

  • 井字棋

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

  • 扫雷

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

普通模式

tictactoe

  • 定义长度为9的数组模拟棋盘
  • 固定判赢路径:[0, 1, 2],[3, 4, 5],[6, 7, 8],[0, 3, 6],[1, 4, 7],[2, 5, 8],[0, 4, 8],[2, 4, 6],数字为数组下标
<script setup>
import { ref } from 'vue'
const chessboard = ref([])
const player = ref('X')

// 落子
const playChess = (index) => {
  if (chessboard.value[index] === '') {
    chessboard.value[index] = player.value
    player.value = player.value === 'X' ? 'O' : 'X'
    const winner = isWin()
    if (winner) {
      alert(`Player ${winner} wins!`)
      initBoard()
    } else if(isEnd()) {
      alert('平局')
      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 isEnd = () => {
  return chessboard.value.every(v => v !== '')
}

// 初始化棋盘
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="{ 'chesspiece--x': item === 'X', 'chesspiece--o': item === 'O', 'chesspiece--none': item === '' }"
        @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;
        }
      }
    }
  }
  .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);
      }
    }
  }
}
</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
上次更新: 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