簡體   English   中英

無法修復我的JavaScript代碼中的錯誤

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

我目前正在用Javascript編寫生活中的conway游戲,但是遇到了一些我似乎找不到解決方法的錯誤。 錯誤是:

  • 未捕獲的TypeError:無法讀取未定義的屬性“ 1”-此行-var 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);    

這是一個錯字。

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/游戲邏輯仍然存在一些問題,這些模式根本不符合Conway的模式,但是至少現在它提供了一些輸出...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM