javascript - issue with my game of life logic? -
i have coded conway's game of life in javascript, seems producing screen, doens't seem following same logic or running in same way @ conways game of life. don't know wrong , why running this, code seems me should following correct rules. can spot why not running correctly? here link 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 =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>
your update
function hard follow. i've cleaned up. give try:
gol.prototype.update = function(ro,col){ var cell = this.grid[ro][col]; // uncomment this. need re-set every iteration (and fix typo) cell.neighbours = 0; for(var = 0; < cell.checkneighbours.length; i++){ // new indices var newrow = ro + cell.checkneighbours[i][0]; var newcol = col + cell.checkneighbours[i][1]; // check indices in range if (newrow >= 0 && newrow < this.grid.length && newcol >= 0 && newcol < this.grid[newrow].length) { if(this.grid[newrow][newcol].alive){ cell.neighbours++; } } } };
Comments
Post a Comment