查询“康威的生命游戏”算法
Query about "Conway's Game of Life" algorithm
可能的重复项:
康威游戏的生活在javascript(最好的溶胶
我正在为康威的生命游戏编写代码。我正在接受 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;
}
使用 ==
进行比较(甚至===
)。使用=
进行分配。
这一行是错误的:
if(this.neighbors(x, y) = undefined)
应该是比较,而不是分配。
编辑:修复第二个错误
我 99% 确定您标记的行不是导致问题的行。请检查您的主机错误并仔细验证行号。
我很确定问题是因为您正在尝试引用不存在的单元格。考虑边缘周围的单元格 - 外面没有邻居。您需要修改代码以检查是否没有超出数组的边界。
...
if (y > 0 && this.get(x, y - 1)) {
n++;
}
... etc. ...
额外提示:始终将开口大括号放在同一条线上。
编辑:请参阅我对此问题重复项的回答。
值得通过像jsHint这样的检查工具来运行你的代码:
Line 11: window.a = [];
Missing "use strict" statement.
Line 19: console.log(this.map, "map")
Missing semicolon.
Line 20: }
Missing semicolon.
Line 24: for( y = 0; y < this.height; y++)
Missing "use strict" statement.
Line 26: console.log("enter for loop")
Missing semicolon.
Line 29: if(Math.random() > .5)
A leading decimal point can be confused with a dot: '.5'.
Line 37: console.log("enter function")
Missing semicolon.
Line 42: }
Missing semicolon.
Line 46: x = x % this.width;
Missing "use strict" statement.
Line 51: }
Missing semicolon.
Line 56: x = x % this.width;
Missing "use strict" statement.
Line 59: }
Missing semicolon.
Line 63: n = 0;
Missing "use strict" statement.
Line 97: }
Missing semicolon.
Line 102: var newMap = new Array(this.width);
Missing "use strict" statement.
Line 112: console.log("enter all for")
Missing semicolon.
Line 115: if(this.neighbors(x, y) = undefined)
Expected a conditional expression and instead saw an assignment.
Line 129: if(this.get(x, y) == true && this.neighbors(x, y) < 2)
Expected '===' and instead saw '=='.
Line 135: if(this.get(x, y) == true && this.neighbors(x, y) == 2 || this.neighbors(x, y) == 3)
Expected '===' and instead saw '=='.
Line 135: if(this.get(x, y) == true && this.neighbors(x, y) == 2 || this.neighbors(x, y) == 3)
Expected '===' and instead saw '=='.
Line 135: if(this.get(x, y) == true && this.neighbors(x, y) == 2 || this.neighbors(x, y) == 3)
Expected '===' and instead saw '=='.
Line 137: newMap[x][y] = true
Missing semicolon.
Line 142: if(this.get(x, y) == true && this.neighbors(x, y) > 3)
Expected '===' and instead saw '=='.
Line 148: if(this.get(x, y) == false && this.neighbors(x, y) == 3)
Expected '===' and instead saw '=='.
Line 148: if(this.get(x, y) == false && this.neighbors(x, y) == 3)
Expected '===' and instead saw '=='.
Line 156: }
Missing semicolon.
Line 8: conway.maingame = function(width, height)
'conway' is not defined.
Line 15: for( i = 0; i < this.width; i++)
'i' is not defined.
Line 15: for( i = 0; i < this.width; i++)
'i' is not defined.
Line 15: for( i = 0; i < this.width; i++)
'i' is not defined.
Line 17: this.map[i] = new Array(height);
'i' is not defined.
Line 19: console.log(this.map, "map")
'console' is not defined.
Line 22: conway.maingame.prototype.randomize = function()
'conway' is not defined.
Line 24: for( y = 0; y < this.height; y++)
'y' is not defined.
Line 24: for( y = 0; y < this.height; y++)
'y' is not defined.
Line 24: for( y = 0; y < this.height; y++)
'y' is not defined.
Line 26: console.log("enter for loop")
'console' is not defined.
Line 27: for( x = 0; x < this.width; x++)
'x' is not defined.
Line 27: for( x = 0; x < this.width; x++)
'x' is not defined.
Line 27: for( x = 0; x < this.width; x++)
'x' is not defined.
Line 31: i = true;
'i' is not defined.
Line 35: i = false;
'i' is not defined.
Line 37: console.log("enter function")
'console' is not defined.
Line 38: this.set(x, y, i);
'x' is not defined.
Line 38: this.set(x, y, i);
'y' is not defined.
Line 38: this.set(x, y, i);
'i' is not defined.
Line 44: conway.maingame.prototype.set = function(x, y, val)
'conway' is not defined.
Line 49: console.log(this.map, "map2");
'console' is not defined.
Line 54: conway.maingame.prototype.get = function(x, y)
'conway' is not defined.
Line 61: conway.maingame.prototype.neighbors = function(x, y)
'conway' is not defined.
Line 61:
Too many errors. (38% scanned).
这里真的有太多的错误,无法将其缩小到特定问题......
- 如何使用phaser使html5游戏在移动设备浏览器上运行
- 循环比赛位置算法
- javascript扫雷器floodfill算法不能正常工作
- 如何有效地将游戏数据存储在URL查询字符串中
- Javascript游戏输入失去焦点
- 如何在p5.js中管理多智力竞赛游戏的多个屏幕
- setInterval游戏循环的范围问题
- Node JS中的排名系统算法
- 查找仅适用于原始图像的图像放大算法的名称
- 在数组的 2/3 上调用自身的排序算法
- 为网络游戏制作动画
- 重置游戏和获胜者问题
- 帆布游戏的滞后运动
- Javascript单独的游戏窗口
- Luhn算法的实现
- 算法:从数组(javascript/angular)中按当前日期获取上一个和下一个事件
- Javascript猜测游戏-计数器不工作
- 查询“康威的生命游戏”算法
- 游戏物理-如何加快这种移动算法?在Javascript中
- 生命的游戏:试图用新的算法来提高计算效率,但没有成功.为什么?