温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

微信小程序如何实现五子棋游戏

发布时间:2022-05-24 19:51:42 阅读:370 作者:iii 栏目:开发技术
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

本文小编为大家详细介绍“微信小程序如何实现五子棋游戏”,内容详细,步骤清晰,细节处理妥当,希望这篇“微信小程序如何实现五子棋游戏”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。

微信小程序如何实现五子棋游戏

微信小程序如何实现五子棋游戏

效果图

.wxml

<view class="title">
  <view wx:if="{{currindex < 324 || defeat}}">
  {{defeat?'胜出方:'+(outindex?'黑棋':'白棋'):'轮到了:'+(outindex?'白棋':'黑棋')}}
  </view>
  <view wx:else>平局</view>
</view>
<view class="gobang">
  <view wx:for="{{detail}}" wx:key="index" bindtap="{{defeat || item.type > 0?'':'palyclass'}}" data-index="{{index}}">
    <view class="piece {{item.type > 0?item.type == 1?'black':'white':''}}"></view>
  </view>
</view>
<button wx:if="{{defeat || currindex > 323}}" bindtap="reset">重新开始</button>

.wxss

page{background#fff;}
.title{width100%;display: flex;align-items: center;justify-content: center;margin-top20rpx;font-size34rpx;}
.gobang{width342px;height342px;margin30rpx calc((100% - 342px) / 2);float: left;
position: relative;right9.5px;}
.gobang>view{width5.55%;height19px;float: left;display: flex;align-content: center;justify-content: center;
border-left1px solid #333;border-top1px solid #333;background#F0BF7C;}
.gobang>view:nth-child(18n+1){width5.6%;border-left0;border-top0;background#fff;}
.gobang>view:nth-child(18n){border-right1px solid #333;}
.gobang>view:nth-child(n+307){border-left0;background#fff;border-right0;}
.piece{backgroundrgba(255,255,255,0);width100%;height100%;border-radius50%;transition: background 0.3s;
position: relative;left9.5px;bottom9.5px;}
.white{background:radial-gradient(15px 15px at 15px 15px,#fff,#e2e2e2);box-shadow:2px 2px 4px rgba(0,0,0,0.3);}
.black{background:radial-gradient(10px 10px at 15px 15px,#e2e2e2,#333);box-shadow:2px 2px 4px rgba(0,0,0,0.4);}

.js

Page({
  data: {
  },
  onLoadfunction (options) {
    this.reset()
  },
  reset(e){
    var detail = []
    for(let i = 0;i < 324;i++){
      detail.push({type:0})
    }
    this.setData({
      detail:detail,
      defeat:false,
      outindex:false,
      currindex:0,
    })
  },
  palyclass(e){
    var index = e.currentTarget.dataset.index,detail = this.data.detail,outindex = this.data.outindex,
    currindex = this.data.currindex;
    currindex++
    detail[index].type = outindex?2:1
    this.setData({
      detail:detail,
      outindex:!outindex,
      currindex:currindex,
    })
    if(currindex > 8){
      this.validate(index)
    }
  },
  validate(index){
    var detail = this.data.detail,type = this.data.outindex?1:2,remai_left = (index%18)+1,remai_right = 19 - remai_left,
    remai_upper = Math.floor(index / 18) + 1,remai_lower = 19 - remai_upper,arr = [];
    for(let i = 1;i < (remai_left > 4?5:remai_left);i++){
      arr.unshift(index - i)
    }
    for(let i = 1;i < (remai_right > 4?5:remai_right);i++){
      arr.push(index + i)
    }
    this.result(arr,type).then(() => {
      arr = [];
      for(let i = 1;i < (remai_upper > 4?5:remai_upper);i++){
        arr.unshift(index - (18*i))
      }
      for(let i = 1;i < (remai_lower > 4?5:remai_lower);i++){
        arr.push(index + (18*i))
      }
      this.result(arr,type).then(() => {
        var oblique_left = remai_upper < remai_left?remai_upper:remai_left,
        oblique_right = remai_lower < remai_right?remai_lower:remai_right;
        arr = [];
        for(let i = 1;i < (oblique_left > 4?5:oblique_left);i++){
          arr.unshift(index - (18*i) - i)
        }
        for(let i = 1;i < (oblique_right > 4?5:oblique_right);i++){
          arr.push(index + (18*i) + i)
        }
        this.result(arr,type).then(() => {
          var curved_left = remai_upper < remai_right?remai_upper:remai_right,
          curved_right = remai_lower < remai_left?remai_lower:remai_left;
          arr = [];
          for(let i = 1;i < (curved_left > 4?5:curved_left);i++){
            arr.unshift(index - (18*i) + i)
          }
          for(let i = 1;i < (curved_right > 4?5:curved_right);i++){
            arr.push(index + (18*i) - i)
          }
          this.result(arr,type).then(() => {
            return;
          })
        })
      })
    })
  },
  result(arr,type){
    var detail = this.data.detail,number = 0;
    for(let i = 0;i < arr.length;i++){
      if(detail[arr[i]].type == type){
        number++
      }else if(number > 0){
        break;
      }
    }
    return new Promise((resolve, reject) => {
      if(number > 3){
        this.tips(type);
      }else{
        resolve()
      }
    })
  },
  tips(type){
    this.setData({
      defeat:true,
    })
    wx.showModal({
      title'提示',
      content'恭喜'+(type == 1?'黑棋':'白棋')+'获得了胜利',
      showCancel:false,
      confirmText:'我知道了'
    })
  },
})

读到这里,这篇“微信小程序如何实现五子棋游戏”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注亿速云行业资讯频道。

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI

开发者交流群×