生命游戏中的主要课程不起作用

时间:2014-04-07 16:54:45

标签: java main conways-game-of-life

我试图解决老师的生命游戏问题。那场比赛的规则是:

任何活着的邻居少于两个的活细胞都会死亡,就像人口不足一样。任何有两三个活着的邻居的活细胞都活在下一代。任何有三个以上活着的邻居的活细胞都会死亡,就像过度拥挤一样。具有正好三个活邻居的任何死细胞变成活细胞,就好像通过繁殖一样。

我的代码有两个问题 - 首先,我的主要课程似乎没有工作。其次,我通过许多if else语句来解决问题。是否有更简洁的方法为我的getNeighbors()方法编写异常?

谢谢!

import java.util.Random;

public class GameOfLife {
final static int ROWS = 6;
final static int COLUMNS = 7;
String[][] simulator;
private Random randomGenerator;

public GameOfLife() {
    simulator = new String[ROWS][COLUMNS];
    randomGenerator = new Random();
}

public void fillSpot (int row, int column) {
    simulator [row][column] = "O";
} 
private void deleteSpot (int row, int column) {
    simulator[row][column] = "";
}
// Do I need the above methods? really?
public void randomSimulation() {
    for (int i = 0; i <= ROWS; i++) {
        for (int j = 0; j <= COLUMNS; j++) {
            int random = randomGenerator.nextInt(1);
            if (random == 1) {
                fillSpot(i,j);
            }
        }
    }
}

private void getNeighbors (int row, int column) {
    int neighbors = 0;
    if (row < ROWS && row > 0 && column < COLUMNS && column > 0) {
        for (int i = row - 1; i <= row + 1; i++) {
            for (int j = column - 1; j <= column + 1; j++) {
                String temp = simulator[i][j];
                if (temp.contains("O")) {
                    neighbors++;
                }
            }
        }
    }

    if (row > ROWS || column > COLUMNS || row < 0 || column < 0) {
    }

    else if (row == ROWS && column < COLUMNS && column != 0) {
        for (int i = row - 1; i <= ROWS; i++) {
            for (int j = column - 1; j <= column + 1; j++) {
                String temp = simulator[i][j];
                if (temp.contains("O")) {
                    neighbors++;
                }
            }
        }
    }
    else if (row < ROWS && column == COLUMNS && row != 0) {
        for (int i = row - 1; i <= row + 1; i++) {
            for (int j = column - 1; j <= COLUMNS; j++) {
                String temp = simulator[i][j];
                if (temp.contains("O")) {
                    neighbors++;
                }
            }
        }
    }
    else if (row == 0 && column < COLUMNS && column != 0) {
        for (int i = 0; i <= row + 1; i++) {
            for (int j = column - 1; j <= COLUMNS + 1; j++) {
                String temp = simulator[i][j];
                if (temp.contains("O")) {
                    neighbors++;
                }
            }
        }
    }
    else if (row == 0 && column == COLUMNS) {
        for (int i = row; i <= row + 1; i++) {
            for (int j = column - 1; j <=COLUMNS; j++) {
                String temp = simulator[i][j];
                if (temp.contains("O")) {
                    neighbors++;
                }
            }
        }
    }
    else if (column == 0 && row < ROWS && row != 0) {
        for (int i = row - 1; i <= row + 1; i++) {
            for (int j = column; j <= COLUMNS + 1; j++) {
                String temp = simulator[i][j];
                if (temp.contains("O")) {
                    neighbors++;
                }
            }
        }
    }
    else { 
        for (int i = row; i <= row + 1; i++) {
            for (int j = column; j <= column + 1; j++) {
                String temp = simulator[i][j];
                if (temp.contains("O")) {
                    neighbors++;
                }
            }
        }
    }
    // for row == 0 && column == 0
    if (simulator [row][column].contains("O")) {
        neighbors--;
    }
    simulator[row][column] += " " + neighbors;
}
//There are wayyy too manyy clauses here for me to be comfortable. There's got to be    a way to do this cleaner

private void nextGenPlanning() {
    for (int i = 0; i <= ROWS; i++) {
        for (int j = 0; j <= COLUMNS; j++) {
            getNeighbors(i,j);
        }
    }
}

private void nextGen() {
    nextGenPlanning();
    for (int i = 0; i <= ROWS; i++) {
        for (int j = 0; j <= COLUMNS; j++) {
            String temp = simulator[i][j];
            if (temp.charAt(temp.length()) <= 1 ||  temp.charAt(temp.length()) >= 4) {
                deleteSpot(i,j);
            }
            else {
                fillSpot (i,j);
            }
        }
    }
}

public String toString() {
    String string = "";
    for (int i = 0; i < ROWS; i++) {
        string = string + "|";
        for (int j = 0; j < COLUMNS; j++) {
            String temp = simulator[i][j];
            string = string + temp.charAt(0);
        }
        string = string + "|\n";
    }
    return string;
}

public String simulate (int numOfTrials) {
    String string = "";
    for (int i = 0; i <= numOfTrials; i++) {
        nextGen();
        string += toString();
    }
    return string;
}

public void main (String [] args) {
    randomSimulation();
    System.out.println(simulate(2));
}   
}

2 个答案:

答案 0 :(得分:1)

首先,你有:

public void main (String [] args) {
    randomSimulation();
    System.out.println(simulate(2));
}   

你应该:

public static void main (String[] args) {
    GameOfLife game = new GameOfLife();
    game.randomSimulation();
    System.out.println(game.simulate(2));
}   

其次,对于getNeighbors,首先要考虑'get'方法通常返回一个值。如果您计算邻居的数量,请考虑:

public int getNeighbors(int x, int y) {
  int neighbors = 0;
  int leftX = Math.max(x-1, 0); 
  int rightX = Math.min(x+1, COLUMNS); 
  int topY = Math.max(y-1, 0); 
  int bottomY = Math.min(y+1, ROWS); 

  for (int i=leftX; i < rightX; i++) {
    for (int j=topY; j < bottomY; j++) {
      if (simulator[i][j].contains('O')) { // Notice I'm using a char here, see my next comment
        neighbors++;
      }
     }
   }

   return neighbors;
}

第三,如果模拟器中的每个空间只包含一个字符值,我建议您为模拟器使用char[][]而不是String[][]。有一些关于Java中的字符串的东西你不需要被绊倒 - 例如,在Java中,你不能使用==来比较字符串的值(你需要使用String的equals()方法)。

答案 1 :(得分:-1)

首先,你的主要课程应该是Public static void main(String[] args)你可以使用switch case除了if else,如果你真的确定你是否可以使用if(blabla == blablabla&amp;(this means and)blaba = = babalaba)