Game of Life Neighbor Check (is there an easier way?)

I'm trying to make a life game in C ++ at the moment, this is my first C ++ exercise. I have one question: we need to make some game modes, for example, one of them is called "torus", when the cells leaving the board must re-enter it on the opposite side.

Now I'm checking the neighbors right now. But I hard code it with hell a lot if offers, because I tried some for-loops, but it didn’t work.

But is this really the only option? For hard coding of every possibility (cells on the left side, right side, upper side, lower side, etc.

This is the code snippet I have for this:

int countNeighboursTorus(int a, int b) {

int living = 0;

// when the starting cell is the one on the upper left (start of the board)
if (a == 0 && b == 0) {

    // cell below
    if (board[a - 1][b] != DEAD) {
        living++;
    }
    // cell right below
    if (board[a + 1][b + 1] != DEAD) {
        living++;
    }
    // cell right
    if (board[a][b + 1] != DEAD) {
        living++;
    }
    // cell above (other side of the board)
    if (board[HEIGHT - 1][b] != DEAD) {
        living++;
    }
    // cell above right (other side of the board)
    if (board[HEIGHT - 1][b + 1] != DEAD) {
        living++;
    }

}

// first edge case (height = 0, width != 0):
else if (a == 0 && b != 0) {
    // cell below
    if (board[a - 1][b] != DEAD) {
        living++;
    }
    // cell right below
    if (board[a + 1][b + 1] != DEAD) {
        living++;
    }
    // cell right
    if (board[a][b + 1] != DEAD) {
        living++;
    }
    // cell left below
    if (board[a + 1][b - 1] != DEAD) {
        living++;
    }
    // cell left
    if (board[a][b - 1] != DEAD) {
        living++;
    }
    // cell left above (other side of the board)
    if (board[HEIGHT - 1][b - 1] != DEAD) {
        living++;
    }
    // cell above (other side of the board)
    if (board[HEIGHT - 1][b] != DEAD) {
        living++;
    }
    // cell above right (other side of the board)
    if (board[HEIGHT - 1][b + 1] != DEAD) {
        living++;
    }
}

return living;

}

for, , , : . , 3 , 2 . , , . - "+" - 5/6 , ( ).

// for any other "normal" cases inside of the board:
else if (a != 0 && b != 0 && a < HEIGHT - 1 && b < WIDTH - 1) {
    for (int c = -1; c < 2; c++) {
        for (int d = -1; d < 2; d++) {
            if (!(c == 0 && d == 0)) {
                if (board[a + c][b + d] != DEAD) {
                    living++; 
                }
            }
        }
    }
}

- , ? , , ? , ? . , . ++, Java , , ++. !

+4
3

. :

    if (board[a + 1][b + 1] != DEAD) {

:

    if (board[(a + 1) % HEIGHT][(b + 1) % WIDTH] != DEAD) {

(% , ). -1 ( -1), /:

    if (board[(a - 1 + HEIGHT) % HEIGHT][(b - 1 + WIDTH) % WIDTH] != DEAD) {

.

+5

, :

if (board[a + c][b + d] != DEAD) {

, a + c b + d , , . "torus" , "" "" , .

+1

In addition to Martin Bonner 's answer, prepare an offset table for eight neighbors:

static const int NoOfNeighbors = 8;

int  dVertNeigh [NoOfNeighbors] = {
    HEIGHT-1, HEIGHT-1, HEIGHT-1,
    0,                  0,
    1,        1,        1};

int  dHorizNeigh [NoOfNeighbors] = {
    WIDTH-1,  0,        1,
    WIDTH-1,            1,
    WIDTH-1,  0,        1};

then you can count neighboring cells with a simple loop:

for (int ngh = 0; ngh < NoOfNeighbors; ngh++) {
    int neighborV = (a + dVertNeigh[ngh]) % HEIGHT;
    int neighborH = (b + dHorizNeigh[ngh]) % WIDTH;

    if (board[neighborV][neighborH] != DEAD) {
        living++;
    }
}
+1
source

Source: https://habr.com/ru/post/1688817/


All Articles