康威的生命游戏算法错误

时间:2014-09-16 23:21:05

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

这是代码。只要我没有手动声明GameBoard和CopyBoard并让用户提交高度和宽度,游戏就会起作用。

为什么算法只适用于第一个坐标,但之后会跳过剩下的代码,所以它会自动显示一个空板?

    public static void main(String[] args) {

int tracker = 0, neighbors = 0, answer = 0, height = 0, width = 0;
int widthAlg = width - 1;
int heightAlg = height - 1;

Scanner kb = new Scanner(System.in);
System.out.println(" [1]: Still life \n [2]: Oscillate \n [3]: Cool Game 1 \n [4]: Cool Game 2");
answer = kb.nextInt();

height = 3;
width = 4;
boolean GameBoard[][] = new boolean[height][width];
GameBoard[0][0] = true;
GameBoard[0][1] = true;
GameBoard[1][0] = true;
GameBoard[1][3] = true;
GameBoard[2][1] = true;
GameBoard[2][2] = true;
boolean CopyBoard[][] = new boolean[height][width];

System.out.println("\n\nThis is the original gameboard");
//display of the gameboard
String[][] Display = new String[height][width];
for (int i = 0; i < GameBoard.length; i++) {
    for (int j = 0; j < GameBoard[i].length; j++) {
        if (GameBoard[i][j]) {
            Display[i][j] = "*";
        } else {
            Display[i][j] = "-";
        }
        System.out.print(Display[i][j] + " ");
    }
    System.out.println("");
};

    String answer1;



    //algorithm
    do{
tracker++;
System.out.print("\nYou have gone " + tracker + " time(s)");
for(int i =0; i < GameBoard.length; i++){
    for(int j = 0; j < GameBoard[i].length; j++){
   if(i == 0 && j == 0){
       if(GameBoard[i][j+1])        {neighbors++;}
       if(GameBoard[i+1][j])        {neighbors++;}
       if(GameBoard[i+1][j+1])      {neighbors++;}
   }

   else if( i == 0 && j >= 1 && j < widthAlg ){
       if(GameBoard[i][j-1])        {neighbors++;}
       if(GameBoard[i+1][j-1])      {neighbors++;}
       if(GameBoard[i][j+1])        {neighbors++;}
       if(GameBoard[i+1][j])        {neighbors++;}
       if(GameBoard[i+1][j+1])      {neighbors++;}
   }

   else if(i == 0 && j == widthAlg){
       if(GameBoard[i][j-1])        {neighbors++;}
       if(GameBoard[i+1][j-1])      {neighbors++;}
       if(GameBoard[i+1][j])        {neighbors++;}
   }

   else if( i >= 1 && i < heightAlg && j == 0 ){
       if(GameBoard[i-1][j])        {neighbors++;}
       if(GameBoard[i][j+1])        {neighbors++;}
       if(GameBoard[i-1][j+1])      {neighbors++;}
       if(GameBoard[i+1][j])        {neighbors++;}
       if(GameBoard[i+1][j+1])      {neighbors++;}
   }

    else if( i >= 1 && i < heightAlg && j >= 1 && j < widthAlg ){
       if(GameBoard[i-1][j-1])      {neighbors++;}
       if(GameBoard[i-1][j])        {neighbors++;}
       if(GameBoard[i-1][j+1])      {neighbors++;}
       if(GameBoard[i][j+1])        {neighbors++;}
       if(GameBoard[i][j-1])        {neighbors++;}
       if(GameBoard[i+1][j])        {neighbors++;}
       if(GameBoard[i+1][j+1])      {neighbors++;}
       if(GameBoard[i+1][j-1])      {neighbors++;}
   }

   else if( i > 0 && i < heightAlg && j == widthAlg ){
       if(GameBoard[i-1][j])        {neighbors++;}
       if(GameBoard[i-1][j-1])      {neighbors++;}
       if(GameBoard[i+1][j])        {neighbors++;}
       if(GameBoard[i][j-1])        {neighbors++;}
       if(GameBoard[i+1][j-1])      {neighbors++;}
   }

   else if( i==0 && j == heightAlg ){
       if(GameBoard[i-1][j])        {neighbors++;}
       if(GameBoard[i][j+1])        {neighbors++;}
       if(GameBoard[i-1][j+1])      {neighbors++;}
   }
   else if( i == heightAlg && j >= 1 && j < widthAlg ){
       if(GameBoard[i][j-1])        {neighbors++;}
       if(GameBoard[i-1][j])        {neighbors++;}
       if(GameBoard[i-1][j+1])      {neighbors++;}
       if(GameBoard[i-1][j-1])      {neighbors++;}
       if(GameBoard[i][j+1])        {neighbors++;}
   }

   else if( i == heightAlg  && j == widthAlg ){
       if(GameBoard[i][j-1])        {neighbors++;}
       if(GameBoard[i-1][j-1])      {neighbors++;}
       if(GameBoard[i-1][j])        {neighbors++;}
   }
            //checks the rules to see who survived 
                //and placing the answer in the CopyBoard
                if (GameBoard[i][j]) {
                    if (neighbors >= 2 && neighbors <= 3) {
                        CopyBoard[i][j] = true;
                    } else {
                        CopyBoard[i][j] = false;
                    }
                } else if (!GameBoard[i][j]) {
                    if (neighbors == 3) {
                        CopyBoard[i][j] = true;
                    } else {
                        CopyBoard[i][j] = false;
                    }
                }
                neighbors = 0;
            }
        }
        System.out.println();
        //display copyboard
        for (int i = 0; i < GameBoard.length; i++) {
            for (int j = 0; j < GameBoard[i].length; j++) {
                if (CopyBoard[i][j]) {
                    Display[i][j] = "*";
                } else {
                    Display[i][j] = "-";
                }
                System.out.print(Display[i][j] + " ");
            }
            System.out.println("");
        }
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Would you like to go again? If yes press \'Y\', else press anything else.");
        answer1 = keyboard.next().toUpperCase();
        if (answer1.toUpperCase().charAt(0) == 'Y')//;
        {   //if he wants to go again copy the results to the original gameboard and repeat.
            for (int i = 0; i < GameBoard.length; i++) {
                System.arraycopy(CopyBoard[i], 0, GameBoard[i], 0, GameBoard[i].length);
            }
        }
    } while (answer1.charAt(0) == 'Y');

}

}

1 个答案:

答案 0 :(得分:3)

我认为你问题的一部分,至少是这些问题......

int widthAlg = width - 1;
int heightAlg = height - 1;

...在行之前出现,其中width和height设置为实际值。因此,widthAlg和heightAlg的值始终为-1

如果您使用调试器逐步执行该程序,这样的逻辑错误将很快变得清晰。