微信小程序> 微信小程序(游戏)五子棋(AI篇)

微信小程序(游戏)五子棋(AI篇)

浏览量:5375 时间: 来源:Rattenking

WXRUI体验二维码

小程序

如果文章对你有帮助的话,请打开微信扫一下二维码,点击一下广告,支持一下作者!谢谢!

DEMO下载

五子棋AI篇DEMO

效果图

小程序

原理

  1. 将棋盘中能够胜利的五子连珠方法遍历一个数组;
  2. 当AI持棋时,遍历棋盘中所有棋子的空位;
  3. 如果用户落子该位置,给用户该位置的五连珠方式进行加分:1连10分,2连20分,3连40分,4连80分;
  4. 如果AI落子该位置,给AI该位置的五连珠方式进行加分:1连15分,2连25分,3连45分,4连85分;
  5. 最后对该位置的分值进行比较,取最大分值位置的坐标,AI在最大分值位落子。

AI代码

computerAI(){    var playerScore = [],computerScore = [];    var max = 0,u = 0, v = 0;    for (var i = 0; i  this.type; i++){      playerScore[i] = [];      computerScore[i] = [];      for (var j = 0; j  this.type; j++){        playerScore[i][j] = 0;        computerScore[i][j] = 0;      }    }    for (var x = 0; x  this.type; x++) {      for (var y = 0; y  this.type; y++) {        var po = this.checkPosition(x, y);        if (po.status == 0){          for (var k = 0; k  this.count; k++) {            if (this.WIN_ARRAY[x][y][k]){              if (this.player[k] == 1){                playerScore[x][y] += 10;              } else if (this.player[k] == 2){                playerScore[x][y] += 20;              } else if (this.player[k] == 3) {                playerScore[x][y] += 40;              } else if (this.player[k] == 4) {                playerScore[x][y] += 80;              }              if (this.computer[k] == 1) {                computerScore[x][y] += 15;              } else if (this.player[k] == 2) {                computerScore[x][y] += 25;              } else if (this.player[k] == 3) {                computerScore[x][y] += 45;              } else if (this.player[k] == 4) {                computerScore[x][y] += 85;              }            }          }          if (playerScore[x][y]  max){            max = playerScore[x][y];            u = x;            v = y;          } else if (playerScore[x][y] == max){            if (computerScore[x][y]  computerScore[u][v]){              u = x;              v = y;            }          }          if (computerScore[x][y]  max) {            max = computerScore[x][y];            u = x;            v = y;          } else if (computerScore[x][y] == max) {            if (playerScore[x][y]  playerScore[u][v]) {              u = x;              v = y;            }          }        }      }    }    var point = this.checkPosition(u,v);        if(point.status == 0){      this.oneStep(point);      point.status = -1;      this.COMPUTER_ARRAY.push(point);      for (var i = 0; i  this.count; i++) {        if (this.WIN_ARRAY[point.pointX][point.pointY][k]) {          this.computer[k]++;          this.player[k] = 100;        }      }      if (point.status == -1 && this.COMPUTER_ARRAY.length = this.CHESS_LEN && this.checkWin(point, this.COMPUTER_ARRAY)) {        wx.showToast({ title: '白棋胜利!' });        this.isStart = false;      }      if (this.isStart) {        this.isWho = !this.isWho;      }    }  }

注意

此种方式实现的算法AI的防守比较重,进攻性不强,有待优化。而且很简单就能给AI设置陷阱而取得胜。

WXRUI体验二维码

小程序

如果文章对你有帮助的话,请打开微信扫一下二维码,点击一下广告,支持一下作者!谢谢!

其他

我的博客,欢迎交流!

我的CSDN博客,欢迎交流!

微信小程序专栏

前端笔记专栏

微信小程序实现部分高德地图功能的DEMO下载

微信小程序实现MUI的部分效果的DEMO下载

微信小程序实现MUI的GIT项目地址

微信小程序实例列表

前端笔记列表

游戏列表

版权声明

即速应用倡导尊重与保护知识产权。如发现本站文章存在版权问题,烦请提供版权疑问、身份证明、版权证明、联系方式等发邮件至197452366@qq.com ,我们将及时处理。本站文章仅作分享交流用途,作者观点不等同于即速应用观点。用户与作者的任何交易与本站无关,请知悉。

产品经理

手机 : 13312967497

擅长 : 小程序流量变现

扫码领取礼包

最新资讯

热门模板

  • 头条
  • 搜狐
  • 微博
  • 百家
  • 一点资讯
  • 知乎