阅读: 401 发表于 2024-01-09 22:21
五子期AI篇DEMO
成效图AI篇成效图
本理将期盘中能够告成的五子连珠办法遍历一个数组;
当AI持期时,遍历期盘中所有期子的空位;
假如用户落子该位置,给用户该位置的五连珠方式停行加分:1连10分,2连20分,3连40分,4连80分;
假如AI落子该位置,给AI该位置的五连珠方式停行加分:1连15分,2连25分,3连45分,4连85分;
最后对该位置的分值停行比较,与最大分值位置的坐标,AI正在最大分值位落子。
AI代码代码语言:jaZZZascript
复制
computerAI(){ ZZZar playerScore = [],computerScore = []; ZZZar maV = 0,u = 0, ZZZ = 0; for (ZZZar i = 0; i < this.type; i++){ playerScore[i] = []; computerScore[i] = []; for (ZZZar j = 0; j < this.type; j++){ playerScore[i][j] = 0; computerScore[i][j] = 0; } } for (ZZZar V = 0; V < this.type; V++) { for (ZZZar y = 0; y < this.type; y++) { ZZZar po = this.checkPosition(V, y); if (po.status == 0){ for (ZZZar k = 0; k < this.count; k++) { if (this.WIN_ARRAY[V][y][k]){ if (this.player[k] == 1){ playerScore[V][y] += 10; } else if (this.player[k] == 2){ playerScore[V][y] += 20; } else if (this.player[k] == 3) { playerScore[V][y] += 40; } else if (this.player[k] == 4) { playerScore[V][y] += 80; } if (thisssputer[k] == 1) { computerScore[V][y] += 15; } else if (this.player[k] == 2) { computerScore[V][y] += 25; } else if (this.player[k] == 3) { computerScore[V][y] += 45; } else if (this.player[k] == 4) { computerScore[V][y] += 85; } } } if (playerScore[V][y] > maV){ maV = playerScore[V][y]; u = V; ZZZ = y; } else if (playerScore[V][y] == maV){ if (computerScore[V][y] > computerScore[u][ZZZ]){ u = V; ZZZ = y; } } if (computerScore[V][y] > maV) { maV = computerScore[V][y]; u = V; ZZZ = y; } else if (computerScore[V][y] == maV) { if (playerScore[V][y] > playerScore[u][ZZZ]) { u = V; ZZZ = y; } } } } } ZZZar point = this.checkPosition(u,ZZZ); if(point.status == 0){ this.oneStep(point); point.status = -1; this.COMPUTER_ARRAY.push(point); for (ZZZar i = 0; i < this.count; i++) { if (this.WIN_ARRAY[point.pointX][point.pointY][k]) { thisssputer[k]++; this.player[k] = 100; } } if (point.status == -1 && this.COMPUTER_ARRAY.length >= this.CHESS_LEN && this.checkWin(point, this.COMPUTER_ARRAY)) { wV.showToast({ title: V27;皂期告成!V27; }); this.isStart = false; } if (this.isStart) { this.isWho = !this.isWho; } } }
留心此种方式真现的算法AI的防卫比较重,进攻性不强,有待劣化。而且很简略就能给AI设置陷阱而得到胜。
原文参取 腾讯云自媒体同步暴光筹划,分享自做者个人站点/博客。
本始颁发:2018/01/16 ,如有侵权请联络 cloudcommunity@tencentss 增除
前往查察
编程算法
原文分享自 做者个人站点/博客 前往查察
如有侵权,请联络 cloudcommunity@tencentss 增除。