首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java -生命的游戏;生命检测的问题

Java -生命的游戏;生命检测的问题
EN

Stack Overflow用户
提问于 2012-02-07 10:24:52
回答 2查看 1K关注 0票数 1

我正在尝试Game of Life -我坚持的是根据周围的区域来决定一个空间是活的还是死的。我已经到了说不出自己做错了什么的地步--希望另一双眼睛能帮上忙。

这只是一小段代码,完整代码如下:http://pastebin.com/ucYe653p

代码语言:javascript
复制
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;

            }

        }
    }

}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-02-07 10:41:51

你就是整个代码:

代码语言:javascript
复制
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方法的主体中,不应该这样做,因为它会过早地影响相邻单元格的计算。相反,只在此方法的主体中设置克隆数组的单元。然后,在方法的末尾,要么迭代克隆的板,将其结果复制到原始板中,要么返回克隆的板,并在调用该方法时使用它将其结果设置到板中。即,

代码语言:javascript
复制
board = updateMatrix(board, birthLive);
票数 1
EN

Stack Overflow用户

发布于 2012-02-07 10:33:55

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

代码语言:javascript
复制
  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++;
                }
            }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9170201

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档