Javascript 当前函数代码完成前调用的函数

Javascript 当前函数代码完成前调用的函数,javascript,Javascript,对这里描述的“5个猎人,3只兔子”问题进行了JS Fiddle实现: 我的代码在这里: 猎兔人 变量大小=5; var=newarray(); var arena=新阵列(侧,侧); 函数搜索器(行、列){ this.row=行; this.col=col; } 函数搜索器(行、列){ this.row=行; this.col=col; } 函数resetArena(){ 猎人=[]; 重画竞技场(); } 函数生成_表(){ //获取主体的引用 var body=document.getEl

对这里描述的“5个猎人,3只兔子”问题进行了JS Fiddle实现:

我的代码在这里:

猎兔人
变量大小=5;
var=newarray();
var arena=新阵列(侧,侧);
函数搜索器(行、列){
this.row=行;
this.col=col;
}
函数搜索器(行、列){
this.row=行;
this.col=col;
}
函数resetArena(){
猎人=[];
重画竞技场();
}
函数生成_表(){
//获取主体的引用
var body=document.getElementsByTagName(“body”)[0];
//创建一个元素和一个元素
var tbl=document.createElement(“表”);
tbl.setAttribute(“类”、“亨特表”);
var tblBody=document.createElement(“tbody”);
//创建所有单元格
对于(变量i=0;i=0)和((亨特罗-i)>=0)){
dRow2=arenaTBody.getElementsByTagName(“tr”)[hunterRow-i];
dCell2=dRow2.getElementsByTagName(“td”)[hunterCol-i];
dCell2.bgColor=“红色”;
}
如果((亨特罗+i=0)){
dRow3=arenaTBody.getElementsByTagName(“tr”)[hunterRow+i];
dCell3=dRow3.getElementsByTagName(“td”)[hunterCol-i];
dCell3.bgColor=“红色”;
}
如果((亨特罗-i)>=0)和((亨特罗+i)<!DOCTYPE html>
<html lang="en">

  <head>
    <meta charset="UTF-8">
    <title>Rabbit Hunters</title>
    <link rel="stylesheet" href="htmltable.css">

    <script language="JavaScript">
      var size = 5;
      var hunters = new Array();
      var arena = new Array(side, side);

      function hunter(row, col) {
        this.row = row;
        this.col = col;
      }

      function hunter(row, col) {
        this.row = row;
        this.col = col;
      }

      function resetArena() {
        hunters = [];
        redrawArena();
      }

      function generate_table() {
        // get the reference for the body
        var body = document.getElementsByTagName("body")[0];

        // creates a <table> element and a <tbody> element
        var tbl = document.createElement("table");
        tbl.setAttribute("class", "huntertable");
        var tblBody = document.createElement("tbody");

        // creating all cells
        for (var i = 0; i < size; i++) {
          // creates a table row
          var row = document.createElement("tr");

          for (var j = 0; j < size; j++) {
            // Create a <td> element and a text node, make the text
            // node the contents of the <td>, and put the <td> at
            // the end of the table row
            var cell = document.createElement("td");
            cell.addEventListener("click", cellClicked);
            cell.bgColor = "green";
            cell.innerHTML = "O";
            row.appendChild(cell);
          }

          // add the row to the end of the table body
          tblBody.appendChild(row);
        }

        // put the <tbody> in the <table>
        tbl.appendChild(tblBody);
        // appends <table> into <body>
        body.appendChild(tbl);
        // sets the border attribute of tbl to 2;
        tbl.setAttribute("border", "5");
      }

      function cellClicked() {

        var cellRow = this.parentNode.rowIndex;
        var cellCol = this.cellIndex;
        var cHunter = new hunter(cellRow, cellCol);

        if (exists(cHunter)) {
          // remove the hunter 
          remove(cHunter);

        } else {

          if (hunters.length == 5) {
            alert("A maximum of 5 hunters are allowed!");
            return;
          }

          hunters.push(cHunter);
          redrawArena();
        }
      }

      function exists(hunter) {
        for (var i = 0; i < hunters.length; i++) {
          if ((hunters[i].row == hunter.row) && (hunters[i].col == hunter.col))
            return true;
        }
        return false;

      }

      function remove(hunter) {
        for (var i = 0; i < hunters.length; i++) {
          if ((hunters[i].row == hunter.row) && (hunters[i].col == hunter.col)) {
            hunters.splice(i, 1);
            break;
          }
        }
        redrawArena();
      }

      function redrawArena() {

        var arenaTable = document.getElementsByTagName("tbl")[0];
        var arenaTBody = document.getElementsByTagName("tbody")[0];

        // reset arena
        for (var rowi = 0; rowi < size; rowi++) {
          for (var coli = 0; coli < size; coli++) {
            rRow = arenaTBody.getElementsByTagName("tr")[rowi];
            rCell = rRow.getElementsByTagName("td")[coli];
            rCell.innerHTML = "O";
            rCell.bgColor = "green";
          }
        }

        for (var hunterIndex = 0; hunterIndex < hunters.length; hunterIndex++) {
          // for each hunter mark the attacked territory:
          hunterRow = hunters[hunterIndex].row;
          hunterCol = hunters[hunterIndex].col;

          huntRow = arenaTBody.getElementsByTagName("tr")[hunterRow];
          huntCell = huntRow.getElementsByTagName("td")[hunterCol];
          huntCell.innerHTML = "H";
          huntCell.bgColor = "red";

          // horizontal and vertical
          for (var i = 0; i < size; i++) {
            hRow = arenaTBody.getElementsByTagName("tr")[hunterRow];
            hCell = hRow.getElementsByTagName("td")[i];
            hCell.bgColor = "red";

            vRow = arenaTBody.getElementsByTagName("tr")[i];
            vCell = vRow.getElementsByTagName("td")[hunterCol];
            vCell.bgColor = "red";
          }

          // diagonals
          for (var i = 1; i < size; i++) {

            if (((hunterRow + i) < size) && ((hunterCol + i) < size)) {
              dRow1 = arenaTBody.getElementsByTagName("tr")[hunterRow + i];
              dCell1 = dRow1.getElementsByTagName("td")[hunterCol + i];
              dCell1.bgColor = "red";
            }

            if (((hunterRow - i) >= 0) && ((hunterCol - i) >= 0)) {
              dRow2 = arenaTBody.getElementsByTagName("tr")[hunterRow - i];
              dCell2 = dRow2.getElementsByTagName("td")[hunterCol - i];
              dCell2.bgColor = "red";
            }

            if (((hunterRow + i) < size) && ((hunterCol - i) >= 0)) {
              dRow3 = arenaTBody.getElementsByTagName("tr")[hunterRow + i];
              dCell3 = dRow3.getElementsByTagName("td")[hunterCol - i];
              dCell3.bgColor = "red";
            }

            if (((hunterRow - i) >= 0) && ((hunterCol + i) < size)) {
              dRow4 = arenaTBody.getElementsByTagName("tr")[hunterRow - i];
              dCell4 = dRow4.getElementsByTagName("td")[hunterCol + i];
              dCell4.bgColor = "red";
            }
          }
        }
        alert("Checking for win ...");
        checkWin();
      }

      function checkWin() {
        // check arena for 5 hunters and 3 rabbits...
        if (hunters.length < 5)
            return;

                var arenaTable = document.getElementsByTagName("tbl")[0];
                var arenaTBody = document.getElementsByTagName("tbody")[0];

        var rabbits = 0;
        for (var rowi = 0; rowi < size; rowi++) {
          for (var coli = 0; coli < size; coli++) {
            rRow = arenaTBody.getElementsByTagName("tr")[rowi];
            rCell = rRow.getElementsByTagName("td")[coli];
            if (rCell.bgColor == "green") {
                rabbits++;            
            }

          }
        }
        if (rabbits == 3)
            alert("Congrats! You did it!")
      }

    </script>
  </head>

  <body onload="generate_table()">
    <h1>Rabbit Hunters</h1>
    <p>
      <ol>
        <li>The grid below represents a forest filled with rabbits (green).</li>
        <li>Hunters can attack horizontally and diagonally in all directions (like a chess queen).</li>
        <li>Once placed, hunters will kill all rabbits in their lines of sight (try clicking!).</li>
        <li>To remove hunters just click on them again.</li>
        <li>The Reset button clears the whole forest.</li>
      </ol>
      <strong>Can you place 5 hunters on the grid below so that they spare 3 rabbits (three green squares should remain)?</strong>
    </p>
    <p>
      <input type="button" value="Reset" onclick="resetArena()" />
    </p>
  </body>

</html>