gpt4 book ai didi

java - 康维斯生命游戏 - 滑翔机模式在第一次突变后不起作用?

转载 作者:行者123 更新时间:2023-11-30 07:24:36 24 4
gpt4 key购买 nike

我正在用 Java 开发 Conways Game of Life,但遇到了一些问题。

我的测试用例是滑翔机模式,但是当我尝试运行该程序时,滑翔机仅在第二代中第一次正确变异。第一次突变后的每一代都是不正确的,我不明白为什么。

Glider Should Mutate like this

This is what I get from running the code, the Glider shouldn't be stuck with a repeating pattern.

World.class(我的大部分方法都驻留在其中)

public class World extends Patterns
{

private char [][] world;
private char [][] tempWorld;
private int numRows;
private int numCols;

//World Constructor
public World (int r, int c)
{
this.numRows = r;
this.numCols = c;
world = new char [r][c];
tempWorld = new char [r][c];
initalizeWorld();

}

//Initializes the world and fills array with blank spaces
private void initalizeWorld()
{
// Initialize all indexes to ' '
for(int i = 0; i < numRows; i++)
{
for(int j = 0; j < numCols; j++)
{
world[i][j] = ' ';
}
}
}


//Creates and stores data in a temp world for the next Generation
public void nextGen()
{
for(int i = 0; i < numRows; i++)
{
for(int j = 0; j < numCols; j++)
{
changeCell(i,j);
}
}
world = tempWorld;
}

//Prints world with *'s as Cells and O's as dead cells
public void printWorld()

{
// Print every value inside array
for(int i = 0; i < numRows; i++)
{
for(int j = 0; j < numCols; j++)
{
if(world[i][j] == '*')
System.out.print(" ");

System.out.print(world[i][j]);

if(world[i][j] != '*')
{
System.out.print("O");
}

}
System.out.println(" ");
}
}

//Method check's for neighbors of a cell
public int getNeighbors(int x, int y)
{
int numNeighbors = 0;

for(int i = x - 1; i <= x + 1; i++)
{
for(int j = y - 1; j <= y + 1; j++)
{

if( (i >= 0) && (i < numRows) && (j < numCols) && (j >= 0))
{

if (world[i][j] == '*')
{
numNeighbors++;
}

}

}
}

//don't count the cell itself
return numNeighbors-1;

}

//Method uses getNeighbors to check whole array for every cells # of Neighbors
public void checkAllNeighbors()
{
for(int i = 0; i < numRows; i++)
{
for(int j = 0;j < numCols; j++)
{

//if(world[i][j] == '*')
{
int neighbors = getNeighbors(i, j);
System.out.println("The cell at (" + i + ", " + j + ") has " + neighbors + " neighbors");
}

}
}


}

//Method changes cell based on Neighbor Counts
public void changeCell (int r, int c)
{
int count = getNeighbors(r,c);

if (count == 3)
{
tempWorld [r][c] = '*';
}
else if (count >= 2 && count <= 3)
{
tempWorld [r][c] = '*';
}
else
{
tempWorld [r][c] = ' ';
}

}

//Gets world
public char [][] getWorld()
{
return world;
}

public void putGlider(int r, int c)
{
char pattern[][] = getGlider();


for (int i = 0; i < pattern.length; i++)
{
for (int j = 0; j < pattern[0].length; j++)
{

if ( (i + r < numRows) && (j + c < numCols) )
{
world[i + r][j + c] = pattern[i][j];

}
}
}
}

}

Patterns.class(当前保存滑翔机图案,但稍后将容纳更多)

public class Patterns 
{
private char[][] gliderArray = {{' ','*',' '},
{' ',' ','*'},
{'*','*','*'}};

//private char [][]

public char[][] getGlider()
{
return gliderArray;
}

}

test.class(我正在测试我的方法的地方)

    import java.util.Scanner;

public class test
{
public static void main(String[] args)
{
World test = new World (5,5);

test.getWorld();
test.putGlider(0,0);
test.printWorld();

System.out.println("Press enter to start generations!");
new Scanner(System.in).nextLine();

for(int i=0; i<10; i++)
{
test.nextGen();
System.out.println(" ");
test.printWorld();
try
{
Thread.sleep(2000);
} catch (InterruptedException e)
{
e.printStackTrace();
}
}

}

}

我已经在这个项目上工作了一段时间,但我真的陷入了困境。我不知道出了什么问题以及如何解决它,我开始有点生气。任何帮助将不胜感激!

谢谢!

基于 Sci Prog 建议的新 nextGen 方法,我不确定是否正确实现了循环中的循环。

public void nextGen()
{
for(int i = 0; i < numRows; i++)
{
for(int j = 0; j < numCols; j++)
{
changeCell(i,j);
}
}

for (int r=0; r < this.numRows; r++)
for (int c=0; c < this.numCols; c ++)
world[r][c] = tempWorld[r][c];
}

我还更改了我的changeCell方法,但新的输出比我之前的输出更加偏离。

最佳答案

第一个问题

线路world = tempWorld;复制 tempWorld引用数组到world 。因此,两个变量都引用内存中的同一对象:在第一次迭代之后,每当您更改 tempWorld 中的元素时,它还会更改 world 中的相同元素.

您应该手动复制每个元素,即使它是较长的代码

for (int r=0; r < this.numRows; r++)
for (int c=0; c < this.numCols; c ++)
world[r][c] = tempWorld[r][c];

第二个问题

在你的changeCell中方法,您不检查当前单元格是“”还是“*”。 (活细胞=2或3,死细胞=3)

 //Method changes cell based on Neighbor Counts
public void changeCell (int r, int c)
{
int count = getNeighbors(r,c);
const char current = world[r][c];

if ((current == ' ') && (count == 3))
{
tempWorld [r][c] = '*';
}
else if ((current=='*') && (count >= 2) && (count <= 3))
{
tempWorld [r][c] = '*';
}
else
{
tempWorld [r][c] = ' ';
}

}

关于java - 康维斯生命游戏 - 滑翔机模式在第一次突变后不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36989644/

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