for (var x = 0; x <= this.num; x++) {
for (var y = 0; y <= this.num; y++) {
if (this.wins[x][y][k] && this.chessBoard[x][y] === 0) {
if ((x-q)*(x-q) === 1 ||(y-w)*(y-w) === 1 ) {
u = x;
v = y;
}
}
}
}
}
}
}
}
}
}
}
}
}
this.chessList.push([u, v, this.type]);
this.oneStep(u, v, this.type, this.backGauge);
for (var k = 0; k < this.count; k++) {
if (!this.type && this.wins[u][v][k]) {
this.myWin[k] += 999;
if (this.otherWin[k] < 999) {
this.otherWin[k]++;
if (this.otherWin[k] >= 5) {
alert('电脑赢了');
this.over = true;
this.admit = false;
this.admitted.classList.add('admit_btn');
this.admitted.classList.remove('start_btn');
this.back.classList.add('admit_btn');
this.back.classList.remove('start_btn');
}
1赞 · 1采集
慕移动9910096
2018-01-25
for (var x = 0; x <= this.num; x++) {
for (var y = 0; y <= this.num; y++) {
if (this.wins[x][y][k] && this.chessBoard[x][y] === 0) {
if ((x-q)*(x-q) === 1 ||(y-w)*(y-w) === 1 ) {
u = x;
v = y;
}
}
}
}
}
}
}
}
}
if (aIScore[i][j][l] === max) {
for (var m = 1; m < 5; m++) {
if (myScore[i][j][m] > maxScore) {
maxScore = myScore[i][j][m];
u = i;
v = j;
} else if (myScore[i][j][m] === maxScore) {
for (var k = 0; k < this.count; k++) {
if (this.otherWin[k] === l) {
for (var x = 0; x <= this.num; x++) {
for (var y = 0; y <= this.num; y++) {
if (this.wins[x][y][k] && this.chessBoard[x][y] === 2) {
q = x;
w = y;
}
}
}
1赞 · 1采集
慕移动9910096
2018-01-25
if (aIScore[i][j][l] > max) {
max = aIScore[i][j][l];
u = i;
v = j;
}
}
}
}
}
}
for (var i = 0; i <= this.num; i++) {
for (var j = 0; j <= this.num; j++) {
if (this.chessBoard[i][j] === 0) {
for (var l = 1; l < 5; l++) {
if (myScore[i][j][l] === max) {
for (var m = 1; m < 5; m++) {
if (aIScore[i][j][m] > maxScore) {
maxScore = aIScore[i][j][m]
u = i;
v = j;
} else if (myScore[i][j][m] === maxScore) {
for (var k = 0; k < this.count; k++) {
if (this.myWin[k] === l) {
for (var x = 0; x <= this.num; x++) {
for (var y = 0; y <= this.num; y++) {
if (this.wins[x][y][k] && this.chessBoard[x][y] === 1) {
q = x;
w = y;
}
}
}
1赞 · 1采集
慕移动9910096
2018-01-25
for (var i = 0; i <= this.num; i++) {
for (var j = 0; j <= this.num; j++) {
if (this.chessBoard[i][j] === 0) {
for (var k = 0; k < this.count; k++) {
if (this.wins[i][j][k]) {
if (this.myWin[k] === 1) {
myScore[i][j][1] += 200;
} else if (this.myWin[k] === 2) {
myScore[i][j][2] += 600;
} else if (this.myWin[k] === 3) {
myScore[i][j][3] += 4000;
} else if (this.myWin[k] === 4) {
myScore[i][j][4] += 20000;
}
if (this.otherWin[k] === 1) {
aIScore[i][j][1] += 220;
} else if (this.otherWin[k] === 2) {
aIScore[i][j][2] += 620;
} else if (this.otherWin[k] === 3) {
aIScore[i][j][3] += 5500;
} else if (this.otherWin[k] === 4) {
aIScore[i][j][4] += 50000;
}
}
for (var l = 1; l < 5; l++) {
if (myScore[i][j][l] > max) {
max = myScore[i][j][l];
u = i;
v = j;
}
1赞 · 1采集
慕移动9910096
2018-01-25
if (!that.over) {
console.log(that.type)
that.type = !that.type;
console.log(that.type)
if (that.isAI) {
that.isComput();
}
}
};
},
// 电脑走棋
isComput: function () {
var aIScore = []; // 电脑分数统计 人机对战时使用
var myScore = []; // 玩家1分数统计 人机对战时使用
var max = 0; // 统计最高分数
var maxScore = 0; // 统计最高分数
var u = 0, v = 0, q = 0, w = 0;
for (var i = 0; i <= this.num; i++) {
aIScore[i] = [];
myScore[i] = [];
for (var j = 0; j <= this.num; j++) {
aIScore[i][j] = [];
myScore[i][j] = [];
for (var k = 1; k < 5; k++) {
aIScore[i][j][k] = 0;
myScore[i][j][k] = 0;
}
}
}
1赞 · 1采集
慕移动9910096
2018-01-25
for (var k = 0; k < that.count; k++) {
if (that.type && that.wins[i][j][k]) {
that.otherWin[k] += 999;
if (that.myWin[k] < 999) {
that.myWin[k]++;
if (that.myWin[k] >= 5) {
alert('你赢了!');
that.over = true;
that.admit = false;
that.admitted.classList.add('admit_btn');
that.admitted.classList.remove('start_btn');
that.back.classList.add('admit_btn');
that.back.classList.remove('start_btn');
}
}
} else if (!that.type && that.wins[i][j][k] && !that.isAI) {
that.myWin[k] += 999;
if (that.otherWin[k] < 999) {
that.otherWin[k]++;
if (that.otherWin[k] >= 5) {
alert('你输了!');
that.over = true;
that.admit = false;
that.admitted.classList.add('admit_btn');
that.admitted.classList.remove('start_btn');
that.back.classList.add('admit_btn');
that.back.classList.remove('start_btn');
}
}
}
}
1赞 · 1采集
慕移动9910096
2018-01-25
for (var i = 0; i < this.count; i++) {
this.myWin[i] = 0;
this.otherWin[i] = 0;
}
}
// 判断是否悔棋
if (this.takeBack) {
for (var i = 0; i < this.chessList.length; i++) {
this.oneStep(this.chessList[i][0], this.chessList[i][1], this.chessList[i][2], this.backGauge);
}
}
// 绑定canvas点击事件
this.chessDom.onclick = function (e) {
if (that.over) {
return;
}
var x = e.offsetX;
var y = e.offsetY;
var i = Math.round((x - backGauge) / residue);
var j = Math.round((y - backGauge) / residue);
if (i > that.num || j > that.num || that.chessBoard[i][j] !== 0) {
return;
} else {
that.takeBack = false;
if (that.type) {
that.chessBoard[i][j] = 1;
} else {
that.chessBoard[i][j] = 2;
}
}
that.chessList.push([i, j, that.type]);
that.oneStep(i, j, that.type, backGauge);
1赞 · 1采集
慕移动9910096
2018-01-25
// 第一种赢法---所有5子竖线
for (var i = 0; i <= this.num; i++) {
for (var j = 0; j <= this.num - 4; j++) {
for (var k = 0; k <= 4; k++) {
this.wins[i][j + k][this.count] = true;
}
this.count++;
}
}
// 第二种赢法---所有5子横线
for (var i = 0; i <= this.num; i++) {
for (var j = 0; j <= this.num - 4; j++) {
for (var k = 0; k <= 4; k++) {
this.wins[j + k][i][this.count] = true;
}
this.count++;
}
}
// 第三种赢法---所有5子斜线
for (var i = 0; i <= this.num - 4; i++) {
for (var j = 0; j <= this.num - 4; j++) {
for (var k = 0; k <= 4; k++) {
this.wins[i + k][j + k][this.count] = true;
}
this.count++;
}
}
// 第四种赢法---所有5子反斜线
for (var i = 0; i <= this.num - 4; i++) {
for (var j = this.num; j >= 4; j--) {
for (var k = 0; k <= 4; k++) {
this.wins[i + k][j - k][this.count] = true;
}
this.count++;
}
}
1赞 · 1采集
慕移动9910096
2018-01-25
// 画棋盘
darwChessboard: function () {
var residue = Math.floor((this.windowWidth - 50) / (this.num + 1));
this.residue = residue;
var that = this;
var backGauge = (this.windowWidth - 50 - residue * this.num) / 2;
this.backGauge = backGauge;
for (var k = 0; k <= this.num; k++) {
this.context.moveTo(backGauge,backGauge + residue * k);
this.context.lineTo(backGauge + residue * this.num,backGauge + residue * k);
this.context.stroke();
this.context.moveTo(backGauge + residue * k,backGauge);
this.context.lineTo(backGauge + residue * k,backGauge + residue * this.num);
this.context.stroke();
};
if (!this.takeBack) {
for (var f = 0; f <= this.num; f++) {
this.chessBoard[f] = [];
this.wins[f] = [];
for (var s = 0; s <= this.num; s++) {
this.chessBoard[f][s] = 0;
this.wins[f][s] = [];
}
}
1赞 · 1采集
慕移动9910096
2018-01-25
if (this.chessList.length > 0) {
deleteItem = this.chessList.pop();
for (var i = 0; i < this.count; i++) {
if (deleteItem[2]) {
if (this.myWin[i] && this.wins[deleteItem[0]][deleteItem[1]][i]) {
if (this.myWin[i] < 999) {
this.myWin[i]--;
}
this.otherWin[i] -= 999;
}
} else {
if (this.otherWin[i] && this.wins[deleteItem[0]][deleteItem[1]][i]) {
if (this.otherWin[i] < 999) {
this.otherWin[i]--;
}
this.myWin[i] -= 999;
}
}
}
this.chessBoard[deleteItem[0]][deleteItem[1]] = 0;
} else {
this.type = !this.type;
}
this.resetFn();
},
1赞 · 1采集
慕移动9910096
2018-01-25
// 为canvas添加背景图片
var logo = document.getElementById('img');
this.context.drawImage(logo, 0, 0, this.windowWidth - 50, this.windowWidth - 50)
// 画棋盘
this.darwChessboard();
},
// 悔棋
takeBackFn: function () {
if (this.takeBack && this.chessList.length > 0) {
return;
}
this.takeBack = true;
var deleteItem = this.chessList.pop();
this.chessBoard[deleteItem[0]][deleteItem[1]] = 0;
for (var i = 0; i < this.count; i++) {
if (deleteItem[2]) {
if (this.myWin[i] && this.wins[deleteItem[0]][deleteItem[1]][i]) {
if (this.myWin[i] < 999) {
this.myWin[i]--;
}
this.otherWin[i] -= 999;
}
} else {
if (this.otherWin[i] && this.wins[deleteItem[0]][deleteItem[1]][i]) {
if (this.otherWin[i] < 999) {
this.otherWin[i]--;
}
this.myWin[i] -= 999;
}
}
}