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 小时前
URL.createObjectURL 与 FileReader:Web 文件处理两大法宝的对比
前端·javascript
赵广陆2 小时前
SprinBoot+Vue宠物寄养系统的设计与实现
前端·vue.js·宠物
A黄俊辉A3 小时前
vue3中把封装svg图标为全局组件
前端·javascript·vue.js
老贾爱编程3 小时前
VUE实现刻度尺进度条
前端·javascript·vue.js
F2E_Zhangmo3 小时前
vue如何做到计算属性传参?
前端·javascript·vue.js
繁依Fanyi4 小时前
828华为云征文|华为Flexus云服务器搭建OnlyOffice私有化在线办公套件
服务器·开发语言·前端·python·算法·华为·华为云
叫我小鹏呀4 小时前
vue3中el-table中点击图片放大时,被表格覆盖
前端·javascript·vue.js
我命由我123454 小时前
2.使用 VSCode 过程中的英语积累 - Edit 菜单(每一次重点积累 5 个单词)
前端·javascript·ide·vscode·学习·编辑器·学习方法
四季予你664 小时前
vue2 和 vue3 的区别
前端·javascript·vue.js
炒毛豆4 小时前
vue3+ant design vue实现可编辑表格弹出气泡弹出窗~
前端·javascript·vue.js