vue实现井字棋游戏


Posted in Javascript onSeptember 29, 2020

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

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

vue实现井字棋游戏

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;
 }
 }
})

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

Javascript 相关文章推荐
js和jquery对dom节点的操作(创建/追加)
Apr 21 Javascript
javascript操作数组详解
Dec 17 Javascript
JS实现网页每隔3秒弹出一次对话框的方法
Nov 09 Javascript
详解JavaScript的回调函数
Nov 20 Javascript
Highcharts使用简例及异步动态读取数据
Dec 30 Javascript
对jQuary选择器的全面总结
Jun 20 Javascript
jQuery生成假加载动画效果
Dec 01 Javascript
谈谈jQuery之Deferred源码剖析
Dec 19 Javascript
详解vue之页面缓存问题(基于2.0)
Jan 10 Javascript
JavaScript箭头(arrow)函数详解
Jun 04 Javascript
vue中实现点击按钮滚动到页面对应位置的方法(使用c3平滑属性实现)
Dec 29 Javascript
JavaScript文档对象模型DOM
Nov 20 Javascript
js实现移动端图片滑块验证功能
Sep 29 #Javascript
js+cavans实现图片滑块验证
Sep 29 #Javascript
如何利用JS将手机号中间四位变成*号
Sep 29 #Javascript
原生JavaScript实现刮刮乐
Sep 29 #Javascript
原生JavaScript实现拖动校验功能
Sep 29 #Javascript
使用JavaScript实现贪吃蛇游戏
Sep 29 #Javascript
解决idea开发遇到javascript动态添加html元素时中文乱码的问题
Sep 29 #Javascript
You might like
DEDE采集大师官方留后门的删除办法
2011/01/08 PHP
php使用SAE原生Mail类实现各种类型邮件发送的方法
2016/10/10 PHP
PHP简单判断iPhone、iPad、Android及PC设备的方法
2016/10/11 PHP
php 微信开发获取用户信息如何实现
2016/12/13 PHP
php中简单的对称加密算法实现
2017/01/05 PHP
laravel model模型处理之修改查询或修改字段时的类型格式案例
2019/10/17 PHP
html向js方法传递参数具体实现
2013/08/08 Javascript
js报$ is not a function 的问题的解决方法
2014/01/20 Javascript
JS倒计时代码汇总
2014/11/25 Javascript
javascript实现表单提交后,提交按钮不可用的方法
2015/04/18 Javascript
js基础知识(公有方法、私有方法、特权方法)
2015/11/06 Javascript
深入理解angularjs过滤器
2016/05/25 Javascript
AngularJs directive详解及示例代码
2016/09/01 Javascript
微信小程序 wx.request(object) API详解及实例代码
2016/09/30 Javascript
vue注册组件的几种方式总结
2018/03/08 Javascript
基于Express框架使用POST传递Form数据
2019/08/10 Javascript
vue仿ios列表左划删除
2019/09/26 Javascript
加速vue组件渲染之性能优化
2020/04/09 Javascript
[01:01:52]DOTA2-DPC中国联赛定级赛 SAG vs iG BO3第二场 1月9日
2021/03/11 DOTA
Django的URLconf中使用缺省视图参数的方法
2015/07/18 Python
Pytorch入门之mnist分类实例
2018/04/14 Python
Python用csv写入文件_消除空余行的方法
2018/07/06 Python
pycharm 配置远程解释器的方法
2018/10/28 Python
PyCharm2019安装教程及其使用(图文教程)
2019/09/29 Python
Python API len函数操作过程解析
2020/03/05 Python
基于python模拟TCP3次握手连接及发送数据
2020/11/06 Python
python爬虫基础之urllib的使用
2020/12/31 Python
波兰最大的电商平台:Allegro.pl
2021/02/06 全球购物
怎么样写好简历中的自我评价
2013/10/25 职场文书
公务员职务工作的自我评价
2013/11/01 职场文书
送货司机岗位职责
2013/12/11 职场文书
护士进修自我鉴定
2014/02/07 职场文书
初二学习计划书范文
2014/04/27 职场文书
创业计划书之网络外卖
2019/10/31 职场文书
基于Python绘制子图及子图刻度的变换等的问题
2021/05/23 Python
python 学习GCN图卷积神经网络
2022/05/11 Python