[英]Java Game of Life implementation: Issues with board updates and infinite loops.
Trying to make a Game of Life program. 尝试制作“人生游戏”程序。 Having issues with loops and getting an accurate updating of the game board.
遇到循环问题并获得游戏板的准确更新。
Sorry but I am a beginner so I'm not so good at this. 抱歉,我是初学者,所以我不太擅长此事。 any help is appreciated.
任何帮助表示赞赏。
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();
}
}
}
The comments show off the problem areas. 注释显示了问题区域。 Replace the CAPITAL TEXT with the appropriate variables/numbers.
将CAPITAL TEXT替换为适当的变量/数字。
import java.util.Random; 导入java.util.Random; public class Life { public static void main(String[] args) { int gridSize = 200;
公共类Life {公共静态void main(String [] args){int gridSize = 200; int cellSize = 3;
int cellSize = 3; Grid grid = new Grid(gridSize, cellSize, "The Game of Life");
Grid grid = new Grid(gridSize,cellSize,“生命的游戏”); grid.setDelay(10);
grid.setDelay(10);
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();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.