vue3项目 / 三子棋

参考: https://blog.csdn.net/iron_nini/article/details/130369453

App.vue

javascript 复制代码
<template>
  <div class="box">
    <div style="margin-bottom: 10px; font-size: 32px">{{ status }}</div>
    <div class="board-row">
      <squareComponent
        v-for="(item, index) in squareValues"
        :key="index"
        :text="item"
        @on-click="handleClick(index)"/>
    </div>
    <button id="again"
    @click="handleAgain"
    >Again</button>
  </div>
</template>

<script setup>
import { ref, watchEffect } from 'vue';
import squareComponent from './squareComponent';

const squareValues = ref(Array(9).fill(''));
const xIsNext = ref(true);
const status = ref('');


const calculateWinner = (squares) => {
  const lines = [
    [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(let i = 0; i < lines.length; i++) {
    const [a, b, c] = lines[i];
    if(squares[a] && squares[a] === squares[b] && squares[a] === squares[c]) {
      return squares[a];
    }
  }

  return null;
}

watchEffect(() => {
  if(calculateWinner(squareValues.value)){
    status.value = 'Winner: ' + calculateWinner(squareValues.value) + ' ! ';
  }
  else if(!squareValues.value.filter(item => item === '').length){
    status.value = 'Draw !';
  }
  else{
    status.value = 'Next player is ' + (xIsNext.value ? 'X' : 'O');
  }
});

const handleClick = (index) => {
  if(squareValues[index] || calculateWinner(squareValues.value)) {
    return;
  }

  if(xIsNext.value) {
    squareValues.value[index] = 'X';
  }
  else {
    squareValues.value[index] = 'O';
  }

  xIsNext.value = !xIsNext.value;
}

const handleAgain = () => {
  squareValues.value = Array(9).fill('');
  xIsNext.value = true;
}
</script>

<style scoped>
.box {
  width: 100%;
  height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: lightgreen;
  
}
.board-row {
    display: grid;
    grid-template-columns: repeat(3, minmax(0,1fr));
}
#again {
  font-size: 32px;
  font-family:'Times New Roman', Times, serif;
  margin-top: 20px;
}
</style>

squareComponent.vue

javascript 复制代码
<template>
  <button class="square" @click="handleClick">{{ text }}</button>
</template>

<script>
export default{
  name: 'squareComponent',
  props:{
    text:{
      type: String,
      default: ''
    }
  },
  emits: ['on-click'],

  setup(props, context){
    const handleClick = () =>{
      context.emit('on-click', props.text);
    };

    return {
      handleClick
    };
  },
};
</script>

<style scoped lang="less">
.square{
  box-sizing: border-box;
  width: 100px;
  height: 100px;
  margin: 10px;
  border-radius: 50%;
  background: pink;
  color: black;
  font-weight: 700;
  font-family: 'Times New Roman', Times, serif;
  font-size: 32px;
}
</style>

效果预览



相关推荐
打不着的大喇叭2 分钟前
uniapp的光标跟随和打字机效果
前端·javascript·uni-app
无我Code7 分钟前
2025----前端个人年中总结
前端·年终总结·创业
程序猿阿伟10 分钟前
《前端路由重构:解锁多语言交互的底层逻辑》
前端·重构
Sun_light26 分钟前
6个你必须掌握的「React Hooks」实用技巧✨
前端·javascript·react.js
爱学习的茄子28 分钟前
深度解析JavaScript中的call方法实现:从原理到手写实现的完整指南
前端·javascript·面试
莫空000029 分钟前
Vue组件通信方式详解
前端·面试
呆呆的心29 分钟前
揭秘 CSS 伪元素:不用加标签也能玩转出花的界面技巧 ✨
前端·css·html
susnm33 分钟前
Dioxus 与数据库协作
前端·rust
优雅永不过时_v38 分钟前
基于vite适用于 vue和 react 的Three.js低代码与Ai结合编辑器
前端·javascript
小皮侠39 分钟前
nginx的使用
java·运维·服务器·前端·git·nginx·github