src/conway.c
void conway_evolve(conway_t *conway)
{
    int i, j;
    int neighbors;

    /* Prepare cells for next generation*/
    for (i = 0; i < conway->num_rows; ++i) {
        for (j = 0; j < conway->num_cols; ++j) {
            neighbors = conway_neighbors(conway, i, j);

            if (conway->universe[i][j].alive &&
                    (neighbors < 2 || neighbors > 3)) {
                    conway->universe[i][j].state = CELL_EXPIRING;
            }
            if (!conway->universe[i][j].alive && neighbors == 3) {
                    conway->universe[i][j].state = CELL_RISING;
            }
        }
    }

    /* Evolve to the next generation */
    for (i = 0; i < conway->num_rows; ++i) {
        for (j = 0; j < conway->num_cols; ++j) {
            switch (conway->universe[i][j].state) {
                case CELL_RISING:
                    conway->universe[i][j].alive = true;
                    break;

                case CELL_EXPIRING:
                    conway->universe[i][j].alive = false;
                    break;

                case CELL_NORMAL:
                default:
                    break;
            }
            conway->universe[i][j].state = CELL_NORMAL;
        }
    }
}
/* Calculates the next generation for a Conway universe */
Calculates the next generation for a Conway universe
Calculates the next generation for a Conway universe
void conway_evolve(conway_t *conway)
{
    int i, j;
    int neighbors;
    for (i = 0; i < conway->num_rows; ++i) {
        for (j = 0; j < conway->num_cols; ++j) {
            neighbors = conway_neighbors(conway, i, j);
            if (conway->universe[i][j].alive &&
                    (neighbors < 2 || neighbors > 3)) {
                    conway->universe[i][j].state = CELL_EXPIRING;
            }
            if (!conway->universe[i][j].alive && neighbors == 3) {
                    conway->universe[i][j].state = CELL_RISING;
            }
        }
    }
    for (i = 0; i < conway->num_rows; ++i) {
        for (j = 0; j < conway->num_cols; ++j) {
            switch (conway->universe[i][j].state) {
                case CELL_RISING:
                    conway->universe[i][j].alive = true;
                    break;
                case CELL_EXPIRING:
                    conway->universe[i][j].alive = false;
                    break;
                case CELL_NORMAL:
                default:
                    break;
            }
            conway->universe[i][j].state = CELL_NORMAL;
        }
    }
}
void conway_evolve ( conway_t * conway ) { int i , j ; int neighbors ; for ( i = 0 ; i < conway -> num_rows ; ++ i ) { for ( j = 0 ; j < conway -> num_cols ; ++ j ) { neighbors = conway_neighbors ( conway , i , j ) ; if ( conway -> universe [ i ] [ j ] . alive && ( neighbors < 2 || neighbors > 3 ) ) { conway -> universe [ i ] [ j ] . state = CELL_EXPIRING ; } if ( ! conway -> universe [ i ] [ j ] . alive && neighbors == 3 ) { conway -> universe [ i ] [ j ] . state = CELL_RISING ; } } } for ( i = 0 ; i < conway -> num_rows ; ++ i ) { for ( j = 0 ; j < conway -> num_cols ; ++ j ) { switch ( conway -> universe [ i ] [ j ] . state ) { case CELL_RISING : conway -> universe [ i ] [ j ] . alive = true ; break ; case CELL_EXPIRING : conway -> universe [ i ] [ j ] . alive = false ; break ; case CELL_NORMAL : default : break ; } conway -> universe [ i ] [ j ] . state = CELL_NORMAL ; } } }
Calculates the next generation for a Conway universe
Calculates the next generation for a Conway universe
/* Prepare cells for next generation*/ /* Evolve to the next generation */