有两个疑问,同学们帮忙解答一下
1:分出输赢以后清除画布,重新开始!但是用clearRect(0, 0, canvas.width, canvas.height) 会连边框一起清除。所以只能是刷新页面?
2: 可能是我代码问题,算法有问题好像! 不会堵我下的棋。而且就算我随便下,它也要好久才能赢!
代码贴上来给大家看一下希望有人帮我看看:
var canvas = document.getElementById('can') var context = canvas.getContext('2d'); var chess = []; var wins = []; var me = true; var over = false; //赢法统计数组 var mywin = []; var computerwin = []; for (var i = 0; i < 15; i++) { chess[i] = []; for (var j = 0; j < 15; j++) { chess[i][j] = 0; } } 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++ } } for (var i = 0; i < count; i++) { mywin[i] = 0; computerwin[i] = 0; } context.strokeStyle = '#bfbfbf' for (var i = 0; i < 15; i++) { context.moveTo(15 + i * 30, 15); context.lineTo(15 + i * 30, 435); context.moveTo(15, 15 + i * 30); context.lineTo(435, 15 + i * 30); context.stroke() } var oneStep = function (i, j, me) { context.beginPath(); context.arc(15 + i * 30, 15 + j * 30, 13, 0, Math.PI * 2); context.closePath() var yin = context.createRadialGradient(15 + i * 30 + 2, 15 + j * 30 - 2, 13, 15 + i * 30 + 2, 15 + j * 30 - 2, 0) if (me) { yin.addColorStop(0, '#0a0a0a') yin.addColorStop(1, '#636766') } else { yin.addColorStop(0, '#d1d1d1') yin.addColorStop(1, '#f9f9f9') } context.fillStyle = yin context.fill() } canvas.onclick = function (e) { if (over) { return } if (!me) { return } var x = e.offsetX; var y = e.offsetY; i = Math.floor(x / 30); j = Math.floor(y / 30); if (chess[i][j] == 0) { oneStep(i, j, me); chess[i][j] = 1; for (var k = 0; k < count; k++) { if (wins[i][j][k]) { mywin[k]++ computerwin[k] = 6; if (mywin[k] == 5) { window.alert('你赢了'); over = true; } } } if (!over) { me = !me; computerAi() } } } var computerAi = function () { 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 (chess[i][j] == 0) { for (var k = 0; k < count; k++) { if (wins[i][j][k]) { if (mywin[k] == 1) { myScore[i][j] += 500; } else if (mywin[k] == 2) { myScore[i][j] += 600; } else if (mywin[k] == 3) { myScore[i][j] += 1000; } else if (mywin[k] == 4) { myScore[i][j] += 5000; } if (computerwin[k] == 1) { computerScore[i][j] += 520; } else if (computerwin[k] == 2) { computerScore[i][j] += 650 ; } else if (computerwin[k] == 3) { computerScore[i][j] +=1200; } else if (computerwin[k] == 4) { computerScore[i][j] += 10000; } } } 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 (myScore[i][j] > myScore[u][v]) { u = i; v = j; } } } } } oneStep(u,v,false); chess[u][v]=2; for (var k = 0; k < count; k++) { if (wins[u][v][k]) { computerwin[k]++ mywin[k] = 6; if (computerwin[k] == 5) { window.alert('计算机赢了'); over = true; } } } if (!over) { me = !me; } }