JavaScript实现⽹页版五⼦棋游戏本⽂实例为⼤家分享了JavaScript实现⽹页版五⼦棋游戏的具体代码,供⼤家参考,具体内容如下
学习js的第三天,跟着⽼师完成的五⼦棋⼩游戏,记录学习成果欢迎⼤佬们⼀起分享经验,批评指正。本程序主要通过三部分实现:
1.棋盘绘制
2.⿏标交互
3.输赢判断
<!DOCTYPE html>
<html>
<head>
<title>
canvastest
</title>
</head>
<body>
<h1> canvas</h1>
<canvas id="canvas"width="400"height="400">
</canvas>
<script src="cdn.bootcdn/ajax/libs/jquery/3.6.0/jquery.js"></script>
<script>
var ElementById("canvas");
var Context("2d");
ctx.strokeStyle="black";
var bow=0;
//画出棋盘;
var matrix=[
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
]
;
ctx.beginPath();
for(var i=0;i<19;i++){
ctx.lineTo(10+i*20,370);
ctx.lineTo(370,i*20+10);
}
ctx.stroke();
//⿏标交互;
console.log(bow);
var arcPosX,arcPosY;
var mtxPosX,mtxPosY;
for(var x=0;x<19;x++)
{
if((Math.abs(event.offsetX-(10+x*20)))<10)
{
arcPosX=10+x*20;
mtxPosX=x;
}
if((Math.abs(event.offsetY-(10+x*20)))<10)
{
arcPosY=10+x*20;
mtxPosY=x;
}
}
if(matrix[mtxPosX][mtxPosY] == 0)
{
bow=!bow;
ctx.beginPath();
if(bow){
ctx.fillStyle="Black";
ctx.arc(arcPosX,arcPosY,10,0,Math.PI*2,false);
matrix[mtxPosX][mtxPosY]=1;
}
else{
ctx.fillStyle="White";
ctx.arc(arcPosX,arcPosY,10,0,Math.PI*2,false);
ctx.stroke();
matrix[mtxPosX][mtxPosY]=2;
}
ctx.fill();
}
//实现输赢判断
var winFlag=0;
if(winFlag==0){
if(matrix[mtxPosX-1][mtxPosY] == matrix[mtxPosX][mtxPosY])
{
if(matrix[mtxPosX-2][mtxPosY] == matrix[mtxPosX][mtxPosY])
{
if(matrix[mtxPosX-3][mtxPosY] == matrix[mtxPosX][mtxPosY])
{
if(matrix[mtxPosX-4][mtxPosY] == matrix[mtxPosX][mtxPosY])
{
winFlag = 1;
}
else
{
if(matrix[mtxPosX+1][mtxPosY] == matrix[mtxPosX][mtxPosY]) {
winFlag = 1;
}
else
{
winFlag = 0;
}
}
}
else
{
for(var w = 0; w < 2 ; w ++)
{
if(matrix[mtxPosX+w+1][mtxPosY] != matrix[mtxPosX][mtxPosY]) {
winFlag = 0;
break;
}
else
{
winFlag = 1;
else
{
for(var w = 0; w < 3 ; w ++)
{
if(matrix[mtxPosX+w+1][mtxPosY] != matrix[mtxPosX][mtxPosY]) {
winFlag = 0;
break;
}
else
{
winFlag = 1;
}
}
}
}
else
{
for(var w = 0; w < 4 ; w ++)
{
if(matrix[mtxPosX+w+1][mtxPosY] != matrix[mtxPosX][mtxPosY]) {
winFlag = 0;
break;
}
else
{
winFlag = 1;
}
}
}
if(matrix[mtxPosX][mtxPosY-1] == matrix[mtxPosX][mtxPosY])
{
if(matrix[mtxPosX][mtxPosY-2] == matrix[mtxPosX][mtxPosY])
{
if(matrix[mtxPosX][mtxPosY-3] == matrix[mtxPosX][mtxPosY])
{
if(matrix[mtxPosX][mtxPosY-4] == matrix[mtxPosX][mtxPosY])
{
winFlag = 1;
}
else
{
if(matrix[mtxPosX][mtxPosY+1] == matrix[mtxPosX][mtxPosY]) {
winFlag = 1;
}
else
{
winFlag = 0;
}
}
}
else
{
for(var w = 0; w < 2 ; w ++)
{
if(matrix[mtxPosX][mtxPosY+w+1] != matrix[mtxPosX][mtxPosY]) {
winFlag = 0;
break;
}
else
{
winFlag = 1;
}
}
}
}
else
{
winFlag = 0;
break;
}
else
{
winFlag = 1;
}
}
}
}
else
{
for(var w = 0; w < 4 ; w ++)
{
if(matrix[mtxPosX][mtxPosY+w+1] != matrix[mtxPosX][mtxPosY])
{
winFlag = 0;
break;
}
else
{
winFlag = 1;
}
}
}
if(matrix[mtxPosX-1][mtxPosY-1] == matrix[mtxPosX][mtxPosY])
{
if(matrix[mtxPosX-2][mtxPosY-2] == matrix[mtxPosX][mtxPosY])
{
if(matrix[mtxPosX-3][mtxPosY-3] == matrix[mtxPosX][mtxPosY])
{
if(matrix[mtxPosX-4][mtxPosY-4] == matrix[mtxPosX][mtxPosY])
{
winFlag = 1;
}
else
{
if(matrix[mtxPosX+1][mtxPosY+1] == matrix[mtxPosX][mtxPosY])
{
winFlag = 1;
}
else
{
winFlag = 0;
}
}
}
else
{
for(var w = 0; w < 2 ; w ++)
{
if(matrix[mtxPosX+w+1][mtxPosY+w+1] != matrix[mtxPosX][mtxPosY]) {
winFlag = 0;
break;
}
else
{
winFlag = 1;
}
}
}
}
else
{
for(var w = 0; w < 3 ; w ++)
{
if(matrix[mtxPosX+w+1][mtxPosY+w+1] != matrix[mtxPosX][mtxPosY]) {
winFlag = 0;
break;
}
}
}
}
网页游戏小游戏else
{
for(var w = 0; w < 4 ; w ++)
{
if(matrix[mtxPosX+w+1][mtxPosY+w+1] != matrix[mtxPosX][mtxPosY]) {
winFlag = 0;
break;
}
else
{
winFlag = 1;
}
}
}
if(matrix[mtxPosX-1][mtxPosY+1] == matrix[mtxPosX][mtxPosY])
{
if(matrix[mtxPosX-2][mtxPosY+2] == matrix[mtxPosX][mtxPosY])
{
if(matrix[mtxPosX-3][mtxPosY+3] == matrix[mtxPosX][mtxPosY])
{
if(matrix[mtxPosX-4][mtxPosY+4] == matrix[mtxPosX][mtxPosY])
{
winFlag = 1;
}
else
{
if(matrix[mtxPosX+1][mtxPosY-1] != matrix[mtxPosX][mtxPosY])
{
winFlag = 0;
}
else
{
winFlag = 1;
}
}
}
else
{
for(var w = 0; w < 2 ; w ++)
{
if(matrix[mtxPosX+w+1][mtxPosY-w-1] != matrix[mtxPosX][mtxPosY]) {
winFlag = 0;
break;
}
else
{
winFlag = 1;
}
}
}
}
else
{
for(var w = 0; w < 3 ; w ++)
{
if(matrix[mtxPosX+w+1][mtxPosY-w-1] != matrix[mtxPosX][mtxPosY]) {
winFlag = 0;
break;
}
else
{
winFlag = 1;
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论