gpt4 book ai didi

java - 编程简介 : Conways Game of Life

转载 作者:行者123 更新时间:2023-12-02 04:55:38 26 4
gpt4 key购买 nike

我已经研究这段代码很长时间了,但我似乎无法弄清楚。我的代码的顶部部分用于预填充网格。但由于某种原因,我无法让我的形象按照预期移动和成长。请帮助我!!

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);
}

最佳答案

我注意到的两个问题:

  1. 您应该仅在完成所有单元格的更新后更新网格。
  2. 您的缩进已关闭,并且 } 放错了位置。重新格式化后,可以清楚地看到您没有覆盖所有情况,因此有些单元格没有获得新值。例如,如果一个单元格是活的,则只有当它有 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:

  1. If x is alive and has exactly 2 or 3 live neighbors, then x survives to the next generation. Otherwise it dies.
  2. 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com