2016-02-25 70 views
-1

我目前正在使用Javascript编写生活中的康威游戏,但遇到了一些我无法找到解决方案的错误。的错误是:无法修复我的javascript代码中的错误

  • 未捕获的类型错误:无法读取属性 '1' 的未定义的 - 这是此行 - 变种currentneighbour = cell.grid [RO + neighbour1] [COL + neighbour2];
  • GoL.update - 与上面相同的行
  • GoL.updateAll - 这是这一行 - this.update(i,j);
  • (匿名函数) - 这是在底部的这条线gameoflife.updateAll();

我不知道为什么我得到这些错误,并遵循一个似乎适用于他们的教程。 这里是我的游戏代码。

//object constructor 
function cell(){ 
    this.alive = Math.random() >0.7;  
    this.neighbours = 0; //number of live neighbours 
    this.checkneighbours = [[-1,-1],[-1,0],[0,-1],[-1,1],[1,-1],[1,0],[0,1],[1,1]]; 
} 


function GoL(size){ 
    this.size = size; 
    this.grid = this.makeGrid(size); 
};  

    GoL.prototype.makeGrid = function(size){ 
     var grid = []; 
     for(var i=0; i<size; i++){ 
      var row=[]; 
      for(var j =0; j<size; j++){ 
       row.push(new cell()); 
      } 
      grid.push(row); 
     } 
      return grid; 
    }; 

    GoL.prototype.drawGrid = function(){ 
     console.log("\033[2J"); 
     for(var i=0;i<this.size;i++){ 
      var row =this.grid[i]; 
      var rowCell=""; 
      for(var j=0;j<this.size;j++){ 
       var cell = row[j]; 
       if(cell.alive){ 
        rowCell += "X|"; 
       }else{ 
        rowCell += " |"; 
       }    
      } 
      console.log(rowCell); 
     }  
    }; 

GoL.prototype.underpopulation = function(ro,col){ 
    var cell = this.grid[ro][col]; 
    if(cell.neighbours <2){ 
     return true; 
    }else{ 
     return false; 
    } 
}; 
GoL.prototype.overpopulation = function(ro,col){ 
    var cell = this.grid[ro][col]; 
    if(cell.neighbours >3){ 
     return true; 
    }else{ 
     return false; 
    } 
}; 

GoL.prototype.backtolife = function(ro,col){ 
    var cell = this.grid[ro][col]; 
    if(cell.neighbours ===3 && !cell.alive){ 
    return true; 
    }else{ 
     return false; 
    } 
}; 

GoL.prototype.update = function(ro,col){  
    var cell = this.grid[ro][col]; 
    cell.num_of_neighbours = 0; 
    for(var i =0; i<cell.checkneighbours.length; i++){ 
     var checkneighbour = cell.checkneighbours[i]; 
     var neighbour1 = checkneighbour[0]; 
     var neighbour2 = checkneighbour[1]; 
     if(neighbour1>=0 && neighbour1 < this.size && neighbour2 >=0 && neighbour2 < this.size){ 
     var currentneighbour = cell.grid[ro + neighbour1][col+neighbour2]; 
     if(currentneighbour.alive){ 
      cell.num_of_neighbours++; 
     } 
     } 
    } 
}; 

GoL.prototype.updateAll = function(){ 
    for(var i=0; i<this.size;i++){ 
     for(var j=0; j<this.size;j++){ 
      this.update(i,j); 
     } 
    } 
} 

GoL.prototype.cellstatus = function(ro,col){ 
    var cell = this.grid[ro][col]; 
    if(this.underpopulation(ro,col) || this.overpopulation(ro,col)){ 
     cell.alive = false; 
    }else if(this.backtolife(ro,col)){ 
     cell.alive = true; 
    } 
}; 

GoL.prototype.allcellstatus = function(ro,col){ 
    for(var i=0; i<this.size;i++){ 
     for(var j=0; j<this.size;j++){ 
      this.cellstatus(i,j); 
     } 
    } 
}; 


var gameoflife = new GoL(40); 

var interval = setInterval(function(){ 
    gameoflife.drawGrid(); 
    gameoflife.updateAll(); 
    gameoflife.allcellstatus(); 
},1000);  
+0

为了方便起见,我已经做了一小段代码(只改变它的输出写入页面而不是控制台,这使得调试一个严重的PITA):https://jsfiddle.net/nw4Lw7z9/ –

回答

2

这是一个错字。

var currentneighbour = cell.grid[ro + neighbour1][col + neighbour2];

应该

var currentneighbour = this.grid[ro + neighbour1][col + neighbour2];

一个无关的错误附近:要设置cell.num_of_neighbours++;但想读cell.neighbours

拨弄应用这些更改:https://jsfiddle.net/nw4Lw7z9/1/有还是一些非常错误的游戏逻辑,这些模式不匹配康威的所有,但至少它现在给一些输出...