首页 > 代码库 > 人机五子棋
人机五子棋
利用简单的算法实现一个简单五子棋:
css代码:
*{margin:0;padding: 0}
img{
margin-left: 20px;
height: 630px;
width: 1300px;
position: absolute;
}
canvas{
display: block;
margin:auto;
box-shadow: -2px -2px 2px #EFEFEF,5px 5px 5px #B9B9B9;
background: #886600;
position: relative;
top: 50px;
}
html代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>人机版五子棋</title>
</head>
<body>
<canvas id="canvas" width="750" height="580"></canvas>
</body>
</html>
JS代码:
var balls=[];
var cl=true;
var over=false;
for(var i=0;i<25;i++){
balls[i]=[];
for(var j=0;j<25;j++){
balls[i][j]=0;
}
}
//赢法数组
var Winer=[];
for(var i=0;i<25;i++){
Winer[i]=[];
for(var j=0;j<19;j++){
Winer[i][j]=[];
}
}
var count=0;
//横线所有赢法
for( var i=0;i<25;i++){
for(j=0;j<15;j++){
for(var k=0;k<5;k++){
Winer[i][j+k][count]=true;
}
count++;
}
}
//竖线所有赢法
for(var i=0;i<19;i++){
for(var j=0;j<21;j++){
for(var k=0;k<5;k++){
Winer[j+k][i][count]=true;
}
count++;
}
}
//斜线所有赢法
for(var i=0;i<21;i++){
for(var j=0;j<15;j++){
for(var k=0;k<5;k++){
Winer[i+k][j+k][count]=true;
}
count++;
}
}
//反斜线所有赢法
for(var i=0;i<21;i++){
for(var j=18;j>3;j--){
for(var k=0;k<5;k++){
Winer[i+k][j-k][count]=true;
}
count++;
}
}
//console.log(count);
//赢法统计数组
var myWin=[];
var computerWin=[];
for(var i=0;i<count;i++){
myWin[i]=0;
computerWin[i]=0;
}
window.onload=function(){
var canvas=document.getElementById(‘canvas‘);
var context=canvas.getContext(‘2d‘);
context.strokeStyle="#B9B9B9";
for (var i = 0; i <= 25; i++) {
context.moveTo(15,15+i*30);
context.lineTo(735,15+i*30);
context.stroke();
context.moveTo(15+i*30,15);
context.lineTo(15+i*30,555)
}
canvas.onclick=function(event){
if(over){
return;
}
if(cl!=true){
return;
}
var x=event.offsetX;
var y=event.offsetY;
var i=Math.floor(x/30);
var j=Math.floor(y/30);
if(balls[i][j]==0){
draw(i,j,cl);
balls[i][j]=1;
for(var k=0;k<count;k++){
if(Winer[i][j][k]){
myWin[k]++;
computerWin[k]=6;
if(myWin[k]==5){
window.alert(‘你赢了‘);
over=true;
}
}
}
if(!over){
cl=!cl;
computerAi();
}
}
}
}
function computerAi(){
var max=0;
var u=0,v=0;
var myScore=[];
var computerScore=[];
for(var i=0;i<25;i++){
myScore[i]=[];
computerScore[i]=[];
for(var j=0;j<19;j++){
myScore[i][j]=0;
computerScore[i][j]=0;
}
}
for(var i=0;i<25;i++){
for(var j=0;j<19;j++){
if(balls[i][j]==0){
for(var k=0;k<count;k++){
if(Winer[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(myScore[i][j]>myScore[u][v]){
u=i;
v=j;
}
}
}
}
}
draw(u,v,false);
balls[u][v]=2;
for(var k=0;k<count;k++){
if(Winer[u][v][k]){
computerWin[k]++;
myWin[k]=6;
if(computerWin[k]==5){
window.alert(‘计算机赢了‘);
over=true;
}
}
}
if(!over){
cl=true;
}
}
function draw(i,j,cl){
var canvas=document.getElementById(‘canvas‘);
var context=canvas.getContext(‘2d‘);
context.beginPath();
context.arc(15+i*30,15+j*30,10,0,2*Math.PI);
context.closePath();
var gradient=context.createRadialGradient(15+i*30,15+j*30,10,15+i*30,15+j*30,1);//实现颜色渐变函数
if(cl){
gradient.addColorStop(0,"#0A0A0A");
gradient.addColorStop(1,"#636766");
}else{
gradient.addColorStop(0,"#D1D1D1");
gradient.addColorStop(1,"#F9F9F9");
}
context.fillStyle=gradient;
context.fill();
}
人机五子棋