vue实现井字棋游戏

 更新时间:2020年09月29日 16:28:34   作者:知我未夠好丶  
这篇文章主要为大家详细介绍了vue实现井字棋游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了vue实现井字棋游戏的具体代码,供大家参考,具体内容如下

之前看react的教程时看到的小游戏,试着用vue做一个。右边的winner提示胜者,还没有胜者时提示下一个棋子的种类。restart按钮点击可重新开始。go to step可跳转到第n步。

html:

<div id="app">
 <ul id="board" class="white normal">
 <li class="square" v-for="i, idx in datas" @click=set(idx)>{{i}}</li>
 </ul>
 <div id="console">
 <div id="hint" class="white">{{hint}}</div>
 <input type="button" class="white" id="restart" value="restart" @click="init()"/>
 <ul id="history" class="normal">
  <li class="history" v-for="i, idx in history">
  <input type="button" class="white" :value="'go to step' + (idx + 1)" @click=jump(idx) />
  </li>
 </ul>
 </div>
</div>

css:

<style type="text/css">
 body {
 background: #5af;
 }
 
 .white {
 background: #fff;
 border-radius: 11px;
 outline: none;
 border: none;
 }
 
 .normal {
 list-style: none;
 padding: 0px;
 margin: 0px;
 }
 
 #app {
 display: flex;
 justify-content: space-between;
 width: 450px;
 height: 306px;
 position: absolute;
 top: 0;
 bottom: 0;
 left: 0;
 right: 0;
 margin: auto;
 }
 
 #board {
 display: flex;
 width: 306px;
 height: 306px;
 flex-wrap: wrap;
 overflow: hidden;
 }
 
 #hint {
 width: 100px;
 height: 22px;
 text-align: center;
 margin: 10px;
 }
 
 #restart {
 width: 70px;
 height: 22px;
 margin: 10px;
 }
 
 #history,
 .history {
 margin: 5px;
 }
 
 .square {
 height: 100px;
 width: 100px;
 border: #ebebeb solid 1px;
 flex: 0 0 auto;
 font-size: 50px;
 font-weight: 900;
 line-height: 100px;
 text-align: center;
 }
</style>

js:

new Vue({
 el: '#app',
 data: {
 datas: Array(9).fill(''),
 history: [],
 next: true,
 winner: '',
 cases: [
  [0, 1, 2],
  [3, 4, 5],
  [6, 7, 8],
  [0, 3, 6],
  [1, 4, 7],
  [2, 5, 8],
  [0, 4, 8],
  [2, 4, 6],
 ]
 },
 methods: {
 //放置棋子
 set(idx) {
  if (!this.datas[idx] && !this.winner) {
  this.$set(this.datas, idx, this.next_player);
  this.history.push({
   status: [...this.datas],
   player: this.next
  });
  if (this.is_win(this.next_player)) {
   this.winner = this.next_player;
  }
  this.next = !this.next;
  }
 },
 //跳转到第n步
 jump(idx) {
  this.datas = this.history[idx].status;
  this.history.splice(idx + 1, this.history.length - idx - 1);
  this.next = !this.history[idx].player;
  this.winner = this.is_win('O') 
  ? 'O' 
  : this.is_win('X') 
   ? 'X' 
   : '';
 },
 //判断是否胜出
 is_win(player) {
  return this.cases.some(arr => arr.every(el => this.datas[el] === player));
 },
 //初始化
 init() {
  this.datas = Array(9).fill('');
  this.history = [];
  this.next = true;
  this.winner = '';
 }
 },
 computed: {
 next_player() {
  return this.next ? 'O' : 'X';
 },
 hint() {
  return this.winner ? 'winner: ' + this.winner : 'next: ' + this.next_player;
 }
 }
})

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

您可能感兴趣的文章:

相关文章

  • vue项目打包:修改dist文件名方式

    vue项目打包:修改dist文件名方式

    这篇文章主要介绍了vue项目打包:修改dist文件名方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-12-12
  • Vue生命周期实例分析总结

    Vue生命周期实例分析总结

    Vue的生命周期就是vue实例从创建到销毁的全过程,也就是new Vue()开始就是vue生命周期的开始。Vue实例有⼀个完整的⽣命周期,也就是从开始创建、初始化数据、编译模版、挂载Dom->渲染、更新->渲染、卸载等⼀系列过程,称这是Vue的⽣命周期
    2022-10-10
  • Vue实现省市区级联下拉选择框

    Vue实现省市区级联下拉选择框

    这篇文章主要为大家详细介绍了Vue实现省市区级联下拉选择框,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-03-03
  • vue3中vue.config.js配置及注释详解

    vue3中vue.config.js配置及注释详解

    在Vue 3.0中,与2.0版本相比有一定的差别,最明显的就是缺少了build、config文件夹,下面这篇文章主要给大家介绍了关于vue3中vue.config.js配置及注释的相关资料,需要的朋友可以参考下
    2022-08-08
  • element-ui 上传图片后清空图片显示的实例

    element-ui 上传图片后清空图片显示的实例

    今天小编就为大家分享一篇element-ui 上传图片后清空图片显示的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-09-09
  • Vue监听属性和计算属性

    Vue监听属性和计算属性

    这篇文章主要介绍了Vue监听属性和计算属性,基本用法添加watch属性,值为一个对象。对象的属性名就是要监视的数据,属性值为回调函数,每当这个属性名对应的值发生变化,就会触发该回调函数执行,下面来看详细内容,需要的朋友也可以参考一下
    2021-12-12
  • table表格中使用el-popover 无效问题解决方法

    table表格中使用el-popover 无效问题解决方法

    这篇文章主要介绍了table表格中使用el-popover 无效问题解决方法,实例只针对单个的按钮管用在表格里每一列都有el-popover相当于是v-for遍历了 所以我们在触发按钮的时候并不是单个的触发某一个,需要的朋友可以参考下
    2024-01-01
  • vue 表单之通过v-model绑定单选按钮radio

    vue 表单之通过v-model绑定单选按钮radio

    这篇文章主要介绍了vue 表单之v-model绑定单选按钮radio的实例代码,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-05-05
  • 详解VueJS应用中管理用户权限

    详解VueJS应用中管理用户权限

    本篇文章主要给大家讲述了VueJS应用中管理用户权限的详细过程和方法,以及相关的代码展示,需要的朋友参考下吧。
    2018-02-02
  • vue下canvas裁剪图片实例讲解

    vue下canvas裁剪图片实例讲解

    在本篇文章里小编给大家整理了关于vue下canvas裁剪图片实例讲解内容,需要的朋友们可以参考下。
    2020-04-04

最新评论