《js之人工智能-五子棋-算法》核心源码:
var width = 450;
var height = 450;
var me = true;
var over = false;
var wins = []; //算法 三维数组 记录所有的赢法组合
for(var i = 0; i < 15; i++) {
wins[i] = [];
for(var j = 0; j < 15; j++) {
wins[i][j] = [];
}
}
var count = 0;
for(var i = 0; i < 15; i++) { //所有横向的赢法
for(var j = 0; j < 11; j++) {
// wins[0][0][0] = true
// wins[0][1][0] = true
// wins[0][2][0] = true
// wins[0][3][0] = true
// wins[0][4][0] = true
// wins[0][1][1] = true
// wins[0][2][1] = true
// wins[0][3][1] = true
// wins[0][4][1] = true
// wins[0][5][1] = true
for(var k = 0; k < 5; k++) {
wins[i][j + k][count] = true;
}
count++;
}
}
for(var i = 0; i < 15; i++) { //所有竖向的赢法
for(var j = 0; j < 11; j++) {
for(var k = 0; k < 5; k++) {
wins[j + k][i][count] = true;
}
count++;
}
}
for(var i = 0; i < 11; i++) { //所有斜线的赢法
for(var j = 0; j < 11; j++) {
for(var k = 0; k < 5; k++) {
wins[i + k][j + k][count] = true;
}
count++;
}
}
for(var i = 0; i < 11; i++) { //所有反斜线的赢法
for(var j = 14; j > 3; j--) {
for(var k = 0; k < 5; k++) {
wins[i + k][j - k][count] = true;
}
count++;
}
}
console.log("共有" + count + "种赢法"); //572种赢法
/**
* 赢法统计数组
*/
var myWin = [];
var computerWin = [];
for(var i = 0; i < count; i++) { //所有反斜线的赢法
myWin[i] = 0;
computerWin[i] = 0;
}
var chessBoard = []; //记录已经下过的棋子
for(var i = 0; i < 15; i++) {
chessBoard[i] = [];
for(var j = 0; j < 15; j++) {
chessBoard[i][j] = 0;
}
}
var chess = document.getElementById('chess');
var context = chess.getContext('2d');
context.strokeStyle = "#BFBFBF";
var logo = new Image();
logo.src = 'favicon.ico';
logo.onload = function() {
//context.drawImage(logo, 15 + 5 * 30, 15 + 5 * 30, 120, 120);
drawChessBoard();
if(!localStorage.getItem("hasTip")) {
showAlert("你为黑子,计算机为白子", 50);
localStorage.setItem("hasTip", true);
}
}
function drawChessBoard() {
for(var i = 0; i < 15; i++) {
context.moveTo(15 + i * 30, 15); //画竖线
context.lineTo(15 + i * 30, 435); //画竖线
context.stroke(); //描边
context.moveTo(15, 15 + i * 30); //画横线
context.lineTo(435, 15 + i * 30); //画横线
context.stroke();
}
}
function oneStep(i, j, me) {
context.beginPath();
context.arc(15 + i * 30, 15 + j * 30, 13, 0, 2 * Math.PI);
context.closePath();
var gradient = context.createRadialGradient(15 + i * 30 + 2, 15 + j * 30 - 2, 13, 15 + i * 30 + 2, 15 + j * 30 - 2, 0);
if(me) {
gradient.addColorStop(0, "#0A0A0A");
gradient.addColorStop(1, "#636766");
} else {
gradient.addColorStop(0, "#D1D1D1");
gradient.addColorStop(1, "#F9F9F9");
}
context.fillStyle = gradient;
context.fill(); //填充
}
chess.onclick = function(e) {
if(over) {
return;
}
if(!me) {
return;
}
var x = e.offsetX;
var y = e.offsetY;
var i = Math.floor(x / 30);
var j = Math.floor(y / 30);
if(chessBoard[i][j] == 0) {
oneStep(i, j, me);
chessBoard[i][j] = 1;
for(var k = 0; k < count; k++) {
if(wins[i][j][k]) {
myWin[k]++;
computerWin[k] = 6;
if(myWin[k] == 5) {
console.log("you win");
//window.alert("you win"); //会有阻塞
showAlert("you win", 50);
over = true;
}
}
}
if(!over) {
me = !me;
computerAI();
}
}
//console.log(JSON.stringify(chessBoard));
}
function computerAI() {
var myScore = [];
var computerScore = [];
var max = 0; //最高分
var u = 0,
v = 0; //坐标
for(var i = 0; i < 15; i++) {
myScore[i] = [];
computerScore[i] = [];
for(var j = 0; j < 15; j++) {
myScore[i][j] = 0;
computerScore[i][j] = 0;
}
}
for(var i = 0; i < 15; i++) {
for(var j = 0; j < 15; j++) {
if(chessBoard[i][j] == 0) { //空闲的点
for(var k = 0; k < count; k++) {
if(wins[i][j][k]) {
if(myWin[k] == 1) {
myScore[i][j] += 200;
} else if(myWin[k] == 2) {
myScore[i][j] += 400;
} else if(myWin[k] == 3) {
myScore[i][j] += 2000;
} else if(myWin[k] == 4) {
myScore[i][j] += 10000
}
if(computerWin[k] == 1) {
computerScore[i][j] += 220;
} else if(computerWin[k] == 2) {
computerScore[i][j] += 420;
} else if(computerWin[k] == 3) {
computerScore[i][j] += 2100;
} else if(computerWin[k] == 4) {
computerScore[i][j] += 20000
}
}
}
if(myScore[i][j] > max) {
max = myScore[i][j];
u = i;
v = j;
} else if(myScore[i][j] == max) {
if(computerScore[i][j] > computerScore[u][v]) {
u = i;
v = j;
}
}
if(computerScore[i][j] > max) {
max = computerScore[i][j];
u = i;
v = j;
} else if(computerScore[i][j] == max) {
if(computerScore[i][j] > computerScore[u][v]) {
u = i;
v = j;
}
}
}
}
}
oneStep(u, v, false);
chessBoard[u][v] = 2;
for(var k = 0; k < count; k++) {
if(wins[u][v][k]) {
computerWin[k]++;
myWin[k] = 6;
if(computerWin[k] == 5) {
console.log("computer win");
showAlert("computer win", 50);
//window.alert("computer win"); //会有阻塞
over = true;
}
}
}
if(!over) {
me = !me;
}
}
function showAlert(content, delay) { //避免alert阻塞线程
setTimeout(function() {
alert(content);
}, (delay && delay > -1) ? delay : 300);
}