亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

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

vue實現井字棋游戲

瀏覽:83日期: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
相關文章:
主站蜘蛛池模板: 亚洲欧美一区在线 | 欧美刺激午夜性久久久久久久 | 欧美久久伊人 | 在线观看香蕉免费啪在线观看 | 国产欧美亚洲精品第一页青草 | 三个黑人强一个女人视频 | 欧美日韩一区二区三区免费不卡 | 黄色一级片免费看 | 在线观看欧洲成人免费视频 | 91香蕉国产在线观看人员 | 精品视频免费看 | 俄罗斯aa一级毛片 | 一级毛片视频在线 | 免费观看欧美精品成人毛片能看的 | 91福利区 | 蕾丝视频在线看片国产 | 在线成人免费看大片 | 豆国产97在线 | 中国 | 欧美激情一区二区 | 小视频在线免费观看 | 国产午夜高清一区二区不卡 | 国产一区日韩二区欧美三区 | 全国男人的天堂网站 | 午夜精品一区二区三区在线观看 | 日本一级毛片视频 | 亚洲国产精品区 | 爱爱视频在线播放 | 亚洲成在人网站天堂一区二区 | 日韩在线视频网址 | 国产91无毒不卡在线观看 | 日韩在线亚洲 | 亚洲精品国产国语 | 亚洲欧美日韩国产精品网 | 91久久免费视频 | 国产成人在线网址 | 91欧美国产 | 国产不卡视频 | 日韩黄色在线播放 | 丝袜足交在线播放 | 成人免播放器午夜视频 | 国产精品久久久久无码av |