欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

JavaScript实现网页版五子棋游戏

程序员文章站 2022-03-27 08:27:38
本文实例为大家分享了javascript实现网页版五子棋游戏的具体代码,供大家参考,具体内容如下学习js的第三天,跟着老师完成的五子棋小游戏,记录学习成果欢迎大佬们一起分享经验,批评指正。本程序主要通...

本文实例为大家分享了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="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实现网页版五子棋游戏

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

相关标签: js 五子棋