Advertisement
Guest User

game of life

a guest
Mar 19th, 2012
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.06 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct cell_t_ {
  5.     struct cell_t_ *neighbours[8];
  6.     int on;
  7. } cell_t;
  8.  
  9. typedef struct world_t_ {
  10.     cell_t **array;
  11.     int width;
  12.     int height;
  13.     void *mem;
  14. } world_t;
  15.  
  16. void printworld(world_t *world, FILE *pOutput)
  17. {
  18.     int x, y;
  19.  
  20.     for(y = 0; y < world->height; y++) {
  21.         for(x = 0; x < world->width; x++) {
  22.             fprintf(pOutput, "%c", (world->array[y][x]).on ? 254 : ' ');
  23.         }
  24.         fputc((int)'\n', pOutput);
  25.     }
  26.     fflush(pOutput);
  27. }
  28.  
  29. void randomizeworld(world_t *world)
  30. {
  31.     int x, y;
  32.  
  33.     for(y = 0; y < world->height; y++) {
  34.         for(x = 0; x < world->width; x++) {
  35.             (world->array[y][x]).on = rand() & 1;
  36.         }
  37.     }
  38. }
  39.  
  40. void updateworld(world_t *world)
  41. {
  42.     int x, y, i, neighbours;
  43.  
  44.     for(y = 0; y < world->height; y++) {
  45.         for(x = 0; x < world->width; x++, neighbours = 0) {
  46.             for(i = 0; i < 8; i++)
  47.                 if((world->array[y][x].neighbours[i]) && ((world->array[y][x]).neighbours[i]->on & 1))
  48.                     neighbours++;
  49.  
  50.             if((neighbours < 2) || (neighbours > 3))
  51.                 (world->array[y][x]).on |= 2;
  52.             else if(neighbours == 3)
  53.                 (world->array[y][x]).on |= 4;
  54.         }
  55.     }
  56.  
  57.     for(y = 0; y < world->height; y++) {
  58.         for(x = 0; x < world->width; x++) {
  59.             if(world->array[y][x].on & 4)
  60.                 world->array[y][x].on = 1;
  61.             else if(world->array[y][x].on & 2)
  62.                 world->array[y][x].on = 0;
  63.         }
  64.     }
  65. }
  66.  
  67. void destroyworld(world_t *world)
  68. {
  69.     free(world->mem);
  70. }
  71.  
  72. int createworld(world_t *world, int width, int height)
  73. {
  74.     int i, j;
  75.     unsigned long base   = sizeof(cell_t *) * height;
  76.     unsigned long rowlen = sizeof(cell_t)   * width;
  77.  
  78.     if(!(world->mem = calloc(base + (rowlen * height), 1)))
  79.         return 0;
  80.  
  81.     world->array  = world->mem;
  82.     world->width  = width;
  83.     world->height = height;
  84.  
  85.     for(i = 0; i < height; i++) {
  86.         world->array[i] = world->mem + base + (i * rowlen);
  87.     }
  88.  
  89.     for(i = 0; i < height; i++) {
  90.         for(j = 0; j < width; j++) {
  91.             if(j != 0) {
  92.                 (world->array[i][j]).neighbours[3] = &(world->array[i][j - 1]);
  93.             }
  94.  
  95.             if(i != 0) {
  96.                 (world->array[i][j]).neighbours[1] = &(world->array[i - 1][j]);
  97.             }
  98.  
  99.             if(j != (width - 1)) {
  100.                 (world->array[i][j]).neighbours[4] = &(world->array[i][j + 1]);
  101.             }
  102.  
  103.             if(i != (height - 1)) {
  104.                 (world->array[i][j]).neighbours[6] = &(world->array[i + 1][j]);
  105.             }
  106.  
  107.             if((i != 0) && (j != 0)) {
  108.                 (world->array[i][j]).neighbours[0] = &(world->array[i - 1][j - 1]);
  109.             }
  110.  
  111.             if((i != (height - 1)) && (j != (width - 1))) {
  112.                 (world->array[i][j]).neighbours[7] = &(world->array[i + 1][j + 1]);
  113.             }
  114.  
  115.             if((i != (height - 1)) && (j != 0)) {
  116.                 (world->array[i][j]).neighbours[5] = &(world->array[i + 1][j - 1]);
  117.             }
  118.  
  119.             if((i != 0) && (j != (width - 1))) {
  120.                 (world->array[i][j]).neighbours[2] = &(world->array[i - 1][j + 1]);
  121.             }
  122.         }
  123.     }
  124.  
  125.     return 1;
  126. }
  127.  
  128. int main(int argc, char *argv[]) {
  129.     world_t gameoflife;
  130.  
  131.     if(createworld(&gameoflife, 79, 24)) {
  132.         randomizeworld(&gameoflife);
  133.         do {
  134.             printworld(&gameoflife, stdout);
  135.             getchar();
  136.             fflush(stdin);
  137.             updateworld(&gameoflife);
  138.         }while(1);
  139.         destroyworld(&gameoflife);
  140.     }
  141.  
  142.     return 0;
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement