四糸乃 2015-01-27 20:32
浏览 888

生命的游戏neighborCount(int row, int col)问题

求大神告知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] = ".";
            }
        }
    }

}

}

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 ubuntu虚拟机打包apk错误
    • ¥199 rust编程架构设计的方案 有偿
    • ¥15 回答4f系统的像差计算
    • ¥15 java如何提取出pdf里的文字?
    • ¥100 求三轴之间相互配合画圆以及直线的算法
    • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
    • ¥15 名为“Product”的列已属于此 DataTable
    • ¥15 安卓adb backup备份应用数据失败
    • ¥15 eclipse运行项目时遇到的问题
    • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发