求大神告知neighborCount还有哪里不对呢?
public class GameOfLife {
private String[][] Society;
private int Rows;
private int Columns;
public GameOfLife(int rows, int cols) {
Society = new String[rows][cols];
Rows = Society.length;
Columns = Society[0].length;
}
public int numberOfRows() {
return Rows;
}
public int numberOfColumns() {
return Columns;
}
public void growCellAt(int row, int col) {
Society[row][col] = "O";
}
public boolean cellAt(int row, int col) {
if(Society[row][col] != null){
return true;
}else{
return false;
}
}
public String toString() {
String text = "";
for(int i = 0; i < Society.length; i++ ){
for(int j = 0; j < Society[0].length; j++ ){
if(Society[i][j] == null)
Society[i][j] = ".";
System.out.print(Society[i][j]);
}
System.out.println();
}
return text;
}
public int neighborCount(int row, int col) {
int count = 0;
// up and left
if (Society[(row - 1 + Society.length) % Society.length][(col - 1 + Society[0].length) % Society[0].length] == "O") {
count++;
}
// up
if (Society[(row - 1 + Society.length) % Society.length][col] == "O") {
count++;
}
// up and right
if (Society[(row - 1 + Society.length) % Society.length][(col + 1 + Society[0].length) % Society[0].length] == "O") {
count++;
}
// right
if (Society[row][(col + 1 + Society[0].length) % Society[0].length] == "O") {
count++;
}
// left
if (Society[row][(col - 1 + Society[0].length) % Society[0].length] == "O") {
count++;
}
// down and right
if (Society[(row + 1 + Society.length) % Society.length][(col + 1 + Society[0].length) % Society[0].length] == "O") {
count++;
}
// down
if (Society[(row + 1 + Society.length) % Society.length][col] == "O"){
count++;
}
// down and left
if (Society[(row + 1 + Society.length) % Society.length][(col - 1 + Society[0].length) % Society[0].length] == "O") {
count++;
}
return count;
}
public void update() {
for (int i = 0; i < Society.length - 1; i++) {
for (int j = 0; j < Society[i].length - 1; j++) {
if(neighborCount(i,j) ==3){
Society[i][j] = "O";
}
if(neighborCount(i,j) ==2){
Society[i][j] = Society[i][j];
}
if(neighborCount(i,j) >3 || neighborCount(i,j) <2){
Society[i][j] = ".";
}
}
}
}
}