简体   繁体   English

无法修复我的JavaScript代码中的错误

[英]Can't fix errors in my javascript code

I am currently programming the conway game of life in Javascript but have encountered a few errors which I can't seem to find the solution to. 我目前正在用Javascript编写生活中的conway游戏,但是遇到了一些我似乎找不到解决方法的错误。 the errors are: 错误是:

  • Uncaught TypeError: Cannot read property '1' of undefined - which is this line - var currentneighbour = cell.grid[ro + neighbour1][col+neighbour2]; 未捕获的TypeError:无法读取未定义的属性“ 1”-此行-var currentneighbour = cell.grid [ro + neighbour1] [col + neighbour2];
  • GoL.update - which is the same line as above GoL.update-与上面相同
  • GoL.updateAll - which is this line - this.update(i,j); GoL.updateAll-这是这一行-this.update(i,j);
  • (anonymous function) - which is this line at the bottom- gameoflife.updateAll(); (匿名函数)-这是底部的这一行-gameoflife.updateAll();

I don't know why I am getting these errors, and followed a tutorial which seems to work for them. 我不知道为什么会收到这些错误,并遵循了似乎对他们有用的教程。 here is my code for the game. 这是我的游戏代码。

    //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);    

It's a typo. 这是一个错字。

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

should be 应该

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

An unrelated nearby bug: you are setting cell.num_of_neighbours++; 附近不相关的错误:您正在设置cell.num_of_neighbours++; but trying to read cell.neighbours . 但是试图读取cell.neighbours

Fiddle with those changes applied: https://jsfiddle.net/nw4Lw7z9/1/ There's still something very wrong with the game logic, those patterns don't match Conway's at all, but at least it's giving some output now... 摆弄那些应用的更改: https : //jsfiddle.net/nw4Lw7z9/1/游戏逻辑仍然存在一些问题,这些模式根本不符合Conway的模式,但是至少现在它提供了一些输出...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM