Java生活游戏实现:板更新和无限循环的问题。
试图制作一个程序。循环出现问题,并获得游戏板的准确更新 对不起,我是初学者,所以我不太擅长这个。感谢您的帮助Java生活游戏实现:板更新和无限循环的问题。,java,loops,buffer,infinite,conways-game-of-life,Java,Loops,Buffer,Infinite,Conways Game Of Life,试图制作一个程序。循环出现问题,并获得游戏板的准确更新 对不起,我是初学者,所以我不太擅长这个。感谢您的帮助 import java.util.Random; public class Life { public static void main(String[] args) { int gridSize = 200; int cellSize = 3; Grid grid = new Grid(gridSize, cellSize, "T
import java.util.Random;
public class Life {
public static void main(String[] args) {
int gridSize = 200;
int cellSize = 3;
Grid grid = new Grid(gridSize, cellSize, "The Game of Life");
grid.setDelay(10);
int aliveCells;
int aliveColor = 1;
int deadColor = 0;
while (true) {
for (column = 0;; column++) {
for (row = 0;; row++) {
grid.getPos(row, column);
aliveCells = grid.matchingNeighbors(row, column, aliveColor);
if (aliveCells == 2 || aliveCells == 3) {
grid.setPos(row, column, aliveColor);
} else {
grid.setPos(row, column, deadColor);
}
grid.update();
}
}
}
这些评论显示了问题所在。用适当的变量/数字替换大写文本 导入java.util.Random; 公共阶级生活{ 公共静态void main(字符串[]args){ int gridSize=200; int-cellSize=3; 网格=新网格(网格大小,单元大小,“生命的游戏”); 网格设置延迟(10)
intalivecells;
int aliveColor=1;
int deadColor=0;
//这将创建一个gridSizeXgridSize矩阵,表示电路板。
int bufferBoard[][]=新的int[gridSize][gridSize];
while(true){
//更新由矩阵表示的缓冲板。
for(int column=0;column
你想完成什么?例如,为什么你需要它成为一个无限循环?如果你想得到有用的答案,你可能需要提供更多的信息。使用while(true){…}
将给你一个无限循环,不确定你还想要什么。你已经有了一个无限循环。你还可以提供你的HTML或创建一个摆弄你的代码吗?()你的for{}语句没有终止符。这意味着你永远不会进入grid.update();看起来你在制作一个生活型的游戏程序?我得到一个看起来像静态电视的屏幕。我的实验室说这不正确。是的。听起来不错。如果没有辅助网格,你会有奇怪的事情发生。你能粘贴更新的内容吗()功能?另外,粘贴更多的主功能可能会有帮助,这样我就可以准确地输入需要做的解释。我编辑了它。希望这就是你要求的。实验室说网格已经定义好了,所以我不需要辅助网格。该死的,我不知道你的意思。哈哈。我想我错在哪里了我可能需要另一个if-else语句。如果该单元格以前已死亡,但有3个活动的相邻单元格,它将变为活动。我对此进行了说明吗?行和列的gridSize都是200
int aliveCells;
int aliveColor = 1;
int deadColor = 0;
//This creates a gridSizeXgridSize matrix, representing the board.
int bufferBoard[][] = new int[gridSize][gridSize];
while (true) {
//Update the buffer board, represented by an matrix.
for (int column = 0; column < gridSize; ++column) {
for (int row = 0; row < gridSize; ++row) {
int isAlive = grid.getPos(row, column); //Assigning current cell's alive-ness to variable to be used later.
aliveCells = grid.matchingNeighbors(row, column, aliveColor);
if(isAlive == aliveColor){ //Make all the checks for a living cell.
if (aliveCells == 2 || aliveCells == 3) {
bufferBoard[row][column] = aliveColor; //This cell will be alive.
} else {
bufferBoard[row][column] = deadColor; //This cell will be dead.
}
} else { //The current cell is dead.
if(aliveCells == 3){
bufferBoard[row][column] = aliveColor;
}
}
}
}
//Time to populate the actual board.
//Update the buffer board, represented by an matrix.
for (int column = 0; column < gridSize; ++column) {
for (int row = 0; row < gridSize; ++row) {
grid.getPos(row, column);
if (bufferBoard[row][column] == aliveColor) { //Check the location of the bufferBoard at the specified row and column.
grid.setPos(row, column, aliveColor);
} else {
grid.setPos(row, column, deadColor);
}
grid.update();
}
}
}