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异步组件使用及加载失败异常处理

    vue异步组件使用及加载失败异常处理

    在构建大型单页应用时,组件的按需加载和延迟加载对于性能优化至关重要,本文主要介绍了vue异步组件使用及加载失败异常处理,具有一定的参考价值,感兴趣的可以了解一下
    2024-02-02
  • vite项目配置less全局样式的实现步骤

    vite项目配置less全局样式的实现步骤

    最近想实现个项目,需要配置全局less,本文主要介绍了vite项目配置less全局样式的实现步骤,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习吧
    2024-02-02
  • Vue中父组件向子组件传递数据的几种方法

    Vue中父组件向子组件传递数据的几种方法

    最近在学习vue的源码,总结了几种vue中父子组件传递数据的方法。具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-05-05
  • Vue3编程流畅技巧使用setup语法糖拒绝写return

    Vue3编程流畅技巧使用setup语法糖拒绝写return

    这篇文章主要为大家介绍了Vue3编程流畅技巧使用setup语法糖拒绝写return的方法详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-07-07
  • Vue.js组件实现选项卡以及切换特效

    Vue.js组件实现选项卡以及切换特效

    这篇文章主要为大家详细介绍了Vue.js组件实现选项卡以及切换特效,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-07-07
  • Vue+WebSocket页面实时刷新长连接的实现

    Vue+WebSocket页面实时刷新长连接的实现

    最近vue项目要做数据实时刷新,数据较大,会出现卡死情况,所以本文主要介绍了页面实时刷新长连接,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-06-06
  • Vue3.x使用mitt.js进行组件通信

    Vue3.x使用mitt.js进行组件通信

    Vue2.x 使用 EventBus 进行组件通信,而 Vue3.x 推荐使用 mitt.js。本文就介绍一下mitt.js的具体使用方法,感兴趣的可以了解一下
    2021-06-06
  • vue3 diff 算法示例

    vue3 diff 算法示例

    这篇文章主要为大家介绍了vue3 diff 的算法示例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-07-07
  • 详解Vue.js Mixins 混入使用

    详解Vue.js Mixins 混入使用

    本篇文章主要介绍了Vue.js Mixins 混入使用,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-09-09
  • vue实现pdf文件发送到邮箱功能的示例代码

    vue实现pdf文件发送到邮箱功能的示例代码

    这篇文章主要介绍了vue实现pdf文件发送到邮箱功能,实现代码包括对邮箱格式内容是否为空的验证方法,代码简单易懂,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-05-05

最新评论