微信小程序五子棋游戲AI實(shí)現(xiàn)方法【附demo源碼下載】
本文實(shí)例講述了微信小程序五子棋游戲AI實(shí)現(xiàn)方法。分享給大家供大家參考,具體如下:
DEMO下載
效果圖

原理
1. 將棋盤(pán)中能夠勝利的五子連珠方法遍歷一個(gè)數(shù)組;
2. 當(dāng)AI持棋時(shí),遍歷棋盤(pán)中所有棋子的空位;
3. 如果用戶落子該位置,給用戶該位置的五連珠方式進(jìn)行加分:1連10分,2連20分,3連40分,4連80分;
4. 如果AI落子該位置,給AI該位置的五連珠方式進(jìn)行加分:1連15分,2連25分,3連45分,4連85分;
5. 最后對(duì)該位置的分值進(jìn)行比較,取最大分值位置的坐標(biāo),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;
}
}
}
注意
此種方式實(shí)現(xiàn)的算法AI的防守比較重,進(jìn)攻性不強(qiáng),有待優(yōu)化。而且很簡(jiǎn)單就能給AI設(shè)置陷阱而取得勝。
希望本文所述對(duì)大家微信小程序開(kāi)發(fā)有所幫助。
相關(guān)文章
一個(gè)不用onmouseup的拖動(dòng)函數(shù)
一個(gè)不用onmouseup的拖動(dòng)函數(shù)...2007-05-05
微信小程序setInterval定時(shí)函數(shù)新手使用的超詳細(xì)教程
平時(shí)開(kāi)發(fā)中為實(shí)現(xiàn)倒計(jì)時(shí)效果可以使用setInterval即可,下面這篇文章主要給大家介紹了關(guān)于微信小程序setInterval定時(shí)函數(shù)新手使用的超詳細(xì)教程,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08
javascript實(shí)現(xiàn)5秒倒計(jì)時(shí)并跳轉(zhuǎn)功能
這篇文章主要為大家詳細(xì)介紹了javascript實(shí)現(xiàn)5秒倒計(jì)時(shí)并跳轉(zhuǎn)功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-06-06
javascript 設(shè)計(jì)模式之組合模式原理與應(yīng)用詳解
這篇文章主要介紹了javascript 設(shè)計(jì)模式之組合模式原理與應(yīng)用,結(jié)合實(shí)例形式分析了javascript組合模式基本概念、原理、應(yīng)用場(chǎng)景及操作注意事項(xiàng),需要的朋友可以參考下2020-04-04
js實(shí)現(xiàn)單行文本向上滾動(dòng)效果實(shí)例代碼
這篇文章主要介紹了js實(shí)現(xiàn)單行文本向上滾動(dòng)效果,大家參考使用吧2013-11-11
由document.body和document.documentElement想到的
不知道大家對(duì)這個(gè)標(biāo)題有沒(méi)有想法,反正此前我一直把他們混為了一談。其實(shí)不然,首先需有個(gè)“標(biāo)準(zhǔn)”的概念。2009-04-04
JS實(shí)現(xiàn)仿google、百度搜索框輸入信息智能提示的實(shí)現(xiàn)方法
這篇文章主要介紹了JS實(shí)現(xiàn)仿google、百度搜索框輸入信息智能提示的實(shí)現(xiàn)方法,實(shí)例分析了javascript實(shí)現(xiàn)智能提示功能的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04
js實(shí)現(xiàn)不提示直接關(guān)閉網(wǎng)頁(yè)窗口
本文主要介紹了js實(shí)現(xiàn)不提示直接關(guān)閉網(wǎng)頁(yè)窗口的方法。具有很好的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-03-03

