美文网首页
Vue井字棋

Vue井字棋

作者: kzc爱吃梨 | 来源:发表于2019-09-26 10:17 被阅读0次
  • v-on:click="onClidkCell(0, $event)v-on:click的绑定事件,有$event的接受数据。v-bind:n="n传参
  • props: ["n"], //获取外面的属性n
  • this.$emit("click", this.text); //告诉外面我被点了,并且传递了text参数
<template>
  <div class="wrapper">
    <div>第{{n}}手</div>
    <div class="chess">
      <div class="row">
        <!-- $event是接受cell发过来的参数this.text -->
        <Cell v-on:click="onClidkCell(0, $event)" v-bind:n="n" />  
        <Cell v-on:click="onClidkCell(1, $event)" v-bind:n="n" />
        <Cell v-on:click="onClidkCell(2, $event)" v-bind:n="n" />
      </div>
      <div class="row">
        <Cell v-on:click="onClidkCell(3, $event)" v-bind:n="n" />
        <Cell v-on:click="onClidkCell(4, $event)" v-bind:n="n" />
        <Cell v-on:click="onClidkCell(5, $event)" v-bind:n="n" />
      </div>
      <div class="row">
        <Cell v-on:click="onClidkCell(6, $event)" v-bind:n="n" />
        <Cell v-on:click="onClidkCell(7, $event)" v-bind:n="n" />
        <Cell v-on:click="onClidkCell(8, $event)" v-bind:n="n" />
      </div>
    </div>
    <!-- <div>{{map}}</div> -->
    <div>结果:{{result == null? '胜负未分' : `胜方为${result}` }}</div>
  </div>
</template>

<script>
import Cell from "./Cell";
export default {
  components: { Cell },
  data() {
    return {
      n: 0,
      result: null,
      map: [
        [null, null, null], //1. 声明一个二维数组
        [null, null, null], //2.在用户点击的时候获取用户点击的序号和内容,把对应的i的text填到数组里面
        [null, null, null] //3.调用tell判断谁胜谁负
      ]
    };
  },
  methods: {
    onClidkCell(i, text) {
      console.log(`${i}被点击了,内容是:${text}`); //知道那个元素别点了,而且被点的内容也知道了,用一个数组map存下来
      this.map[Math.floor(i / 3)][i % 3] = text;
      this.n = this.n + 1;
      this.tell();
    },
    tell() {
      const map = this.map;
      for (let i = 0; i < 2; i++) {
        if (
          map[i][0] !== null &&
          map[i][0] == map[i][1] &&
          map[i][1] == map[i][2]
        ) {
          this.result = map[i][0];
        }
      }
      for (let j = 0; j < 2; j++) {
        if (
          map[0][j] !== null &&
          map[0][j] == map[1][j] &&
          map[1][j] == map[2][j]
        ) {
          this.result = map[0][j];
        }
      }
      if (
        map[0][0] !== null &&
        map[0][0] == map[1][1] &&
        map[1][1] == map[2][2]
      ) {
        this.result = map[0][0];
      }
      if (
        map[0][2] !== null &&
        map[0][2] == map[1][1] &&
        map[1][1] == map[2][0]
      ) {
        this.result = map[0][2];
      }
    }
  }
};
</script>

<style>
.row {
  display: flex;
}
.wrapper {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
}
</style>

<template>
  <div>
    <!-- {{n}} 第几次被点-->
    <div id="cell" v-on:click="onClickSelf">
      <template v-if="a">{{text}}</template>
      <template v-else></template>
    </div>
  </div>
</template>

<script>
export default {
    props: ["n"], //获取外面的属性n
  data() {
    return { a: false, text: "" };
  },
  methods: {
    //函数写在methods里面
    onClickSelf() {
        if(this.text !== "") {
            return
        }
      this.a = true;
      this.text = this.n % 2 == 0? "x" : "o"
      this.$emit("click", this.text); //告诉外面我被点了,并且传递了text参数
    }
  }
};
</script>

<style>
#cell {
  color: #9a55a3;
  border: 1px solid black;
  height: 100px;
  width: 100px;
  font-size: 50px;
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>
image.png
  • APP可以向cell传递信息,但是cell不能和cell传递。cell只能向APP传递事件


    image.png

相关文章

  • Vue井字棋

    v-on:click="onClidkCell(0, $event)有v-on:click的绑定事件,有$even...

  • 井字棋

    package newPaca; import java.util.Scanner; public class j...

  • 井字棋源码

  • 设计井字棋

    1、前言 一般的系统设计题都有主体、input、output。但是游戏设计题可能有点违反常规,它主要有以下几点:玩...

  • C++实现的基于α-β剪枝算法的井字棋游戏

    一、井字棋游戏规则 “井字棋”游戏(又叫“三子棋”),是一款十分经典的益智小游戏,操作简单,娱乐性强。两个玩家,一...

  • 多维数组怎么学?看这一篇就够了,带你在游戏中学习。

    tic-tac-toe 游戏 (井字棋游戏) 大家小时候应该都玩过井字棋吧,下课之余和同学来上一把,是多么开心。今...

  • JavaScript新手练习——井字棋

    井字棋是一个很简单的游戏,通过穷举,我们轻易的记录井字棋所有可能的走法。因此我们的AI可以实现的能力是 先手:尽可...

  • R写的三子棋(井字棋)程序

    我家小朋友最近迷上了井字棋,每天都要拉着大人玩两盘,这两天恰好有闲,写个井字棋程序练练手,同时看看能否提起小朋友对...

  • 9.井字棋

    题目内容:嗯,就是视频里说的那个井字棋。视频里说了它的基本思路,现在,需要你把它全部实现出来啦。你的程序先要读入一...

  • Java 井字棋小结

    1.井字棋获胜的四种情况 横行全为同一符号竖行全为同一符号斜对角线为同一符号反对角线为同一符号 2.编程思路 构建...

网友评论

      本文标题:Vue井字棋

      本文链接:https://www.haomeiwen.com/subject/xobructx.html