午夜剧场伦理_日本一道高清_国产又黄又硬_91黄色网战_女同久久另类69精品国产_妹妹的朋友在线

您的位置:首頁技術文章
文章詳情頁

vue實現井字棋游戲

瀏覽:170日期:2022-11-17 16:32:45

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

之前看react的教程時看到的小游戲,試著用vue做一個。右邊的winner提示勝者,還沒有勝者時提示下一個棋子的種類。restart按鈕點擊可重新開始。go to step可跳轉到第n步。

vue實現井字棋游戲

html:

<div id='app'> <ul class='white normal'> <li v-for='i, idx in datas' @click=set(idx)>{{i}}</li> </ul> <div id='console'> <div class='white'>{{hint}}</div> <input type='button' value='restart' @click='init()'/> <ul class='normal'> <li v-for='i, idx in history'> <input type='button' :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
相關文章:
主站蜘蛛池模板: 少妇一区二区视频 | 探花精品 | 亚洲天堂视频在线观看 | 波多野结衣国产 | 欧美视频导航 | 欧美xxxx日本和非洲 | 亚洲国产一区二区三区在线观看 | 手机在线观看毛片 | 日韩成人一级 | 久久精品99国产精品日本 | 国产天堂网 | 国产精品福利一区 | 四虎视频国产精品免费 | 亚洲精品小说 | 岛国精品在线播放 | 丰满漂亮的日本岳 | 嫩草天堂 | 欧美香蕉在线 | 一区二区三区国产在线观看 | 尹人综合网| 黄在线免费| 久久综合久久久 | 国产精品资源站 | 欧美一级在线 | 欧美黄色大全 | 亚洲二三区 | 婷婷激情四射 | 日韩第四页 | 久久久久久久久成人 | 色偷偷噜噜噜亚洲男人 | 亚洲免费中文字幕 | 国产精品自拍区 | 中文字幕在线天堂 | 91麻豆网站 | 国产精品国产精品国产 | 波多野结衣在线观看一区二区 | 国产高清亚洲 | 天堂在线观看中文字幕 | 特级毛片爽www免费版 | 狠狠干狠狠干狠狠干 | 久久久精品免费观看 |