Wrapping in Conway's Game of Life C++

后端 未结 1 1033
生来不讨喜
生来不讨喜 2021-01-23 13:54

I am trying to write a program that implements Conway\'s game of life on a 20x60 cell board. The grid will wrap around so the left side will be connected to (neig

1条回答
  •  伪装坚强ぢ
    2021-01-23 14:06

    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.

    0 讨论(0)
提交回复
热议问题