javascript - 我的人生游戏逻辑有问题吗?

标签 javascript html logic game-physics conways-game-of-life

我用 JavaScript 编写了康威的生命游戏,它似乎在屏幕上生成了一些东西,但它似乎没有遵循与康威生命游戏相同的逻辑或以相同的方式运行。我不知道出了什么问题,也不知道为什么它会这样运行,在我看来,我的代码应该遵循正确的规则。任何人都可以找出为什么它运行不正确吗?这是 JSFiddle https://jsfiddle.net/nw4Lw7z9/1/ 的链接

 //object constructor
function cell(){
    this.alive = Math.random() >0.8;    
    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(){
        grid.innerHTML = '';
        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 += " |";
                }               
            }
            grid.innerHTML = grid.innerHTML + rowCell + "\n";
        }       
    };  

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 = this.grid[ro + neighbour1][col+neighbour2];
        if(currentneighbour.alive){
            cell.neighbours++;
        }
       }
    }
};

GoL.prototype.updateAll = function(){
    for(var i=0; i<this.size-1;i++){
        for(var j=0; j<this.size-1;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(100);   

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

HTML

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Game of Life</title>
    <script src="cell.js" type="text/javascript"> </script>
  </head>
  <body>
    <pre id="grid">
    </pre>
  </body>
</html>

最佳答案

你的update函数很难理解。我已经把它清理干净了。尝试一下:

GoL.prototype.update = function(ro,col){    
    var cell = this.grid[ro][col];
    // Uncomment this. Need to re-set every iteration (and fix typo)
    cell.neighbours = 0;
    for(var i = 0; i < cell.checkneighbours.length; i++){
        // Get new indices
        var newRow = ro + cell.checkneighbours[i][0];
        var newCol = col + cell.checkneighbours[i][1];

        // Check that indices are in range
        if (newRow >= 0 && newRow < this.grid.length && newCol >= 0 && newCol < this.grid[newRow].length) {
            if(this.grid[newRow][newCol].alive){
                cell.neighbours++;
            }
        }
    }
};

关于javascript - 我的人生游戏逻辑有问题吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35654480/

相关文章:

javascript - 使用包含所有脚本的 jQuery 的动态内容

javascript - 单击 HTML5 视频控件上的播放并不能消除我的播放按钮覆盖

jquery - 将 html 元素的 id 传递给 php

c++ - 井字棋的阶乘数组

python - 从字符串中删除括号和特殊字符并转换列表(值以逗号分隔)

Javascript 在 Adob​​e Reader 中有效,但在 Nitropdf 中无效

javascript - OOP 意义上的 jQuery/JavaScript 类继承

在 JSP 中找不到 JavaScript 函数

html - div中的2个span,第一个自动宽度和溢出隐藏,第二个宽度自适应文本

c++ - 请给我一个 boost-multi-index stub - 带有 std::cout