目前正致力于尝试重建Conway的生命游戏的课程项目。这是我的代码的副本,我的主要问题是我的runRound()方法。我必须检查以查看每个单元格中有多少邻居有效(真实)或死亡(假)并相应地编辑我的新网格。然而,当我试图检查我的网格边缘并尝试实现代码以避免这种情况时,我一直走出界限,但我必须遗漏一些东西。谢谢你的任何建议!还是有点新鲜的!
package Programs;
public class GameOfLife {
private boolean[][] grid;
private int time;
public GameOfLife() {
grid = new boolean[10][10];
time = 0;
}
public GameOfLife(int row, int col) {
if (row < 1) {
row = 10;
}
if (col < 1) {
col = 10;
}
grid = new boolean[row][col];
time = 0;
}
public boolean[][] getGrid() {
boolean[][] newGrid = new boolean[grid.length][grid[1].length];
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[i].length; j++) {
newGrid[i][j] = grid[i][j];
}
}
return newGrid;
}
public int getTime() {
return time;
}
public void simpleSetUp(int[][] array) {
if (getTime() == 0) {
for (int row = 0; row < array.length; row++) {
if (array[0].length == 2) {
if (array[row][0] >= 0 && array[row][1] >= 0 && array[row][0] <= grid[0].length
&& array[row][1] <= grid[0].length) {
grid[array[row][0]][array[row][1]] = true;
}
}
}
}
}
public void clearGrid() {
this.time = 0;
for (int row = 0; row < grid.length; row++) {
for (int col = 0; row < grid[col].length; col++) {
grid[row][col] = false;
}
}
}
public void runRound() {
time++;
int live;
boolean[][] newGrid = getGrid();
for (int row = 0; row < grid.length; row++) {
for (int col = 0; col < grid[col].length; col++) {
live = 0;
if (row - 1 >= 0 || col - 1 >= 0) {
if(grid[row - 1][col - 1] == true){
live++;
}
}
if (row + 1 < grid.length || col + 1 < grid[col].length) {
if(grid[row + 1][col + 1] == true){
live++;
}
}
if (row - 1 >= 0 || col + 1 < grid[col].length) {
if(grid[row - 1][col + 1] == true ){
live++;
}
}
if (row + 1 < grid.length || col - 1 >= 0) {
if(grid[row + 1][col - 1] == true){
live++;
}
}
if (row + 1 < grid.length) {
if(grid[row + 1][col] == true){
live++;
}
}
if (row - 1 >= 0) {
if(grid[row - 1][col] == true){
live++;
}
}
if (col + 1 < grid[col].length) {
if(grid[row][col + 1] == true){
live++;
}
}
if (col - 1 >= 0) {
if(grid[row][col - 1] == true){
live++;
}
}
if (grid[row][col] == true && live == 2 || live == 3) {
newGrid[row][col] = true;
} else {
newGrid[row][col] = false;
}
}
}
for (int row = 0; row < grid.length; row++) {
for (int col = 0; col < grid[col].length; col++) {
live = 0;
if (row - 1 >= 0 || col - 1 >= 0) {
if(grid[row - 1][col - 1] == false){
live++;
}
}
if (row + 1 < grid.length || col + 1 < grid[col].length) {
if(grid[row + 1][col + 1] == false){
live++;
}
}
if (row - 1 >= 0 || col + 1 < grid[col].length) {
if(grid[row - 1][col + 1] == false){
live++;
}
}
if (row + 1 < grid.length || col - 1 >= 0) {
if(grid[row + 1][col - 1] == false ){
live++;
}
}
if (row + 1 < grid.length) {
if(grid[row + 1][col] == false ){
live++;
}
}
if (row - 1 >= 0) {
if(grid[row - 1][col] == false){
live++;
}
}
if (col + 1 < grid[col].length) {
if(grid[row][col + 1] == false){
live++;
}
}
if (col - 1 >= 0) {
if(grid[row][col - 1] == false){
live++;
}
}
if (live == 3) {
newGrid[row][col] = true;
} else {
newGrid[row][col] = false;
}
}
}
}
public void runGame(int foo) {
for (int poo = 0; poo < foo; poo++) {
runRound();
}
}
}
答案 0 :(得分:0)
您遇到的主要问题是您的边界检查是&#34;或&#34;逻辑:如果 行或 col 在边界内,则继续计算。你需要在界限中拥有两者;使用和。
如果您对此感到厌倦,可以通过使用 false 值填充整个网格来获得类似的效果,每侧一层。你的实际单元格在1到grid.length + 1的范围内; row / col 0和grid.length + 2是 false 。