生命游戏 - 我不太明白如何正确检查邻居..
任何人都可以告诉我为什么我的右边框和上边框不能正常运作,这些内容被删除了吗?例如,如果我放置一个击中右边界的滑翔机,它会在最后一个之前创建一个正方形的一列,而不是创建一个包含最后一列的正方形,如果我放置一个击中顶部的滑块,它会创建一个包含顶行的正方形但它也继续排在最后一排。击中左边框或下边框的滑翔机虽然创建了一个正方形,包括左列或底行(分别)。这里的算法正在产生差异......我正在使用一个75x75的网格,这就是为什么有常数
public class Grid{
private int columns;
private int rows;
private Cell[][] grid;
public Grid(int column, int row){
this.columns = column;
this.rows = row;
grid = new Cell[row][column];
for (int r = 0; r < row; r++){
for (int c = 0; c < column; c++){
grid[r][c] = new Cell(r,c);
}
}
}
public boolean getCell(int r, int c){
return this.grid[r][c].isCellAlive();
}
public void giveCellLife(int r, int c){
this.grid[r][c].setCell(true);
}
public void killCell(int r, int c){
this.grid[r][c].setCell(false);
}
public void nextGridState(){
cellNeighbors();
for (int r = 0; r < rows; r++){
for (int c = 0; c < columns; c++){
if (grid[r][c].isCellAlive() && grid[r][c].getNeighbors() < 2){
grid[r][c].setCell(false);
}
if (grid[r][c].isCellAlive() && grid[r][c].getNeighbors() > 3){
grid[r][c].setCell(false);
}
if (!grid[r][c].isCellAlive() && grid[r][c].getNeighbors() == 3){
grid[r][c].setCell(true);
}
}
}
reset();
}
public void reset(){
for (int r = 0; r < rows; r++){
for (int c = 0; c < columns; c++){
grid[r][c].removeNeighbors();
}
}
}
public void cellNeighbors(){
for (int r = 0; r < rows; r++){
for (int c = 0; c < columns; c++){
try {
if (grid[r-1][c+1].isCellAlive() == true){
grid[r][c].addNeighbor();
}
if (grid[r][c+1].isCellAlive() == true){
grid[r][c].addNeighbor();
}
if (grid[r+1][c+1].isCellAlive() == true){
grid[r][c].addNeighbor();
}
if (grid[r-1][c].isCellAlive() == true){
grid[r][c].addNeighbor();
}
if (grid[r+1][c].isCellAlive() == true){
grid[r][c].addNeighbor();
}
if (grid[r-1][c-1].isCellAlive() == true){
grid[r][c].addNeighbor();
}
if (grid[r][c-1].isCellAlive() == true){
grid[r][c].addNeighbor();
}
if (grid[r+1][c-1].isCellAlive() == true){
grid[r][c].addNeighbor();
}
} catch (ArrayIndexOutOfBoundsException e){
}
if (r == 0){
try {
if (grid[r][c+1].isCellAlive() == true){
grid[r][c].addNeighbor();
}
if (grid[r][c-1].isCellAlive() == true){
grid[r][c].addNeighbor();
}
if (grid[r+74][c+1].isCellAlive() == true){
grid[r][c].addNeighbor();
}
if (grid[r+74][c-1].isCellAlive() == true){
grid[r][c].addNeighbor();
}
if (grid[r+74][c].isCellAlive() == true){
grid[r][c].addNeighbor();
}
if (grid[r+1][c+1].isCellAlive() == true){
grid[r][c].addNeighbor();
}
if (grid[r+1][c].isCellAlive() == true){
grid[r][c].addNeighbor();
}
if (grid[r+1][c-1].isCellAlive() == true){
grid[r][c].addNeighbor();
}
} catch (ArrayIndexOutOfBoundsException e){
}
}
if (c == 74){
try {
if (grid[r-1][c-1].isCellAlive() == true){
grid[r][c].addNeighbor();
}
if (grid[r][c-1].isCellAlive() == true){
grid[r][c].addNeighbor();
}
if (grid[r+1][c-1].isCellAlive() == true){
grid[r][c].addNeighbor();
}
if (grid[r-1][c].isCellAlive() == true){
grid[r][c].addNeighbor();
}
if (grid[r+1][c].isCellAlive() == true){
grid[r][c].addNeighbor();
}
if (grid[r-1][0].isCellAlive() == true){
grid[r][c].addNeighbor();
}
if (grid[r][0].isCellAlive() == true){
grid[r][c].addNeighbor();
}
if (grid[r+1][0].isCellAlive() == true){
grid[r][c].addNeighbor();
}
} catch (ArrayIndexOutOfBoundsException e){
}
}
}
}
}
}
public class Cell{
private int x;
private int y;
public int neighbors;
private boolean living;
public Cell(int x, int y){
this.x = x;
this.y = y;
neighbors = 0;
living = false;
}
public int getNeighbors(){
return this.neighbors;
}
public void addNeighbor(){
neighbors++;
}
public void removeNeighbors(){
neighbors = 0;
}
public boolean isCellAlive(){
return this.living;
}
public void setCell(boolean isCellAlive){
this.living = isCellAlive;
}
}
我已经看了几个小时了,另一双眼睛真的很好=) 感谢
编辑**这里是问题的一个屏幕......