iis服务器助手广告广告
返回顶部
首页 > 资讯 > 前端开发 > JavaScript >微信小程序实现井字棋游戏
  • 127
分享到

微信小程序实现井字棋游戏

2024-04-02 19:04:59 127人浏览 泡泡鱼
摘要

本文实例为大家分享了微信小程序实现井字棋游戏的具体代码,供大家参考,具体内容如下 效果图 .wxml <view class="title">   <view w

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

效果图

.wxml

<view class="title">
  <view wx:if="{{currindex < 9 || defeat}}">
  {{defeat?'胜出方:':'轮到了:'}}<span class="span">{{defeat?(outindex?'○':'x'):(outindex?'x':'○')}}</span>
  </view>
  <view wx:else>平局</view>
</view>
<view class="curr_toe">
  <view wx:for="{{detail}}" wx:key="index" class="curr_item" 
  bindtap="{{item.type > 0 || defeat?'':'tactoe'}}" data-index="{{index}}">
    <view wx:if="{{item.type > 0}}">{{item.type == 1?'○':'×'}}</view>
  </view>
</view>
<button wx:if="{{defeat || currindex > 8}}" bindtap="reset">重新开始</button>

.wxss

page{background: #fff;}
.title{width: 100%;display: flex;align-items: center;justify-content: center;margin-top: 20rpx;font-size: 34rpx;}
.span{font-size: 60rpx;}
.curr_toe{width: 510rpx;height: 510rpx;margin: 30rpx calc((100% - 500rpx) / 2);border-top:1px solid #DDD;border-right: 1px solid #ddd;}
.curr_item{border-left: 1px solid #ddd;width: 33.33%;height: 170rpx;display: flex;align-items: center;justify-content: center;color:red;
font-size:170rpx;float: left;border-bottom: 1px solid #ddd;}

.js

Page({
  data: {
    lines:[
      [0, 1, 2],
      [3, 4, 5],
      [6, 7, 8],
      [0, 3, 6],
      [1, 4, 7],
      [2, 5, 8],
      [0, 4, 8],
      [2, 4, 6],
    ]
  },
  onLoad: function (options) {
    this.reset()
  },
  reset(e){
    this.setData({
      detail:[
        {type:0},{type:0},{type:0},{type:0},{type:0},{type:0},{type:0},{type:0},{type:0}
      ],
      defeat:false,
      outindex:false,
      currindex:0
    })
  },
  tactoe(e){
    var index = e.currentTarget.dataset.index,currindex = this.data.currindex,
    detail = this.data.detail,outindex = this.data.outindex;
    currindex++
    detail[index].type = outindex?2:1
    this.setData({
      detail:detail,
      currindex:currindex,
      outindex:!outindex
    })
    if(currindex > 4){
      this.validate()
    }
  },
  validate(e){
    var detail = this.data.detail,lines = this.data.lines;
    for(let i = 0;i < lines.length;i++){
      const [a, b, c] = lines[i];
      if(detail[a].type && detail[a].type == detail[b].type && detail[a].type == detail[c].type){
        wx.showModal({
          title: '提示',
          content: (detail[a].type == 1?'○':'×')+'获得了胜利',
          showCancel:false,
          confirmText:'我知道了'
        })
        this.setData({
          defeat:true
        })
        return false;
      }
    }
  },
})

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程网。

--结束END--

本文标题: 微信小程序实现井字棋游戏

本文链接: https://www.lsjlt.com/news/149484.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作