欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue實現(xiàn)井字棋游戲

 更新時間:2020年09月29日 16:28:34   作者:知我未夠好丶  
這篇文章主要為大家詳細介紹了vue實現(xiàn)井字棋游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了vue實現(xiàn)井字棋游戲的具體代碼,供大家參考,具體內(nèi)容如下

之前看react的教程時看到的小游戲,試著用vue做一個。右邊的winner提示勝者,還沒有勝者時提示下一個棋子的種類。restart按鈕點擊可重新開始。go to step可跳轉(zhuǎn)到第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;
  }
 },
 //跳轉(zhuǎn)到第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;
 }
 }
})

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

您可能感興趣的文章:

相關(guān)文章

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

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

    這篇文章主要介紹了vue項目打包:修改dist文件名方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • Vue生命周期實例分析總結(jié)

    Vue生命周期實例分析總結(jié)

    Vue的生命周期就是vue實例從創(chuàng)建到銷毀的全過程,也就是new Vue()開始就是vue生命周期的開始。Vue實例有?個完整的?命周期,也就是從開始創(chuàng)建、初始化數(shù)據(jù)、編譯模版、掛載Dom->渲染、更新->渲染、卸載等?系列過程,稱這是Vue的?命周期
    2022-10-10
  • Vue實現(xiàn)省市區(qū)級聯(lián)下拉選擇框

    Vue實現(xiàn)省市區(qū)級聯(lián)下拉選擇框

    這篇文章主要為大家詳細介紹了Vue實現(xiàn)省市區(qū)級聯(lián)下拉選擇框,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • vue3中vue.config.js配置及注釋詳解

    vue3中vue.config.js配置及注釋詳解

    在Vue 3.0中,與2.0版本相比有一定的差別,最明顯的就是缺少了build、config文件夾,下面這篇文章主要給大家介紹了關(guān)于vue3中vue.config.js配置及注釋的相關(guān)資料,需要的朋友可以參考下
    2022-08-08
  • element-ui 上傳圖片后清空圖片顯示的實例

    element-ui 上傳圖片后清空圖片顯示的實例

    今天小編就為大家分享一篇element-ui 上傳圖片后清空圖片顯示的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-09-09
  • Vue監(jiān)聽屬性和計算屬性

    Vue監(jiān)聽屬性和計算屬性

    這篇文章主要介紹了Vue監(jiān)聽屬性和計算屬性,基本用法添加watch屬性,值為一個對象。對象的屬性名就是要監(jiān)視的數(shù)據(jù),屬性值為回調(diào)函數(shù),每當(dāng)這個屬性名對應(yīng)的值發(fā)生變化,就會觸發(fā)該回調(diào)函數(shù)執(zhí)行,下面來看詳細內(nèi)容,需要的朋友也可以參考一下
    2021-12-12
  • table表格中使用el-popover 無效問題解決方法

    table表格中使用el-popover 無效問題解決方法

    這篇文章主要介紹了table表格中使用el-popover 無效問題解決方法,實例只針對單個的按鈕管用在表格里每一列都有el-popover相當(dāng)于是v-for遍歷了 所以我們在觸發(fā)按鈕的時候并不是單個的觸發(fā)某一個,需要的朋友可以參考下
    2024-01-01
  • vue 表單之通過v-model綁定單選按鈕radio

    vue 表單之通過v-model綁定單選按鈕radio

    這篇文章主要介紹了vue 表單之v-model綁定單選按鈕radio的實例代碼,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-05-05
  • 詳解VueJS應(yīng)用中管理用戶權(quán)限

    詳解VueJS應(yīng)用中管理用戶權(quán)限

    本篇文章主要給大家講述了VueJS應(yīng)用中管理用戶權(quán)限的詳細過程和方法,以及相關(guān)的代碼展示,需要的朋友參考下吧。
    2018-02-02
  • vue下canvas裁剪圖片實例講解

    vue下canvas裁剪圖片實例講解

    在本篇文章里小編給大家整理了關(guān)于vue下canvas裁剪圖片實例講解內(nèi)容,需要的朋友們可以參考下。
    2020-04-04

最新評論