- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在使用JavaScript编写Conways生活游戏,但是遇到了一些我不知道如何解决的错误。想知道是否有人可以帮助我解决这些问题?
当前在Google Chrome浏览器中显示4个错误,具体如下:
//object constructor
function cell(){
this.alive = Math.random() >0.7;
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(){
console.log("\033[2J");
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 += " |";
}
}
console.log(rowCell);
}
};
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<this.checkneighbours.length; i++){
var checkneighbour = this.checkneighbours[i];
var neighbour1 = direction[0];
var neighbour2 = direction[1];
if(neighbour1>=0 && neighbour1 < this.size && neighbour2 >=0 && neighbour2 < this.size){
var currentneighbour = this.grid[ro + neighbour1][col+neighbour2];
if(currentneighbour.alive){
cell.num_of_neighbours++;
}
}
}
};
GoL.prototype.updateAll = function(){
for(var i=0; i<this.size;i++){
for(var j=0; j<this.size;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(40);
var interval = setInterval(function(){
gameoflife.drawGrid();
gameoflife.updateAll();
gameoflife.allcellstatus();
},1000);
最佳答案
一些想法
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++) { // change this to cell
var direction = cell.checkneighbours[i]; // change this to cell, rename 'checkneighbour' to 'direction'
var neighbour1 = direction[0];
var neighbour2 = direction[1];
if (neighbour1 >= 0 && neighbour1 < this.size && neighbour2 >= 0 && neighbour2 < this.size) {
var currentneighbour = this.grid[ro + neighbour1][col + neighbour2]; // add here some error checking for out of range!
if (currentneighbour.alive) {
cell.num_of_neighbours++;
}
}
}
};
关于javascript - 用Java编写Conways Life of Life错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35627349/
我正在为一个学校项目开发康威的生命游戏。我不是直接寻找代码。我想找出我的代码有什么问题。 在康威的生命游戏中,如果一个细胞有 3 个存活的邻居,它就会从死亡状态变为存活状态。如果它有两个或三个活着的邻
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and th
我构建了 Conways 生命游戏,但算法运行不正常。我为此混合使用了 Js 和 Jquery。我的程序所做的是逐个单元格地遍历整个棋盘,检查单元格的邻居,并通过检查它的邻居将游戏规则应用于每个单元格
这是一个有点理论性的问题。在编程作业中,约翰康威告诉我们要实现生命游戏。作为一项附加任务,我们被要求修改程序,以便它可以检测最多四代的模式重复。例如,给定这个特定的游戏“种子”,程序应该像这样: -
我目前正在使用 C++ 开发 Conway 的 Game of Life 的控制台版本。 Repo Link 问题是我的 Draw() 方法没有尽可能快地绘制下一代。 我记得在某处读到过,与获取控制台
我用 Java 编写了康威生命游戏的代码,现在我正在尝试将其转换为 C。代码非常简单,但是我在尝试创建适当的 结构时遇到了问题。 在 Java 中,如果我有实例变量: private boolean
我正在尝试制作康威生命游戏的简单版本,其中计算机生成矩形网格并填充代表“活”细胞的矩形。我遇到的问题是我无法在第一个图案之后清除网格,因此所有图案都在同一个网格上生成,它看起来像一大团彩色矩形。 这是
我的查询真的很难描述,所以我会尽量简洁地解释它。 在康威的生命游戏中,假设我有一张这样的 map : _ _ _ _ _ _ _ _ _ _ U _ R Y
我已经研究这段代码很长时间了,但我似乎无法弄清楚。我的代码的顶部部分用于预填充网格。但由于某种原因,我无法让我的形象按照预期移动和成长。请帮助我!! import java.util.Random;
所以我阅读了 Conways Game of Life 并尝试使用 PyGame 来实现它。 我试着让它面向对象。它的工作方式是我有一个细胞实例列表,然后检查它们有多少邻居,然后根据它们的邻居存活或死
这是康威用 Python 制作的生命游戏模拟的代码片段。出于检查它是否有效的目的,它非常简单 - 但它没有。无法弄清楚为什么,但据我所知,它与更新有关。 感谢任何关于它为什么更新它的方式的输入: py
我正在尝试完成 Hofstadter-Conway $10,000 sequence Scala 中的任务 Rosetta Code .我想尽可能地使用惯用的 Scala,但是我在不使用原始递归的情况
我在使用 C++ 为 Conway 的生命游戏编码时遇到了一些问题。我觉得我有一个好的开始,我只需要一些方向。首先,当我尝试运行该程序时,我总是得到一个空白的控制台。而且我也不太确定为板上没有完整八个
看上了康威的《生命游戏》,开始尝试用python写。目前我还没有为程序的边界编写任何代码,所以我只是在寻求帮助我现在拥有的东西。我似乎在初始化“信号灯”编队时遇到了麻烦。它没有像它应该的那样振荡,而是
我已经编写了 Conway's Game of Life 的简单实现使用 Store comonad(见下面的代码)。我的问题是网格生成从第五次迭代开始明显变慢。我的问题是否与我正在使用 Store
public void actionPerformed(ActionEvent e){ grid=new JButton[length+20][width+20];
我正在尝试用 Java 编写康威生活游戏,但它没有按预期运行。我的意思是,静物有效,但眼罩和轮船不有效。 这是我的算法的代码,播种由一个单独的鼠标监听器处理(按预期工作)。 int[][] s
I am trying to write a program that implements Conway's game of life on a 20x60 cell board. The grid
我正在使用在 involuntaryexercise.com 上找到的 reni2D 库构建这个程序我创建了一个由二维行列组成的网格,单击其中的每个方 block 即可打开和关闭。现在,我正在尝试制作
我正在尝试为 Conway 的人生游戏编写一个计算邻居的方法。如果一个死细胞与 2 或 3 个活细胞相邻,它应该会复活。但是,我的代码没有正确计算所有邻居。如果我给出输入坐标 (10, 10), (1
我是一名优秀的程序员,十分优秀!