【问题标题】:Java - the game of Life; problems with life detectionJava - 生命的游戏;生命检测问题
【发布时间】:2012-02-07 02:24:52
【问题描述】:

我正在尝试Game of Life - 我坚持的是根据它周围的区域来确定一个空间应该是活的还是死的。我已经到了无法判断自己做错了什么的地步——希望另一双眼睛可以提供帮助。

这只是一个 sn-p,完整代码在这里:http://pastebin.com/ucYe653p

public static void updateMatrix(boolean[][] board, int[] birthLive) {

    //clone the board so modified values don't affect the results of upcoming spaces
    boolean[][] boardCopy = board.clone();
    for (int i = 0; i < board.length; i++)
        boardCopy[i] = board[i].clone();

    int count = 0;

    for (int i = 1; i < board.length - 1; i++) {
        for (int j = 1; j < board[i].length - 1; j++) {

            //different requirements for dead or living pieces' living status
            if (board[i][j] == false) {

                //check the nine-by-nine square, starting at (-1, 1) and ending at (1, -1) where 0 is the affected location
                // * * *
                // * 0 *
                // * * *
                for (int ii = board[i - 1].length; ii < board[i + 1].length; ii++) {
                    for (int jj = board[j - 1].length; ii < board[j + 1].length; jj++) {
                        if (boardCopy[i][j] == true)
                            count++;
                    }
                }

                //check to see what high and low amt of required adjacent lifeforms is
                if (count >= birthLive[0] && count <= birthLive[1])
                    board[i][j] = true;
                else
                    board[i][j] = false;
                count = 0;

            }

            else {

                for (int ii = board[i - 1].length; ii < board[i + 1].length; ii++) {
                    for (int jj = board[j - 1].length; ii < board[j + 1].length; jj++) {
                        if (boardCopy[i][j] == true)
                            count++;
                    }
                }

                count -= 1; //For board[i][j] is always true
                if (count >= birthLive[2] && count <= birthLive[3])
                    board[i][j] = true;
                else
                    board[i][j] = false;
                count = 0;

            }

        }
    }

}

【问题讨论】:

  • 您能描述一下问题的症状吗?
  • 您需要让我们知道出了什么问题。 IE。你期望这段代码做什么,它做了什么。
  • 对,对不起。 To calculate whether an entity lives at a given cell for the next iteration, you will need to calculate the number of entities in the neighborhood and compare the count with the birth range (if the cell is empty) or live range (if there was already an entity there). Note that the count must be based on the values as of the previous iteration. This means that you will need to make a copy of the matrix before doing the updates so you can count entities in neighborhoods based on original values. 外观:i.imgur.com/C0i4e.png
  • 您可能会从this code 那里得到一些提示。该线程上的问题似乎与此处的问题相同。

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


【解决方案1】:

你是完整的代码:

import java.util.*;

public class Update {

   public final static int ITERATIONS = 5;

   public static void main(String[] args) {

      System.out.println("This program will simulate the game of Life.");

      Scanner console = new Scanner(System.in);

      System.out.println("Please input the size of your board.");

      System.out.println("Rows:");
      final int rows = console.nextInt();

      System.out.println("Columns:");
      final int columns = console.nextInt();

      System.out.println("Please enter a seed:");
      final long seed = console.nextLong();

      int[] birthLive = new int[4];
      boolean[][] board = new boolean[rows][columns];

      createMatrix(board, seed);

      birthAndLive(birthLive);

      for (int i = 0; i < ITERATIONS; i++) {
         printMatrix(board);
         updateMatrix(board, birthLive);
      }

   }

   public static void createMatrix(boolean[][] board, long seed) {

      Random seedBool = new Random(seed);

      for (int i = 0; i < board.length; i++) {
         for (int j = 0; j < board[i].length; j++) {
            board[i][j] = false;
         }
      }

      for (int i = 1; i < board.length - 1; i++) {
         for (int j = 1; j < board[i].length - 1; j++) {
            board[i][j] = seedBool.nextBoolean();
         }
      }

   }

   public static void birthAndLive(int[] birthLive) {

      Scanner console = new Scanner(System.in);

      System.out.println("Please input the birth range and the live range:");

      System.out.println("Birth (Low):");
      birthLive[0] = console.nextInt();

      System.out.println("Birth (High):");
      birthLive[1] = console.nextInt();

      System.out.println("Live (Low):");
      birthLive[2] = console.nextInt();

      System.out.println("Live (High):");
      birthLive[3] = console.nextInt();

   }

   public static void printMatrix(boolean[][] board) {

      for (int i = 0; i < board.length; i++) {
         for (int j = 0; j < board[i].length; j++) {
            if (board[i][j] == false)
               System.out.print(" - ");
            else
               System.out.print(" # ");
         }
         System.out.println();
      }
      System.out.println();
   }

   public static void updateMatrix(boolean[][] board, int[] birthLive) {

      // clone the board so modified values don't affect the results of upcoming
      // spaces
      boolean[][] boardCopy = board.clone();
      for (int i = 0; i < board.length; i++)
         boardCopy[i] = board[i].clone();

      int count = 0;

      for (int i = 1; i < board.length - 1; i++) {
         for (int j = 1; j < board[i].length - 1; j++) {

            // different requirements for dead or living pieces' living status
            if (board[i][j] == false) {

               // check the nine-by-nine square, starting at (-1, 1) and ending
               // at (1, -1) where 0 is the affected location
               // * * *
               // * 0 *
               // * * *
               for (int ii = board[i - 1].length; ii < board[i + 1].length; ii++) {
                  for (int jj = board[j - 1].length; ii < board[j + 1].length; jj++) {
                     if (boardCopy[i][j] == true)
                        count++;
                  }
               }

               // check to see what high and low amt of required adjacent
               // lifeforms is
               if (count >= birthLive[0] && count <= birthLive[1])
                  board[i][j] = true;
               else
                  board[i][j] = false;
               count = 0;

            }

            else {

               for (int ii = board[i - 1].length; ii < board[i + 1].length; ii++) {
                  for (int jj = board[j - 1].length; ii < board[j + 1].length; jj++) {
                     if (boardCopy[i][j] == true)
                        count++;
                  }
               }

               count -= 1; // For board[i][j] is always true
               if (count >= birthLive[2] && count <= birthLive[3])
                  board[i][j] = true;
               else
                  board[i][j] = false;
               count = 0;

            }

         }
      }

   }
}

您正在 updateMatrix 方法的主体中设置 board[...] 值,不应该这样做,因为它会过早地影响相邻单元格的计算。而是仅在此方法的主体中设置克隆数组的单元格。然后在方法的 end 处遍历克隆板,将其结果复制到原始板中,或者返回克隆板,并在调用该方法时使用它将其结果设置到板中.. .即,

board = updateMatrix(board, birthLive);

【讨论】:

  • 是的,这肯定也是个问题。所有这些board[i][j] = ...should be boardCopy[i][j] = ...
  • 我认为我通过使用 boardCopy 来测试 pos/neg 而不是 board 来规避设置 board 值的影响,但是?我想我更新了它,所以我没有使用任何更改的值 - pastebin.com/ucYe653p
  • 是的,你是对的,诺亚——现在我再看一遍,我认为你做得对。解决此问题是否解决了您的问题?
  • @Noah:是的,你是对的。你在做我习惯的倒退,但如果小心点,它也会起作用。对不起。 :(
【解决方案2】:

这个循环/嵌套循环在两个条件检查中都有ii...第二个可能应该是jj?另外,你在哪里引用iijj?在 if 中,它再次使用了ij

  for (int ii = board[i - 1].length; ii < board[i + 1].length; ii++) {
                for (int jj = board[j - 1].length; ii < board[j + 1].length; jj++) {
                    if (boardCopy[i][j] == true)
                        count++;
                }
            }

【讨论】:

  • 啊,不错。我只是想使用 ii 和 jj 充当 (-1, 1) - (1, -1) - 我想不出一个好的方法来做到这一点。我会画一张图来解释我的意思。 i.imgur.com/NMAW6.png
  • 我明白你在做什么。只是你应该说if(boardCopy[ii][jj] == true),否则你只是一遍又一遍地检查相同的值。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多