// JavaScript Documentvar chessBoard=[];var me=true;var over=false;//赢法数组var wins = [];//赢法的统计数组var myWin=[];//统计我方的var computerWin=[];//统计计算机的for(var i=0;i<15;i++){ chessBoard[i]=[]; for(var j=0;j<15;j++) { chessBoard[i][j]=0; }}for(var i = 0; i < 15; i++) { wins[i] = []; for (var j = 0; j < 15; j++) { wins[i][j] = []; }}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 = 14; j>3; 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 = 0; j < 11; j++) { for (var k = 0; k < 5; k++) { wins[i+k][j+k][count] = true; } count++; }}var count = 0;console.log(count);//初始化为0for(var i=0;i<count;i++){ myWin[i]=0; computerWin[i]=0; }var chess=document.getElementById('chess');var ctx=chess.getContext('2d');ctx.strokeStyle="black";/*..............................棋盘的画法..................*/for(var 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();}/*..........................棋子的画法..........................*/var oneStep=function(i,j,me){ ctx.beginPath(); ctx.arc(15+i*30,15+j*30,13,0,Math.PI*2,true); ctx.closePath(); if(me) { ctx.fillStyle="black"; ctx.fill(); } else { ctx.fillStyle="white"; ctx.fill(); } ctx.stroke()}/*............................实现落棋..............................*/chess.onclick=function(e){ if(over) { 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); if(me) { chessBoard[i][j]=1; } else { chessBoard[i][j]=2; } me=!me; 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; } } } } }
添加回答
举报
0/150
提交
取消