vue做一个井字棋

问题:

  • v-on绑定多个事件
  • 结束条件判断
  • 数组的响应式

v-on绑定多个事件的语法

相同的事件这样写
@click=“fun1(),fun2()”

不同的事件写多个v-on
@mouseenter=“in()” @mouseleave="out()"

结束条件判断

暂时没想到什么好方法,先穷举

要求存在一行或一列或对角线的三个符号相同且不是空就结束,一共存在8中情况

(this.each[0].value==this.each[3].value && this.each[3].value==this.each[6].value && this.each[0].value != '' && this.each[3].value != '' && this.each[6].value != '')||

(this.each[1].value==this.each[4].value && this.each[4].value==this.each[7].value && this.each[1].value != '' && this.each[4].value != '' && this.each[7].value != '')||

(this.each[2].value==this.each[5].value && this.each[5].value==this.each[8].value && this.each[2].value != '' && this.each[5].value != '' && this.each[8].value != '')||

(this.each[0].value==this.each[1].value && this.each[1].value==this.each[2].value && this.each[0].value != '' && this.each[1].value != '' && this.each[2].value != '')||

(this.each[3].value==this.each[4].value && this.each[4].value==this.each[5].value && this.each[3].value != '' && this.each[4].value != '' && this.each[5].value != '')||

(this.each[6].value==this.each[7].value && this.each[7].value==this.each[8].value && this.each[6].value != '' && this.each[7].value != '' && this.each[8].value != '')||

(this.each[0].value==this.each[4].value && this.each[4].value==this.each[8].value && this.each[0].value != '' && this.each[4].value != '' && this.each[8].value != '')||

(this.each[2].value==this.each[4].value && this.each[4].value==this.each[6].value && this.each[2].value != '' && this.each[4].value != '' && this.each[6].value != '')

 数组的响应式

修改已有数组的元素的内容是响应式的

直接修改数组长度和修改数组元素不是响应式的

需要使用以下方法

pop()删除数组尾部的元素

push()在数组尾部添加元素

shift()删除数组的第一个元素

unshift()在数组最前面添加元素

splice(‘开始的索引’,‘元素数量’,‘要替换为的内容’)

Vue.set('要修改的对象','索引值',''修改后的值 )

完整代码

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>购物车</title>
  <script src="../环境/vue.min.js"></script>
  <style>
    *{
      padding: 0px;
      margin: 0px;
      box-sizing: border-box;
      list-style: none;
    }
    body{
      width: 100vw;
      height: 100vh;
      display: flex;
      justify-content: center;
      align-items: center;
    }
    .box{
      width: 40vw;
      height: 40vw;
      border: 1px solid black;
      display: flex;
      flex-direction: column;
    }

    .box1{
      display: flex;
      justify-content: center;
      font-size: 40px;
    }

    .box2 ul{
      display: flex;
      flex-wrap: wrap;
      justify-content: center;
      align-items: center;
    }
    .box2 ul li{
      width: 10vw;
      height: 10vw;
      background-color: aqua;
      margin: 10px;
      display: flex;
      justify-content: center;
      align-items: center;
      font-size: 30px;
    }

    .box2 ul li:hover{
      border: 1px solid red;
    }
  </style>
</head>
<body>
  <div class="box">
    <div class="box1">井字棋</div>
    <div class="box2">
      <ul>
        <li v-for="(item,index) in each" @click="add(index);over()">{{item.value}}</li>
      </ul>
    </div>
  </div>
  <script>
    new Vue({
      el:'.box',
      data:{
        each:[
          {isOK:false, value:''},
          {isOK:false, value:''},
          {isOK:false, value:''},
          {isOK:false, value:''},
          {isOK:false, value:''},
          {isOK:false, value:''},
          {isOK:false, value:''},
          {isOK:false, value:''},
          {isOK:false, value:''},
        ],
        flag:true,
      },
      methods: {
        add(index){
          if(this.each[index].isOK == false){
            if(this.flag){
              this.each[index].value = 'o';
            }
            else{
              this.each[index].value = 'v';
            }
            this.flag = !this.flag;
            this.each[index].isOK = 'true';
          }
        },
        over(){
          if((this.each[0].value==this.each[3].value && this.each[3].value==this.each[6].value && this.each[0].value != '' && this.each[3].value != '' && this.each[6].value != '')||
             (this.each[1].value==this.each[4].value && this.each[4].value==this.each[7].value && this.each[1].value != '' && this.each[4].value != '' && this.each[7].value != '')||
             (this.each[2].value==this.each[5].value && this.each[5].value==this.each[8].value && this.each[2].value != '' && this.each[5].value != '' && this.each[8].value != '')||
             (this.each[0].value==this.each[1].value && this.each[1].value==this.each[2].value && this.each[0].value != '' && this.each[1].value != '' && this.each[2].value != '')||
             (this.each[3].value==this.each[4].value && this.each[4].value==this.each[5].value && this.each[3].value != '' && this.each[4].value != '' && this.each[5].value != '')||
             (this.each[6].value==this.each[7].value && this.each[7].value==this.each[8].value && this.each[6].value != '' && this.each[7].value != '' && this.each[8].value != '')||
             (this.each[0].value==this.each[4].value && this.each[4].value==this.each[8].value && this.each[0].value != '' && this.each[4].value != '' && this.each[8].value != '')||
             (this.each[2].value==this.each[4].value && this.each[4].value==this.each[6].value && this.each[2].value != '' && this.each[4].value != '' && this.each[6].value != '')){
              alert('game over');
          }
        }
      }
    })
  </script>
</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值