#include "life.h" /* * Slow C life code. * * Conway's rules: if there are exactly 2 living neighbors, and the cell * is alive, it stays alive. If there are exactly 3 living neighbors * the cell becomes alive. Other numbers of living neighbors will cause * the cell to die. */ void life_xform(unsigned int *grid, unsigned int *grid2) { int r,c, living_neighbors; for (r = 0; r < height; r++) { for (c = 0; c < width; c++) { living_neighbors = cell_alive(grid,r-1,c-1) + cell_alive(grid,r-1,c) + cell_alive(grid,r-1,c+1) + cell_alive(grid,r,c-1) + cell_alive(grid,r,c+1) + cell_alive(grid,r+1,c-1) + cell_alive(grid,r+1,c) + cell_alive(grid,r+1,c+1); if (living_neighbors == 3) { set_cell(grid2,r,c,1); } else if (living_neighbors == 2) { set_cell(grid2,r,c,cell_alive(grid,r,c)); } else { set_cell(grid2,r,c,0); } } } }