Wrapping in Conway's Game of Life C++

孤者浪人 提交于 2019-12-02 08:25:56

First, I would change the grid to be 0-based instead of 1-based.

Then you can write a simple loop:

int count = 0;
for (i = row - 1; i <= row + 1; i++) {
   for (j = col - 1; j <= col + 1; j++) {
      if(i != row && j != col) {
         count += grid[(i + maxrow)%maxrow][(j + maxcol)%maxcol];
      }
   }
}

The + maxrow is to make sure the index is positive.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!