- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经研究这段代码很长时间了,但我似乎无法弄清楚。我的代码的顶部部分用于预填充网格。但由于某种原因,我无法让我的形象按照预期移动和成长。请帮助我!!
import java.util.Random;
public class Life {
public static void main(String []args){
//Declaring and initializing grid variables
int gridSize = 200;
int cellSize = 3;
Grid grid = new Grid(gridSize, cellSize, "The Game of Life");
grid.setDelay(10);
Random r = new Random();
int aliveColor = 1;
int deadColor= 0;
int aliveCells;
int row = 0;
int column = 0;
int val = grid.getPos(row,column);
int generation;
int aliveNeighbors;
int cell;
//loop statement pre-populating the grid
for (row = 0; row <= gridSize-1; row++){
for(column = 0; column <= gridSize-1; column++){
if (r.nextInt(100) > 49){
grid.setPos(row, column, aliveColor);
}
else
grid.setPos(row, column, deadColor);
}
}
grid.initialize();
//Loop executing the rules of the Game of Life
do
{
row = 0;
column = 0;
generation = 0;
while (row <= gridSize-1){
while (column <= gridSize-1){
cell = grid.getPos(row, column);
aliveNeighbors = grid.matchingNeighbors(row, column,aliveColor);
if (cell == aliveColor)
{
if (aliveNeighbors == 2 || aliveNeighbors == 3){
val =1;
grid.setPos(row, column,val);
}
}
else if (cell == deadColor)
{
if (aliveNeighbors == 3){
val =1;
grid.setPos(row, column,val);
}
}
else{
val = 0;
grid.setPos(row,column,val);
}
column++;
}
row++;
grid.update();
}
grid.update();
generation++;
} while (generation >= 0);
}
}
编辑
//Loop executing the rules of the Game of Life
do
{
row = 0;
column = 0;
generation = 0;
while (row <= gridSize-1){
while (column <= gridSize-1){
cell = grid.getPos(row, column);
aliveNeighbors = grid.matchingNeighbors(row, column,aliveColor);
if (cell == aliveColor)
{
if (aliveNeighbors == 2 || aliveNeighbors == 3){
val =1;
grid.setPos(row, column,val);
}
if (aliveNeighbors ==1 || aliveNeighbors == 4){
val = 0;
grid.setPos(row,column,val);
}
}
else
{
if (cell == deadColor)
{
if (aliveNeighbors == 3){
val =1;
grid.setPos(row, column,val);
}
if (aliveNeighbors == 1 || aliveNeighbors == 2 || aliveNeighbors == 4){
val = 0;
grid.setPos(row, column, val);
}
}
}
column++;
}
row++;
}
grid.update();
generation++;
} while (generation >= 0);
}
EDIT****
do
{
row = 0;
column = 0;
generation = 0;
while (row <= gridSize-1){
while (column <= gridSize-1){
cell = grid.getPos(row, column);
aliveNeighbors = grid.matchingNeighbors(row, column,aliveColor);
if (cell == aliveColor)
{
if (aliveNeighbors == 2 || aliveNeighbors == 3){
grid.setPos(row, column, aliveColor);
} else {
grid.setPos(row,column, deadColor);
}
}
else
{
if (cell == deadColor){
if (aliveNeighbors == 3){
grid.setPos(row, column,aliveColor);
} else {
grid.setPos(row,column,deadColor);
}
}
}
column++;
}
row++;
}
grid.update();
generation++;
} while (generation >= 0);
}
最佳答案
我注意到的两个问题:
您的缩进已关闭,并且 }
放错了位置。重新格式化后,可以清楚地看到您没有覆盖所有情况,因此有些单元格没有获得新值。例如,如果一个单元格是活的,则只有当它有 2 或 3 个邻居时才处理这种情况,但是该 if
没有 else
,因此,一个活的单元格具有 1 个或 4 个或更多邻居的单元格永远不会更新。
您可能会认为在这种情况下代码不知何故会落入一般的“else”,但事实上,它永远不会到达那里。所有单元格值要么是“活”,要么是“死”。因此,构造如下:
if ( cell == aliveColor ) {
// Handle alive cell
} else if ( cell == deadColor ) {
// Handle dead cell
} else {
// Handle all other cases - but there are none!
}
相当于:
if ( cell == aliveColor ) {
// Handle alive cell
} else {
// Handle dead cell
}
最初的“else”永远不会到达,因为除了“aliveColor”和“deadColor”之外没有其他情况。
对您的编辑的回复:
if (cell == aliveColor)
{
if (aliveNeighbors == 2 || aliveNeighbors == 3){
val =1;
grid.setPos(row, column,val);
}
if (aliveNeighbors ==1 || aliveNeighbors == 4){
val = 0;
grid.setPos(row,column,val);
}
}
出于某种原因,您似乎认为每个单元格最多可以有 4 个邻居。再次查看实验室文档中的示例单元格:
⬜︎⬜︎⬛︎⬛︎⬜︎⬛︎⬛︎⬛︎⬜︎
This cell has 5 dead neighbors, and 3 live ones. In fact, each cell can have up to 8 neighbors - the diagonal neighbors also count!
But your rules were:
- If x is alive and has exactly 2 or 3 live neighbors, then x survives to the next generation. Otherwise it dies.
- If x is dead and has exactly 3 live neighbors, then x is alive in the next generation. Otherwise it remains dead
This otherwise translates directly to else
in programming. So instead of having a second if
with the complementary condition (if there are 1, 4,5,6,7 or 8 live neighbors...), just give the first if an else
:
if (aliveNeighbors == 2 || aliveNeighbors == 3) {
grid.setPos(row, column, aliveColor);
} else {
grid.setPos(row, column, deadColor);
}
当然,同样的事情也适用于死亡细胞的规则。
(请注意,我还删除了 val=1
并直接使用该值,val
是多余的。此外,我还使用了解释该值的名称比 1 和 0 (如果你想将游戏更改为使用绿色和红色怎么办?)。
我之前没有注意到的一件重要的事情:
do
{
row = 0;
column = 0;
generation = 0;
while (row <= gridSize-1){
while (column <= gridSize-1){
在行循环之前将行和列初始化为零。
这意味着在第一行末尾,column
保持 gridSize-1
并且不会再次设置为零。因此,实际上您只更新一行,然后只更新其他每一行的最后一列。
应该是:
do
{
row = 0;
generation = 0;
while (row <= gridSize-1){
column = 0;
while (column <= gridSize-1){
或者你可以使用 for 循环:
for ( row = 0; row < gridSize; row++ ) {
for ( column = 0; column < gridSize; column++ ) {
...
如果您选择这样做,请不要忘记从循环体内删除 row++
和 column++
,这样它们就不会递增两次。
关于java - 编程简介 : Conways Game of Life,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28797997/
我正在为一个学校项目开发康威的生命游戏。我不是直接寻找代码。我想找出我的代码有什么问题。 在康威的生命游戏中,如果一个细胞有 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
我是一名优秀的程序员,十分优秀!