Guest User

Untitled

a guest
Mar 13th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.10 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. /* Applies the rules of Life to the grid. */
  4. void updateGrid();
  5. /* Prints out a representation of the grid using binary digits. */
  6. void printGrid();
  7.  
  8. #define GRID_SIZE  10
  9. #define ITERATIONS 10
  10.  
  11. /*  Conway's Game of Life as a C exercise.
  12.  
  13.     TODO: Allow command line arguments specifying the grid size and number of iterations.
  14. */
  15.  
  16. int main(int argc, char **argv)
  17. {
  18.     int grid[GRID_SIZE][GRID_SIZE] = {
  19.         {0, 1, 0, 0, 0, 0, 0, 0, 0, 0},
  20.         {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  21.         {1, 0, 1, 0, 0, 0, 0, 0, 0, 0},
  22.         {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  23.         {0, 0, 0, 1, 1, 1, 0, 1, 0, 0},
  24.         {0, 1, 0, 1, 0, 0, 0, 0, 1, 0},
  25.         {0, 0, 0, 0, 0, 1, 1, 0, 0, 0},
  26.         {0, 0, 0, 0, 1, 0, 0, 1, 0, 0},
  27.         {1, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  28.         {0, 0, 1, 1, 1, 0, 1, 1, 1, 0}
  29.     };
  30.  
  31.     printGrid(grid);
  32.     printf("---\n");
  33.  
  34.     int i = 0;
  35.     while (i < ITERATIONS)
  36.     {
  37.         updateGrid(&grid);
  38.         i++;
  39.     }
  40.  
  41.     printGrid(grid);
  42.     return 0;
  43. }
  44.  
  45. void updateGrid(int gridn[][GRID_SIZE])
  46. {
  47.     int i = 1, j = 1, n = 0;
  48.     int alive = 0;
  49.     int grid[GRID_SIZE][GRID_SIZE];
  50.     memcpy(grid, gridn, sizeof(int) * GRID_SIZE * GRID_SIZE);
  51.  
  52.     while (i < GRID_SIZE)
  53.     {
  54.         while (j < GRID_SIZE)
  55.         {
  56.             if (grid[i-1][j-1] == 1) n++;
  57.             if (grid[i-1][j]   == 1) n++;
  58.             if (grid[i-1][j+1] == 1) n++;
  59.             if (grid[i][j-1]   == 1) n++;
  60.             if (grid[i][j]     == 1) alive = 1;
  61.             if (grid[i][j+1]   == 1) n++;
  62.             if (grid[i+1][j-1] == 1) n++;
  63.             if (grid[i+1][j]   == 1) n++;
  64.             if (grid[i+1][j+1] == 1) n++;
  65.  
  66.             // n is now the number of live neighbours of the current cell.
  67.             if (!alive && n == 3)
  68.             {   // Reproduce!
  69.                 gridn[i][j] = 1;
  70.             }
  71.             else if (alive && n != 2 && n != 3)
  72.             {   // Overcrowding or no opportunity for reproduction. :<
  73.                 gridn[i][j] = 0;
  74.             }
  75.  
  76.             n = 0;
  77.             alive = 0;
  78.             j++;
  79.         }
  80.  
  81.         j = 1;
  82.         i++;
  83.     }
  84. }
  85.  
  86. void printGrid(int grid[][GRID_SIZE])
  87. {
  88.     int i = 0, j = 0;
  89.  
  90.     while (i < GRID_SIZE)
  91.     {
  92.         while (j < GRID_SIZE)
  93.         {
  94.             printf("%d", grid[i][j]);
  95.             j++;
  96.         }
  97.  
  98.         printf("\n");
  99.         j = 0;
  100.         i++;
  101.     }
  102. }
Add Comment
Please, Sign In to add comment