首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Conway's Game of Life Debugging (Java)

Conway's Game of Life Debugging (Java)
EN

Stack Overflow用户
提问于 2015-05-14 05:04:00
回答 1查看 658关注 0票数 0

我试图让我的Conway's Game of Life正确运行,但我总是得到不正确的结果,而且我似乎找不到问题所在。下面是我用来执行“生活的游戏”的代码:

代码语言:javascript
复制
public void generate(int gen)
 {
   generations = gen;
   int count = 0;
   for (int x = 0; x < generations; x++)
   {
      //Copies array to temp
      for (int row = 0; row < 20; row++)
      {
         for (int col = 0; col < 20; col++)
         {
            temp[row][col] = mat[row][col];
         }
      }

      //Gets count of living organisms surrounding
      for (int row = 0; row < 20; row++)
      {
         for (int col = 0; col < 20; col++)
         {
            count = check(row, col);

            //determines life or death
            if (temp[row][col] == false)
            {
               if (count == 3)
               {
                  mat[row][col] = true;
               }
            }
            else
            {
               if (count > 3 || count < 2)
               {
                  mat[row][col] = false;
               }
            }
         }
      }
   }

   displayGrid();
 }

 //Checks the number of living organisms in adjacent cells
 public int check(int row, int col)
 {
   int count = 0;
   for (int r = -1; r < 2; r++)
   {
      for (int c = -1; c < 2; c++)
      {
         if (isLegal((row + r),(col + c)) && temp[row + r][col + c] == true)
         {
            count++;
         }
      }
   }
   return count;
 }

 //Checks whether an adjacent space is in the array 
 public boolean isLegal(int row, int col)
 {
   if (row > 19 || row < 0 || col > 19 || col < 0)
   {
      return false;
   }
   return true;
 }

我尝试编写这个程序的方式有什么根本的错误吗?

EN

回答 1

Stack Overflow用户

发布于 2015-05-14 05:09:23

在你的check()方法中,你将row col的正方形包含在总数中,而它应该忽略它。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30225154

复制
相关文章

相似问题

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