please tell me .. how should i edit the code so that the neighbor cell with -1 value can be removed
here is the code
window.conway =
{
};
window.conway.maingame =
{
};
conway.maingame = function(width, height)
{
window.a = [];
this.width = width;
this.height = height;
this.map = new Array(width);
for( i = 0; i < this.width; i++)
{
this.map[i] = new Array(height);
}
console.log(this.map, "map")
}
conway.maingame.prototype.randomize = function()
{
for( y = 0; y < this.height; y++)
{
console.log("enter for loop")
for( x = 0; x < this.width; x++)
{
if(Math.random() > .5)
{
i = true;
}
else
{
i = false;
}
console.log("enter function")
this.set(x, y, i);
}
}
}
conway.maingame.prototype.set = function(x, y, val)
{
x = x % this.width;
y = y % this.height;
this.map[x][y] = val;
console.log(this.map, "map2");
}
conway.maingame.prototype.get = function(x, y)
{
x = x % this.width;
y = y % this.height;
return this.map[x][y];
}
conway.maingame.prototype.neighbors = function(x, y)
{
n = 0;
if(this.get(x + 1, y + 1))
{
n++;
}
if(this.get(x + 1, y))
{
n++;
}
if(this.get(x + 1, y - 1))
{
n++;
}
if(this.get(x, y - 1))
{
n++;
}
if(this.get(x - 1, y - 1))
{
n++;
}
if(this.get(x - 1, y))
{
n++;
}
if(this.get(x - 1, y + 1))
{
n++;
}
if(this.get(x, y + 1))
{
n++;
}
return n;
}
conway.maingame.prototype.newgeneration = function()
{
var newMap = new Array(this.width);
for( i = 0; i < this.width; i++)
{
newMap[i] = new Array(this.height);
}
for(var y = 0; y < this.height; y++)
{
for(var x = 0; x < this.width; x++)
{
console.log("enter all for")
newMap[x][y] = this.get(x, y);
if(this.neighbors(x, y) = undefined)
{
for( k = 0; k < this.width+1; k++)
{
arr[k]=[];
for( f = 0; f < this.height+1; f++)
{
arr[j]=this.neigbors(x,y);
arr.pop();
}
}
}
[size="7"][size="7"][b]//Error is in this part of the code [/b][/size][/size]
//Rule 1: any live cell with fewer than two live neighbors dies
if(this.get(x, y) == true && this.neighbors(x, y) < 2)
{
newMap[x][y] = false;
}
//Rule 2: Any live cell with two or three live neighbours lives on to the next generation
if(this.get(x, y) == true && this.neighbors(x, y) == 2 || this.neighbors(x, y) == 3)
{
newMap[x][y] = true
}
//Rule 3: any live cell with more than three live neighbors dies
if(this.get(x, y) == true && this.neighbors(x, y) > 3)
{
newMap[x][y] = false;
}
//Rule 4: any dead cell with exactly three live neighbors becomes a live cell
if(this.get(x, y) == false && this.neighbors(x, y) == 3)
{
newMap[x][y] = true;
}
}
}
this.map = newMap;
}

New Topic/Question
Reply


MultiQuote




|