canvas+js实现初级五子棋AI

<!DOCTYPE html>
<html>
<head>
<title>chess</title>
<style type="text/css">
*{
padding: 0;
margin:0;
}
#box{
width: 450px;
height: 450px;
border: 1px solid #000;
margin: 60px auto;
}
</style>
</head>
<body>
<div id="box">
<canvas id="chess" width="450" height="450"></canvas>
</div>
<script type="text/javascript">
var _me =true;
var data =[];
//赢法数组
var wins = [];
var over = false;
// 赢法统计数组
var myWin = [];
var computerWin = [];
var chess = document.getElementById('chess');
var box = document.getElementById('box');
var ctx = chess.getContext('2d');
ctx.strokeStyle = '#BFBFBF';

for(let i=0;i<15;i++){
data[i]=[];
for(let j=0;j<15;j++){
data[i][j]=0;
}
}
for(let i=0;i<15;i++){
wins[i]=[];
for(let j=0;j<15;j++){
wins[i][j]=[];
}
}
var count = 0;
//横线赢法
for(let i = 0;i<15;i++){
for(let j = 0;j<11;j++){
for(let k=0;k<5;k++){
wins[i][j+k][count] = true;
}
count++; 165
}
}
//竖线赢法
for(let i = 0;i<11;i++){
for(let j = 0;j<15;j++){
for(let k=0;k<5;k++){
wins[i+k][j][count] = true;
}
count++;//165
}
}
//斜线赢法
for(let i = 4;i<15;i++){
for(let j = 0;j<11;j++){
for(let k=0;k<5;k++){
wins[i-k][j+k][count] = true;
}
count++;//110
}
}
//反斜线
for(let i = 10;i>-1;i--){
for(let j = 0;j<11;j++){
for(let k=0;k<5;k++){
wins[i+k][j+k][count] = true;
}
count++;//110
}
}
// alert(count);
for(let i=0;i<count;i++){
myWin[i] = 0;
computerWin[i] = 0;
}
function play(){
drowBord();

}
function drowBord(){
for(let i = 0;i<15;i++){
ctx.moveTo(15+i*30 , 15);
ctx.lineTo(15+i*30 , 435);
ctx.stroke();
ctx.moveTo(15 , 15 + i*30 );
ctx.lineTo(435, 15 + i*30 );
ctx.stroke();
}
}
function oneStep(i,j,me){
ctx.beginPath();
ctx.arc(15+i*30,15+j*30,13,0,2*Math.PI);
ctx.closePath();
var gradient = ctx.createLinearGradient(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');
data[i][j] = 1;
}else{
gradient.addColorStop(0,'#D1D1D1');
gradient.addColorStop(1,'#F9F9F9');
data[i][j] = 2;
}
ctx.fillStyle=gradient;
ctx.fill();
}
box.onclick = function(event){
if(over) return
if(!_me) return
var e = event || window.event;
let i = Math.floor(e.offsetX/30);
let j = Math.floor(e.offsetY/30);
if(data[i][j]>0) return
oneStep(i,j,_me);
for(let 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();
}

function computerAI(){
var myScore = [];
var computerScore = [];
var max = 0;
var u=0,v=0;
for(let i=0; i<15; i++){
myScore[i] = [];
computerScore[i] = [];
for(let j=0; j<15; j++){
myScore[i][j] = 0;
computerScore[i][j] = 0;
}
}
for(let i=0;i<15;i++){
for(var j=0;j<15;j++){
if(data[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] += 4500;
}
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] += 2200;
}else if(computerWin[k] == 4){
console.log('k:'+ k+'computerWin[k]:::'+computerWin[k])
computerScore[i][j] += 4322500;
}
}
}
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);
for(let 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;
}
}
play();
</script>
</body>
</html>
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值