This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center
                            
                        
                    
                
                已关闭8年。
            
        

以下代码有什么问题?

   for (var x=0;x<8;x++){
      for (var y=0;y<8;y++){
        table[y][x]=new Peon("black",x,y,table);
      }
   {


我知道这是有问题的,因为如果我从下面的代码中删除它,它将起作用,如果我将其保留在代码中,它将永远无法到达第二个“警报”。

  <HTML>
    <HEAD>
      <TITLE>Chess</TITLE>
      <SCRIPT LANGUAGE="JavaScript">
      var table = [];


      function Peon(color,posX,posY,board){
        this.color=color;
        this.posX=posX;
        this.posY=posY;
        this.board=board; //reference to the game board

        this.move = function(x,y){
          /*
          board[posY][posX]=null;
          board[y][x]= this;

          posX=x;
          posY=y;
          */
          alert("x "+x+" y "+y);
        };
        this.isValidMove = function(x,y){ return false;};
        return true;
      }

      //Board Inizialization
      for (var i=3;i<5;i++){
          table[i]=[];
      }

      for (var x=0;x<8;x++){
          for (var y=0;y<8;y++){
            table[y][x]=new Peon("black",x,y,table);
          }
      }


      var n = new Peon("black",0,0,table);


      function move(x, y) {
        alert("debug");

        n.move(x+1,y+1);
        alert("debug 2");

          //Check if there is a piece already selected
           //no Check if selection is valid
               //yes Select
               //no Do nothing
           //Click on owned piece?
               //yes Change selection if valid
               //no Is move valid?
                   //yes move/eat
                   //no Do nothing
      }


      </SCRIPT>
    </HEAD>
    <BODY>

      <INPUT id="b00" TYPE="button" style="background-color:EEEEEE" Value="Click" onClick="move(0,0)">

      <div id="writeHere"></div>
    </BODY>
  </HTML>

最佳答案

您确定吗?

for (var i=3;i<5;i++)
      table[i]=[];


因为它将仅在索引3和4处创建新数组。

顺便问一下,为什么您的Peon函数返回true?它应该返回一个实例,而不是true。

07-24 09:50
查看更多