文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

JavaScript如何实现网页版五子棋游戏

2023-06-20 18:23

关注

这篇文章将为大家详细讲解有关JavaScript如何实现网页版五子棋游戏,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

本程序主要通过三部分实现:

棋盘绘制
2.鼠标交互
3.输赢判断

<!DOCTYPE html><html><head>  <title>    canvastest    </title></head><body>     <h2> canvas</h2>     <canvas id="canvas"width="400"height="400">     </canvas>     <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script>  <script>   var canv=document.getElementById("canvas");   var ctx=canv.getContext("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.moveTo(10+20*i,10);       ctx.lineTo(10+i*20,370);       ctx.moveTo(10,20*i+10);       ctx.lineTo(370,i*20+10);} ctx.stroke();  //鼠标交互;  $("#canvas").click(function(event)      {      console.log(event.offsetX)      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               {                for(var w = 0; w < 3 ; w ++)                {                 if(matrix[mtxPosX][mtxPosY+w+1] != matrix[mtxPosX][mtxPosY])                 {                  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                   {                    winFlag = 1;                   }                  }                 }                }                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;                 }                }               }              }              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(winFlag ==1){         if(bow)         alert("black win!");         else                         alert("white win!");              }      });  </script></body></html>

JavaScript如何实现网页版五子棋游戏

关于“JavaScript如何实现网页版五子棋游戏”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯