简体   繁体   English

约翰·康威的人生游戏

[英]John Conway's Game of Life

I have been working on a versions of Conway's Game of Life for school and I have run into a problem: cells are becoming dead or alive in the wrong places. 我一直在为学校制作Conway的《生活游戏》的一个版本,但遇到一个问题:在错误的地方细胞正在死亡或存活。

How can I fix that? 我该如何解决?

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;
}

That is where I assume the problem is but if having the whole code would help: 这就是我认为问题出在哪里,但是如果拥有完整的代码会有所帮助:

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

You need to think a lot more about structuring your code and breaking things up into smaller chunks. 您需要更多地考虑结构化代码并将其分解为较小的块。 That will help you a lot, especially when you move onto larger projects in the future. 这将对您有很大帮助,尤其是当您将来进行较大的项目时。

For example write a simple method to count the number of living cells around a given cell. 例如,编写一种简单的方法来计算给定细胞周围的活细胞数量。

Now your main loop through just becomes: 现在您的主循环变成:

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;
        }
    }
}

Your count function can be simple, it just adds up the values at [x-1,y],[x,y+1], etc, remember to check for the edges of the board and handle that case correctly though. 您的计数功能可能很简单,它只将[x-1,y],[x,y + 1]等值相加,请记住检查棋盘的边缘并正确处理这种情况。

In addition to breaking the logic up into different methods you need to either use a debugger to step thru the code or use printlns to display the x and y values your evaluating (See the sample below) You'll see that some of your calculations on the adjoining cells x and y coordinates need work. 除了将逻辑分解为不同的方法之外,您还需要使用调试器来遍历代码,或者使用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);
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM