<!DOCTYPE html>
<html>
<head>
<title>五子棋小游戏</title>
<meta charset="UTF-8">
</head>
<body>
<canvas id="canvas" width="600" height="600" onclick="exec(event)" ></canvas>
<button onclick="reStart();">重新开始</button>
<button onclick="back();">悔棋</button>
</body>
<script type="text/javascript"> var c=document.getElementById("canvas"); var cxt=c.getContext("2d"); var data = []; var clickCount = 0; var canvasWidth = 600; var interval = 20; var isEnd = false; var colorW = '#DAA520'; var colorH = '#000'; init(); function init() { for (var i = 0; i < canvasWidth;) { cxt.beginPath(); cxt.lineWidth="1"; cxt.strokeStyle="#8B4513"; cxt.moveTo(i,0); cxt.lineTo(i,canvasWidth); cxt.stroke(); cxt.beginPath(); cxt.lineWidth="1"; cxt.moveTo(0,i); cxt.lineTo(canvasWidth,i); cxt.stroke(); i = i+interval; } } function exec(e) { if(isEnd) return; var x1=e.clientX; var y1=e.clientY; var newX,newY; for (var i = 0; i < canvasWidth;) { if (x1>=i&&x1<i+interval/2) newX = i; if (x1>=i+interval/2&&x1<i+interval) newX = i+interval; if (y1>=i&&y1<i+interval/2) newY = i; if (y1>=i+interval/2&&y1<i+interval) newY = i+interval; i = i+interval; } if (!checkDataExists(newX,newY)) { var isTrue = true; if (clickCount%2==0) { cxt.fillStyle=colorW; }else{ cxt.fillStyle=colorH; isTrue = false; } cxt.beginPath(); cxt.arc(newX,newY,interval/2,0,Math.PI*2,true); cxt.closePath(); cxt.fill(); data.push({'x':newX,'y':newY,'isTrue':isTrue}); clickCount++; if(isFinish(newX,newY,isTrue)){ isEnd = true; if (isTrue) alert('黄棋赢了'); else alert('黑棋赢了'); } }else{ alert("当前点已经存在"); } } function reStart() { cxt.clearRect(0,0,canvasWidth,canvasWidth); init(); data = []; clickCount=0; isEnd = false; } function back() { cxt.clearRect(0,0,canvasWidth,canvasWidth); init(); clickCount--; data.pop(); isEnd = false; for (var i = 0; i < clickCount; i++) { cxt.beginPath(); cxt.fillStyle = i%2==0 ? colorW:colorH; cxt.arc(data[i].x,data[i].y,10,0,Math.PI*2,true); cxt.closePath(); cxt.fill(); } } function checkDataExists(x,y,isTrue){ for (var i = 0; i < data.length; i++) { if (data[i].x ==x && data[i].y == y && (typeof(isTrue) == "undefined" || data[i].isTrue == isTrue)) return true; } return false; } function isFinish(x1,y1,isTrue) { x2 = x3 = x4 = x5 = x1; y2 = y3 = y4 = y5 = y1; x2 = x1>=5*interval ? x1-5*interval : 0; lineCount = 0; for (var i = 0; i < 10; i++) { tempx = x2+interval*i; if (checkDataExists(tempx,y2,isTrue)) { lineCount++; if (lineCount==5) break; }else lineCount=0; } if (lineCount>=5) return true; if (y1>=5*interval) y3 = y1-5*interval; else y3=0; lineCount = 0; for (var i = 0; i < 10; i++) { tempy = y3+interval*i; if (checkDataExists(x3,tempy,isTrue)) { lineCount++; if (lineCount==5) break; }else lineCount=0; } if (lineCount>=5) return true; x4 = x1-5*interval; y4 = y1-5*interval; lineCount = 0; for (var i = 0; i < 10; i++) { tempy = y4+interval*i; tempx = x4+interval*i; if (checkDataExists(tempx,tempy,isTrue)) { lineCount++; if (lineCount==5) break; }else lineCount=0; } if (lineCount>=5) return true; x5 = x1-5*interval; y5 = y1+5*interval; lineCount = 0; for (var i = 0; i < 10; i++) { tempy = y5-interval*i; tempx = x5+interval*i; if (checkDataExists(tempx,tempy,isTrue)) { lineCount++; if (lineCount==5) break; }else lineCount=0; } if (lineCount>=5) return true; } </script>
</html>