(Java)如何存储x和y值以在for循环之外使用它们?
我正在用Processing 3编写Conway的生活游戏,我想存储x和y,这样方块就不会马上改变,但我不知道如何存储它以便以后使用。感谢您的帮助(Java)如何存储x和y值以在for循环之外使用它们?,java,for-loop,conways-game-of-life,storing-data,Java,For Loop,Conways Game Of Life,Storing Data,我正在用Processing 3编写Conway的生活游戏,我想存储x和y,这样方块就不会马上改变,但我不知道如何存储它以便以后使用。感谢您的帮助 void keyPressed() { for (int x = 0; x < 30; x++) { for (int y = 0; y < 30; y++) { int numNeighbours = numNeighbours(x,y); if (cells[x][y] == true) {
void keyPressed() {
for (int x = 0; x < 30; x++) {
for (int y = 0; y < 30; y++) {
int numNeighbours = numNeighbours(x,y);
if (cells[x][y] == true) {
if (numNeighbours > 3 || numNeighbours <= 1) { //underpopulation or overpopulation
}
}
else if (cells[x][y] == false) {
if (numNeighbours == 3) {
}
}
}
}
}
void键按下(){
对于(int x=0;x<30;x++){
对于(int y=0;y<30;y++){
int numNeighbours=numNeighbours(x,y);
如果(单元格[x][y]==true){
如果(numNeighbours>3 | | numNeighbours基于我对你的代码(以及生活游戏)的理解,你不需要存储x
和y
。你实际上需要做的是存储细胞状态变化的(x,y)
对
您可以通过创建对并将它们添加到列表中来实现这一点
但另一个想法是使用代表下一代游戏的第二个数组,并将所有新值放在那里;例如
for (int x = 0; x < 30; x++) {
for (int y = 0; y < 30; y++) {
int numNeighbours = numNeighbours(x,y);
if (cells[x][y] == true) {
if (numNeighbours > 3 || numNeighbours <= 1) {
cellsNext[x][y] = false;
} else {
cellsNext[x][y] = true;
}
}
else if (cells[x][y] == false) {
if (numNeighbours == 3) {
cellsNext[x][y] = true;
} else {
cellsNext[x][y] = false;
}
}
}
}
for(int x=0;x<30;x++){
对于(int y=0;y<30;y++){
int numNeighbours=numNeighbours(x,y);
如果(单元格[x][y]==true){
如果(numNeighbours>3 | | numNeighbours