简体   繁体   English

计算邻居的数量康威的生命游戏

[英]Counting number of neighbors Conway's Game of Life

I have an error somewhere in this code.我在这段代码的某处有错误。 The number of neighbors is not being counted correctly, as per my understanding.据我了解,邻居的数量没有被正确计算。 The neighbors function is probably where the issue is.邻居 function 可能是问题所在。 My field variable is a 12x12 char array, the '#' is an alive cell and '-' is a dead one.我的字段变量是一个 12x12 字符数组,“#”是一个活动单元格,“-”是一个死单元格。 I am relatively new to programming and would appreciate some help with this.我对编程比较陌生,希望能得到一些帮助。 Thank you!谢谢!

int neighbors(int l, int c)
{
  int num = 0;
  for (int i = -1; i < 2; i++)
  {
    for (int j = -1; j < 2; j++)
    {
      if ((l+i < 0 || l+i > 12) && (c+j < 0 || c+j > 12))
      {
        continue;
      }
      else if ((i != 0 || j != 0) && field[(l + i)][(c + j)] == '#')
      {
        num++;
      }
    }
  }
  return num;
}

//game logic
void logic()
{
  char temp[12][12];
  for (int i = 0; i < 12; i++)
  {
    for (int j = 0; j < 12; j++)
    {
      temp[i][j] = field[i][j];
    }
  }
  for (int i = 0; i < 12; i++)
  {
    for (int j = 0; j < 12; j++)
    {
      if (field[i][j] == '#')
      {
        if (neighbors(i, j) < 1 || neighbors(i, j) > 3)
        {
          temp[i][j] = '-';
        }
        else
        {
          temp[i][j] = '#';
        }
      }
      if (field[i][j] = '-')
      {
        if (neighbors(i, j) == 3)
        {
          temp[i][j] = '#';
        }
        else
        {
          temp[i][j] = '-';
        }
      }
      field[i][j] = temp[i][j];
    }
  }
}

If the array has dimensions 12x12, the maximum index value is 11, hence the test if ((l+i < 0 || l+i > 12) && (c+j < 0 || c+j > 12)) is incorrect.如果数组的维度为 12x12,则最大索引值为 11,因此测试if ((l+i < 0 || l+i > 12) && (c+j < 0 || c+j > 12))是不正确。 It should be:它应该是:

if (l+i < 0 || l+i >= 12 || c+j < 0 || c+j >= 12)
    continue;

Another major problem is you update field[i][j] = temp[i][j];另一个主要问题是你更新field[i][j] = temp[i][j]; inside the update loop: this corrupts the computation for the neighbors of the adjacent cells.在更新循环内部:这会破坏相邻单元格的邻居的计算。 You should first compute the whole temp array and update field in a subsequent loop, or with a single call to memcpy() .您应该首先在后续循环中计算整个temp数组和更新field ,或者通过一次调用memcpy()

Furthermore, the standard rules for Conway's Game of Life are somewhat different from your implementation: if (neighbors(i, j) < 1 || neighbors(i, j) > 3) keeps a cell with a single neighbour alive whereas under the standard rules it should die.此外,康威生命游戏的标准规则与您的实现有些不同: if (neighbors(i, j) < 1 || neighbors(i, j) > 3)规则它应该死。 Change this test to:将此测试更改为:

if (neighbors(i, j) < 2 || neighbors(i, j) > 3)
    temp[i][j] = '-';

Here is a simplified version:这是一个简化版本:

int neighbors(int l, int c) {
    int num = 0;
    for (int i = -1; i < 2; i++) {
        for (int j = -1; j < 2; j++) {
            if (l+i >= 0 && l+i < 12 && c+j >= 0 && c+j <= 12
            &&  i != 0 && j != 0 && field[l+i][c+j] == '#') {
                num++;
            }
        }
    }
    return num;
}

//game logic
void logic() {
    char temp[12][12];
    for (int i = 0; i < 12; i++) {
        for (int j = 0; j < 12; j++) {
            temp[i][j] = field[i][j];
        }
    }
    for (int i = 0; i < 12; i++) {
        for (int j = 0; j < 12; j++) {
            int nb = neighbors(i, j);
            if (nb < 2 || nb > 3) {
                temp[i][j] = '-';
            } else
            if (nb == 3) {
                temp[i][j] = '#';
            }
        }
    }
    for (int i = 0; i < 12; i++) {
        for (int j = 0; j < 12; j++) {
            field[i][j] = temp[i][j];
        }
    }
}

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

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