没有智商的AI版本
js
Page({
data: {
operate: 1, //当前下棋人员
notOperate: 2, //反之
nowChess: [], //二维数组
changeBtn: [],
is: 1, //当前执黑棋,白棋为2
notIs: 2, //当前反方
blackScore: 0, //黑色棋子比分
whiteScore: 0, //白色棋子比分
title: "进行中", //游戏状态
numberExchanges: 0,
changePeople: true, //点击位置执行才能进行AI自动的下一步
},
onShow() {
//走重新开始游戏
this.resume()
},
//重新开始游戏
resume() {
var that = this
this.setData({
numberExchanges: 0,
title: "进行中...",
operate: 1, //当前下棋人员
notOperate: 2, //反之
})
//初始化二维数组
for (var i = 0; i < 8; i++) {
for (var j = 0; j < 8; j++) {
this.setData({
["nowChess[" + i + "][" + j + "]"]: 0,
["nowChess[" + 3 + "][" + 3 + "]"]: 2,
["nowChess[" + 4 + "][" + 3 + "]"]: 1,
["nowChess[" + 3 + "][" + 4 + "]"]: 1,
["nowChess[" + 4 + "][" + 4 + "]"]: 2,
})
}
};
// 计算当前棋子数量
this.score()
//提示刷新
this.prompt()
},
//提示显示
prompt() {
let num = 0
for (let i = 0; i <= 7; i++) {
for (let j = 0; j <= 7; j++) {
if (this.canGo(i, j) && this.data.nowChess[i][j] == 0) {
num++
this.setData({
["nowChess[" + i + "][" + j + "]"]: 3,
})
}
}
}
//如果没有当前可下位置交换执棋
if (num === 0) {
this.setData({
numberExchanges: this.data.numberExchanges + 1
})
if (this.data.numberExchanges === 2) {
this.setData({
numberExchanges: 0,
title: "结束"
})
return
}
if (this.data.operate === 1) {
this.setData({
operate: 2,
notOperate: 1,
})
//因为异步所以加一层判断再走,渲染异步值同步
if (this.data.operate === 2) {
this.prompt()
}
} else {
this.setData({
operate: 1,
notOperate: 2,
})
//因为异步所以加一层判断再走,渲染异步值同步
if (this.data.operate === 1) {
this.prompt()
}
}
}
//提示完之后看执棋方是自己还是AI,如果是AI则直接进入AI自动下棋阶段
if (this.data.operate === this.data.notIs) {
this.AIgo()
}
},
//清空提示
clearPrompt() {
for (let i = 0; i <= 7; i++) {
for (let j = 0; j <= 7; j++) {
if (this.data.nowChess[i][j] === 3) {
this.setData({
["nowChess[" + i + "][" + j + "]"]: 0,
})
}
}
}
},
AIgo() {
//AI计算出都有哪里可以走然后取随机数
let num = 0
this.data.changeBtn = []
for (let i = 0; i < 8; i++) {
for (let j = 0; j < 8; j++) {
if (this.data.nowChess[i][j] === 3) {
this.setData({
["changeBtn[" + num + "].X"]: i,
["changeBtn[" + num + "].Y"]: j,
})
num++
}
}
};
//计算AI随机走哪一位
let random = this.randomNum(0, this.data.changeBtn.length - 1)
//让机器人走点击随机位置的操作
//进行移动
if (this.data.changeBtn.length !== 0) {
this.go(this.data.changeBtn[random].X, this.data.changeBtn[random].Y)
}
},
//点击按钮走更改颜色
clickButton(e) {
const X = e.currentTarget.dataset.indexs
const Y = e.currentTarget.dataset.index
if (this.data.nowChess[X][Y] === 1 || this.data.nowChess[X][Y] === 2) {
return
}
this.go(X, Y)
},
//走棋
go(X, Y) {
//如果是能走的进行下面的执行并且最后将执手人更换
if (this.canGo(X, Y)) {
this.clearPrompt()
//左
this.changeDirect(X, Y, 0, -1)
//右
this.changeDirect(X, Y, 0, 1)
// 上
this.changeDirect(X, Y, -1, 0)
//下
this.changeDirect(X, Y, 1, 0)
//左上
this.changeDirect(X, Y, -1, -1)
//右上
this.changeDirect(X, Y, -1, 1)
// 左下
this.changeDirect(X, Y, 1, -1)
//右下
this.changeDirect(X, Y, 1, 1)
if (this.data.operate === 1) {
this.setData({
operate: 2,
notOperate: 1,
})
} else {
this.setData({
operate: 1,
notOperate: 2,
})
}
//重新校验
this.prompt()
// 计算当前棋子数量
this.score()
//成功走出一步即为成功,失误次清零
this.setData({
numberExchanges: 0
})
}
},
//校验方法
canGo(X, Y) {
if (this.checkDirect(X, Y, 0, -1)) { //左
return true
}
if (this.checkDirect(X, Y, 0, 1)) { //右
return true
}
if (this.checkDirect(X, Y, -1, 0)) { // 上
return true
}
if (this.checkDirect(X, Y, 1, 0)) { //下
return true
}
if (this.checkDirect(X, Y, -1, -1)) { //左上
return true
}
if (this.checkDirect(X, Y, -1, 1)) { //右上
return true
}
if (this.checkDirect(X, Y, 1, -1)) { // 左下
return true
}
if (this.checkDirect(X, Y, 1, 1)) { //右下
return true
}
return false
},
//更改棋子颜色
changeDirect(x1, y1, dx, dy) {
let flag = false
let x = 0
let y = 0
x = x1 + dx
y = y1 + dy
while (x > -1 && x < 8 && y > -1 && y < 8 &&
this.data.nowChess[x][y] &&
this.data.nowChess[x][y] != 0 &&
this.data.nowChess[x][y] === this.data.notOperate) {
x += dx;
y += dy;
flag = true
}
if (x > -1 && x < 8 && y > -1 && y < 8 &&
this.data.nowChess[x][y] &&
this.data.nowChess[x][y] === this.data.operate &&
flag) {
do {
x -= dx;
y -= dy;
if (x != x1 || y != y1) {
this.setData({
["nowChess[" + x + "][" + y + "]"]: this.data.operate,
["nowChess[" + x1 + "][" + y1 + "]"]: this.data.operate,
})
}
} while ((x != x1 || y != y1));
} else {
return
}
},
//校验该方向可否落子
checkDirect(x, y, dx, dy) {
let flag = false
x = x + dx
y = y + dy
while (x > -1 && x < 8 && y > -1 && y < 8 &&
this.data.nowChess[x][y] &&
this.data.nowChess[x][y] != 0 &&
this.data.nowChess[x][y] === this.data.notOperate) {
x += dx;
y += dy;
flag = true
}
if (x > -1 && x < 8 && y > -1 && y < 8 &&
this.data.nowChess[x][y] &&
this.data.nowChess[x][y] === this.data.operate &&
flag) {
return true
} else {
return false
}
},
//计算比分
score() {
let white = 0
let black = 0
for (let i = 0; i < 8; i++) {
for (let j = 0; j < 8; j++) {
if (this.data.nowChess[i][j] === 1) {
black++
} else if (this.data.nowChess[i][j] === 2) {
white++
}
}
};
this.setData({
whiteScore: white,
blackScore: black,
})
},
//生成从minNum到maxNum的随机数
randomNum(minNum, maxNum) {
switch (arguments.length) {
case 1:
return parseInt(Math.random() * minNum + 1, 10);
break;
case 2:
return parseInt(Math.random() * (maxNum - minNum + 1) + minNum, 10);
break;
default:
return 0;
break;
}
},
})