可能的重复:
javascript 中的康威生命游戏(最好的 sol
我正在为康威的生命游戏编写代码。我正在使用 2 个数组;一个用于老一代,一个用于第二代。当我尝试实施规则时,控制台中出现错误:
TypeError: Cannot read property '-1' of undefined
请告诉我,我应该如何更改代码以便可以删除具有 -1 值的相邻单元格?
规则是:
生命游戏的宇宙是一个由方形细胞组成的无限二维正交网格,每个细胞都处于两种可能状态中的一种,活的或死的。每个单元格都与其八个相邻单元格交互,这些相邻单元格是水平、垂直或对角相邻的单元格。在时间的每一步,都会发生以下转换:
任何少于两个活邻居的活细胞都会死亡,好像是由于人口不足造成的。
任何有两三个活邻居的活细胞都可以活到下一代。
任何有超过三个活邻居的活细胞都会死亡,就好像过度拥挤一样。
任何只有三个活邻居的死细胞都会变成活细胞,就像通过繁殖一样。
初始模式构成系统的种子。第一代是通过将上述规则同时应用于种子中的每个细胞而创建的——出生和死亡同时发生,而发生这种情况的离散时刻有时称为滴答声(换句话说,每一代都是前一个)。这些规则继续被重复应用以创造更多的世代。
这是代码:
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();
}
}
}
//Error is in this part of the code
//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;
}
改变带来
即使对代码进行了更改,我的回答也不正确。请告诉我为什么?
conway.maingame.prototype.neighbors = function(x, y)
{
count = 0;
if(x > 0 && y > 0 && this.get(x + 1, y + 1))
{
console.log(this.get(x + 1, y + 1), "vallue neighbor");
count++;
console.log(count);
}
if(x > 0 && y > 0 && this.get(x + 1, y))
{
console.log(this.get(x + 1, y), "vallue neighbor");
count++;
console.log(count);
}
if(x > 0 && y > 0 && this.get(x + 1, y - 1))
{
console.log(this.get(x + 1, y - 1), "vallue neighbor");
count++;
console.log(count);
}
if(x > 0 && y >=0 && this.get(x, y - 1))
{
console.log(this.get(x + 1, y - 1), "vallue neighbor");
count++;
console.log(count);
}
if(x > 0 && y > 0 && this.get(x - 1, y - 1))
{
console.log(this.get(x + 1, y - 1), "vallue neighbor");
count++;
console.log(count);
}
if(x > 0 && y > 0 && this.get(x - 1, y))
{
console.log(this.get(x + 1, y - 1), "vallue neighbor");
count++;
console.log(count);
}
if(x > 0 && y > 0 && this.get(x - 1, y + 1))
{
console.log(this.get(x + 1, y - 1), "vallue neighbor");
count++;
console.log(count);
}
if(x > 0 && y > 0 &&this.get(x, y + 1))
{
console.log(this.get(x + 1, y - 1), "vallue neighbor");
count++;
console.log(count);
}
return count;
}