JavaScript realization of gobang games

  • 2021-09-04 23:15:21
  • OfStack

This article example for everyone to share JavaScript to achieve 5 sub-chess game specific code, for your reference, the specific content is as follows

HTML Part


<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>5 Sub-chess </title>
 <style>
 * {
  padding: 0;
  margin: 0;
 }
 
 body{
 padding-top: 100px;
 
 }
 
 .main {
  width: 600px;
  height: 600px;
  margin: 0 auto;
  background-color: burlywood;
 }

 .col {
  position: relative;

  width: 40px;
  height: 40px;
  box-sizing: border-box;
  border: 1px solid #000;
  border-collapse: collapse;
  /*border-radius: 20px;*/

 }

 .row {
  position: relative;
  display: flex;
  height: 40px;
  /*background-color: brown;*/
 }

 .col-action {
  background-color: blue;
 }

 .col-actionA {
  /*background-color: white;*/

 }

 .col-actionB {
  /*background-color: black;*/

 }
 
 .col-actionA::before{
  content: "";
  position: absolute;
  width: 30px;
  height: 30px;
  background-color: white;
  border-radius: 99px;
  top:4.5px;
  left:4.5px;
  box-shadow: 0 0 2px rgba(0,0,0,0.5);

 }
 .col-actionB::before{
  content: "";
  width: 30px;
  height: 30px;
  background-color: black;
  border-radius: 99px;
  position: absolute;
  top:4.5px;
  left:4.5px;
  box-shadow: 0 0 2px rgba(128,128,128,0.5);
 }
 
 .hq{
 width: 600px;
  height: 600px;
  margin: 0 auto;
 }

 </style>
 
 <script type="text/javascript" src="js/demo03.js" ></script>
 
</head>
<body>

<div class="main">
 
 <div class="qipan" id="qipan">


 </div>
 <div class="hq"><button id="hq"> Regret chess </button></div>
</div>

</body>
</html>

JavaSrcipt


window.onload = function(){
 
 var busz = new Array();
 
 //div Click event 
 var ansj = function () {
  
 const x = this.getAttribute("col");
 const y = this.getAttribute("row");
//  console.log(x, y, nowPlayer)
 
 if (nowPlayer) {
  qjck[this.getAttribute("row")][this.getAttribute("col")] = 1;
  this.classList.add("col-actionA");
  nowPlayer = !nowPlayer;
 } else {
  qjck[this.getAttribute("row")][this.getAttribute("col")] = 2;
  this.classList.add("col-actionB");
  nowPlayer = !nowPlayer;
 }
 
 busz.push(this);
 
 var js = pdsl(y,x);
 
 if(js)
 {
 setTimeout(function(){
 alert(" Game over ");
 location.reload(); // Refresh browser 
 
 },50);
 
 }
  
 this.onclick = null;
  
 }
 
 // Judge whether it is over 
 var pdsl = function(x,y){

 var sx=1,zy=1,zs=1,ys=1,t=1;
 
 // Upper 
 for(t=1;t<=5;t++){
 
 if(x-t < 0)
 break; 
 
 console.log(" Upper "+zy);
 if(qjck[x-t][y]==qjck[x][y] && qjck[x-t][y]!=0)
 sx++;
 else
 break;
 }
 
 // Under 
 for(t=1;t<=5;t++){
 
 if(Number(x)+t >= 10)
 break;
 
 console.log(" Under "+zy);
 if(qjck[Number(x)+t][y]==qjck[Number(x)][y] && qjck[Number(x)+t][y]!=0)
 sx++;
 else
 break;
 }
 
 // Left 
 for(t=1;t<=5;t++){
 
 if(y-t < 0)
 break; 
 
 console.log(" Left "+zy);
 if(qjck[x][y-t]==qjck[x][y] && qjck[x][y-t]!=0)
 zy++;
 else
 break;
 }
 
 // Right 
 for(t=1;t<=5;t++){
 
 if(Number(y)+t >= 10)
 break;
 
 console.log(" Right "+zy);
 if(qjck[x][Number(y)+t]==qjck[x][y] && qjck[x][Number(y)+t]!=0)
 zy++;
 else
 break;
 }
 
 // Upper left 
 for(t=1;t<=5;t++){
 
 if(x-t < 0)
 break; 
 
 console.log(" Upper left "+zy);
 if(qjck[x-t][y-t]==qjck[x][y] && qjck[x-t][y-t]!=0)
 zs++;
 else
 break;
 } 
 
 // Lower right 
 for(t=1;t<=5;t++){
 
 if(Number(x)+t >= 10 || Number(y)+t >= 10)
 break; 
 
 console.log(" Lower right "+zy);
 if(qjck[Number(x)+t][Number(y)+t]==qjck[x][y] && qjck[Number(x)+t][Number(y)+t]!=0)
 zs++;
 else
 break;
 }
 
 // Upper right 
 for(t=1;t<=5;t++){
 
 if(x-t < 0 || Number(y)+t >= 10)
 break; 
 
 console.log(" Upper right "+zy);
 if(qjck[x-t][Number(y)+t]==qjck[x][y] && qjck[x-t][Number(y)+t]!=0)
 ys++;
 else
 break;
 } 
 
 // Lower left 
 for(t=1;t<=5;t++){
 
 if(Number(x)+t >= 10 || y-t < 0)
 break; 
 
 console.log(" Lower right "+zy);
 if(qjck[Number(x)+t][y-t]==qjck[x][y] && qjck[Number(x)+t][y-t]!=0)
 ys++;
 else
 break;
 }
 
 
 console.log(sx + " " + zy + " " + zs + " " + ys);
 
 if(sx == 5 || zy==5 || zs==5 || ys==5)
 return true;
 else
 return false;
 
 }

 var nowPlayer = 0;

 // Chessboard array 
 var qjck = Array();
 
 //div
 var piece = document.createElement("div");
 piece.id = "piece";

 // Get div
 var qipan = document.getElementById("qipan");

 // Generate a chessboard 
 for (let r = 0; r < 15; r++) {
 let newrow = document.createElement("div");
 newrow.id = "row" + r;
 newrow.classList.add("row")

 let arrCol = Array()
 qjck.push(arrCol)

 for (let c = 0; c < 15; c++) {
  arrCol.push(0)
  let newcol = document.createElement("div");

  newcol.id = "col" + c;

  newcol.classList.add("col");

  newcol.setAttribute("row", r);
  newcol.setAttribute("col", c)
  newrow.appendChild(newcol)

  newcol.onclick = ansj;

 }


 // console.log(newrow)
 qipan.appendChild(newrow)
 }
 
 // Regret chess 
 var hq = document.getElementById("hq");
 
 hq.onclick = function(){
 
 if(busz.length <= 0)
 return;
 
 var divt = busz.pop();
 divt.onclick = ansj;
 
 divt.classList.remove("col-actionA");
 divt.classList.remove("col-actionB");
 
 qjck[divt.getAttribute("row")][divt.getAttribute("col")] = 0;
 
 nowPlayer = !nowPlayer;
 
// console.log(qjck);
 console.log(divt);
 }
 
 
}

More interesting classic game implementation topics, share with you:

C + + Classic games summary

python Classic Games Summary

python Tetris Game Collection

JavaScript classic games can't stop playing

javascript Classic Games Summary


Related articles: