约翰康威的生命游戏

时间:2013-12-30 15:03:20

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

我一直在为学校制作康威的生命游戏版本而且我遇到了一个问题:细胞在错误的地方变得死亡或活着。

我该如何解决?

if (alive == 3 && aryBOARD[x][y] == 0) { //rule 4
    aryCHANGE[x][y] = 1;
}
if (alive > 3  && aryBOARD[x][y] == 1) { //rule 3
    aryCHANGE[x][y] = 0;
}
if (alive >= 2 && alive <= 3 &&  aryBOARD[x][y] == 1) { //rule 2
    aryCHANGE[x][y] = 1;
}
if (alive < 2   && aryBOARD[x][y] == 1) { //rule 1
    aryCHANGE[x][y] = 0;
}
if (dead > 5) { //rule check
    aryCHANGE[x][y] = 0;
}

这就是我认为问题所在,但如果整个代码有帮助:

package gameoflife2;

public class Game {

  public static void main(String[] args) {

    int[][] aryBOARD = new int[5][5];
    int x = 0;
    int y = 0;
    int dead = 0;
    int alive = 0;
    int i, j;
    // Board numbers
    // 00011
    // 00001
    // 01000
    // 01100
    // 00000
    aryBOARD[0][0] = 0;
    aryBOARD[0][1] = 0;
    aryBOARD[0][2] = 0;
    aryBOARD[0][3] = 1;
    aryBOARD[0][4] = 1;
    aryBOARD[1][0] = 0;
    aryBOARD[1][1] = 0;
    aryBOARD[1][2] = 0;
    aryBOARD[1][3] = 0;
    aryBOARD[1][4] = 1;
    aryBOARD[2][0] = 0;
    aryBOARD[2][1] = 1;
    aryBOARD[2][2] = 0;
    aryBOARD[2][3] = 0;
    aryBOARD[2][4] = 0;
    aryBOARD[3][0] = 0;
    aryBOARD[3][1] = 1;
    aryBOARD[3][2] = 1;
    aryBOARD[3][3] = 0;
    aryBOARD[3][4] = 0;
    aryBOARD[4][0] = 0;
    aryBOARD[4][1] = 0;
    aryBOARD[4][2] = 0;
    aryBOARD[4][3] = 0;
    aryBOARD[4][4] = 0;
    // end of array
    int[][] aryCHANGE = aryBOARD.clone(); // array change is equal to array
                                          // board

    // printing array
    int rows = 5;
    int colums = 5;

    for (i = 0; i < rows; i++) {
      for (j = 0; j < colums; j++) {
        System.out.print(aryBOARD[i][j] + " ");
      }
      System.out.println("");
    }

    System.out.println("---------------------------");

    // done printing array
    // check for dead or alive cells

    for (x = 0; x <= 4; x++) {
      for (y = 0; y <= 4; y++) {
        alive = 0;
        dead = 0;
        if ((x + 1 < 4) && (x + 1 > 0)) { // right
          if (aryBOARD[x + 1][y] == 0) {
            dead++;
          } else {
            alive++;
          }
        }

        if (((y - 1 < 4) && (y - 1 > 0) && (x + 1 < 4) && (x + 1 > 0))) { // bottom
                                                                          // right
                                                                          // corner
          if (aryBOARD[x + 1][y - 1] == 0) {
            dead++;
          } else {
            alive++;
          }
        }

        if (((y + 1 < 4) && (y + 1 > 0) && (x + 1 < 4) && (x + 1 > 0))) { // top
                                                                          // right
                                                                          // corner
          if (aryBOARD[x + 1][y + 1] == 0) {
            dead++;
          } else {
            alive++;
          }
        }

        if ((y + 1 < 4) && (y + 1 > 0)) {// top middle
          if (aryBOARD[x][y] == 0) {
            dead++;
          } else {
            alive++;
          }
        }

        if (((y + 1 < 4) && (y + 1 > 0) && (x - 1 < 4) && (x - 1 > 0))) {// top
                                                                         // left
                                                                         // corner
          if (aryBOARD[x - 1][y + 1] == 0) {
            dead++;
          } else {
            alive++;
          }
        }

        if ((x - 1 < 4) && (x - 1 > 0)) {// left
          if (aryBOARD[x - 1][y] == 0) {
            dead++;
          } else {
            alive++;
          }
        }

        if (((y - 1 < 4) && (y - 1 > 0) && (x - 1 < 4) && (x - 1 > 0))) {// bottom
                                                                         // left
                                                                         // corner
          if (aryBOARD[x - 1][y - 1] == 0) {
            dead++;
          } else {
            alive++;
          }
        }

        // x++
        if ((y - 1 < 4) && (y - 1 > 0)) {// bottom middle
          if (aryBOARD[x][y - 1] == 0) {
            dead++;
          } else {
            alive++;
          }
        }

        // RULES
        // 1 Any live cell with fewer than two live neighbors dies, as if caused
        // by under-population.
        // 2 Any live cell with two or three live neighbors lives on to the next
        // generation.
        // 3 Any live cell with more than three live neighbors dies, as if by
        // overcrowding.
        // 4 Any dead cell with exactly three live neighbors becomes a live
        // cell, as if by reproduction.

        // test alive and dead

        if (alive == 3 && aryBOARD[x][y] == 0) {// rule 4
          aryCHANGE[x][y] = 1;
        }

        if (alive > 3 && aryBOARD[x][y] == 1) {// rule 3
          aryCHANGE[x][y] = 0;
        }

        if (alive >= 2 && alive <= 3 && aryBOARD[x][y] == 1) {// rule 2
          aryCHANGE[x][y] = 1;
        }

        if (alive < 2 && aryBOARD[x][y] == 1) {// rule 1
          aryCHANGE[x][y] = 0;
        }
        if (dead > 5) {// rule check
          aryCHANGE[x][y] = 0;
        }
      }
    }

    for (i = 0; i < rows; i++) {

      for (j = 0; j < colums; j++) {
        System.out.print(aryCHANGE[i][j] + " ");

      }
      System.out.println("");
    }
    System.out.println("---------------------------");

  } // end main
} // end class

2 个答案:

答案 0 :(得分:1)

您需要更多地考虑构建代码并将其分解为更小的块。这对你有很大帮助,特别是当你将来搬到更大的项目时。

例如,编写一个简单的方法来计算给定单元格周围的活细胞数。

现在你的主要循环变为:

for (int x=0;x<width;x++) {
    for (int y=0;y<height;y++) {
        switch(countLivingAround(x,y)) {
           case 0: // Less than 2 always dies
           case 1:
               grid(x,y) = 0;
               break;
           case 2: // Do nothing, keep current state
               break;
           case 3: // Breed
               grid(x,y) = 1;
               break;
           case 4: // Dies from overcrowding
               grid(x,y) = 0;
               break;
        }
    }
}

您的计数功能可以很简单,它只是将[x-1,y],[x,y + 1]等处的值相加,记得检查板的边缘并正确处理该情况

答案 1 :(得分:0)

除了将逻辑分解为不同的方法之外,您还需要使用调试器来逐步执行代码或使用printlns来显示您评估的x和y值(请参阅下面的示例)您将看到一些你在相邻单元x和y坐标上的计算需要工作。

    for (x = 0; x <= 4; x++) {
        for (y = 0; y <= 4; y++) {
            alive = 0;
            dead = 0;
            System.out.println("evaluating cell x=" + x + ", y = " + y);
            if ((x + 1 < 4) && (x + 1 > 0)) {// right
                if (aryBOARD[x + 1][y] == 0) {
                    dead++;
                }
                else {
                    alive++;
                }
                System.out.println(" check 1 x=" + (x + 1) + ", y = " + y);
            }