home *** CD-ROM | disk | FTP | other *** search
/ Power Programming / powerprogramming1994.iso / progtool / microcrn / issue_45.arc / LIFE.ARC / LIFE.C < prev    next >
Text File  |  1988-04-02  |  7KB  |  270 lines

  1. /* Micro Cornucopia Magazine Issue #45
  2.    Life Figure 1  LIFE.C
  3.  
  4.    Hard coded for CGA */
  5.  
  6.  
  7. #include <graphics.h>
  8. #define BYTE unsigned char
  9. #define WORD unsigned int
  10. #define FALSE 0
  11. #define TRUE 1
  12.  
  13. /* rule parameters governing births and deaths */
  14. #define EL 2  /*envrnmnt,lower:death if n_cnt<EL*/
  15. #define EU 3  /*envrnmnt,upper:      or n_cnt>EU*/
  16. #define FL 3  /*fertlty,lower:birth if n_cnt>=FL*/
  17. #define FU 3  /*fertlty,upper:     and n_cnt<=FU*/
  18. /* configuration constants (array sizes) */
  19. #define MAX_ALIVE 600     /* max # living cells */
  20. #define MAX_GHOST 1700/*max # non-live neighbors*/
  21. #define MAX_BIRTH 300/* max # births/generation */
  22. #define MAX_DEATH 300/* max # deaths/generation */
  23. #define MAX_X 200             /* width of grid  */
  24. #define MAX_Y 200             /* height of grid */
  25. /* global variable declarations */
  26. BYTE n_cnt[MAX_X][MAX_Y]; /* neighbor count ... */
  27.                            /* ... for all cells */
  28. BYTE alive[2][MAX_ALIVE];  /* list/living cells */
  29. BYTE ghost[2][MAX_GHOST];  /*list/neighbor cells*/
  30. BYTE birth[2][MAX_BIRTH];  /* list/new births   */
  31. BYTE death[2][MAX_DEATH];  /* list/new deaths   */
  32. WORD alive_cnt; /* size of list of living cells */
  33. WORD ghost_cnt;    /* size of list of neighbors */
  34. WORD birth_cnt;       /* size of list of births */
  35. WORD death_cnt;       /* size of list of deaths */
  36.  
  37. void locate_deaths(void)
  38. {
  39.    BYTE c, x, y;
  40.    WORD index;
  41.  
  42.    death_cnt = index = 0;
  43.    while (index < alive_cnt)
  44.    {
  45.       x = alive[0][index];  y = alive[1][index];
  46.       c = n_cnt[x][y] - 1;
  47.       if (c >= EL && c <= EU) index++;
  48.       else
  49.       {
  50.          death[0][death_cnt] = x;
  51.          death[1][death_cnt] = y;
  52.          death_cnt++;
  53.          alive_cnt--;
  54.          alive[0][index] = alive[0][alive_cnt];
  55.          alive[1][index] = alive[1][alive_cnt];
  56.       }
  57.    }
  58. }
  59.  
  60. void locate_births(void)
  61. {
  62.    BYTE c, x, y;
  63.    WORD index;
  64.    birth_cnt = index = 0;
  65.    while (index < ghost_cnt)
  66.    {
  67.       x = ghost[0][index];  y = ghost[1][index];
  68.       c = n_cnt[x][y];
  69.       if (c >= FL && c <= FU)
  70.       {
  71.          if (x > 0 && y > 0 && x < MAX_X-1 &&
  72.              y < MAX_Y-1)
  73.          {
  74.             birth[0][birth_cnt] = x;
  75.             birth[1][birth_cnt] = y;
  76.             birth_cnt++;
  77.          }
  78.          ghost_cnt--;
  79.          ghost[0][index] = ghost[0][ghost_cnt];
  80.          ghost[1][index] = ghost[1][ghost_cnt];
  81.       }
  82.       else if (c == 0)
  83.       {
  84.          ghost_cnt--;
  85.          ghost[0][index] = ghost[0][ghost_cnt];
  86.          ghost[1][index] = ghost[1][ghost_cnt];
  87.       }
  88.       else index++;
  89.    }
  90. }
  91.  
  92. void process_births(void)
  93. {
  94.    BYTE x, y;
  95.    WORD index;
  96.  
  97.    for (index = 0; index < birth_cnt; index++)
  98.    {
  99.       x = alive[0][alive_cnt] = birth[0][index];
  100.       y = alive[1][alive_cnt] = birth[1][index];
  101.       alive_cnt++;
  102.       n_cnt[x][y]++;
  103.       x++;
  104.       if (n_cnt[x][y]++ == 0)
  105.       {
  106.          ghost[0][ghost_cnt] = x;
  107.          ghost[1][ghost_cnt] = y;
  108.          ghost_cnt++;
  109.       }
  110.       y++;
  111.       if (n_cnt[x][y]++ == 0)
  112.       {
  113.          ghost[0][ghost_cnt] = x;
  114.          ghost[1][ghost_cnt] = y;
  115.          ghost_cnt++;
  116.       }
  117.       x--;
  118.       if (n_cnt[x][y]++ == 0)
  119.       {
  120.          ghost[0][ghost_cnt] = x;
  121.          ghost[1][ghost_cnt] = y;
  122.          ghost_cnt++;
  123.       }
  124.       x--;
  125.       if (n_cnt[x][y]++ == 0)
  126.       {
  127.          ghost[0][ghost_cnt] = x;
  128.          ghost[1][ghost_cnt] = y;
  129.          ghost_cnt++;
  130.       }
  131.       y--;
  132.       if (n_cnt[x][y]++ == 0)
  133.       {
  134.          ghost[0][ghost_cnt] = x;
  135.          ghost[1][ghost_cnt] = y;
  136.          ghost_cnt++;
  137.       }
  138.       y--;
  139.       if (n_cnt[x][y]++ == 0)
  140.       {
  141.          ghost[0][ghost_cnt] = x;
  142.          ghost[1][ghost_cnt] = y;
  143.          ghost_cnt++;
  144.       }
  145.       x++;
  146.       if (n_cnt[x][y]++ == 0)
  147.       {
  148.          ghost[0][ghost_cnt] = x;
  149.          ghost[1][ghost_cnt] = y;
  150.          ghost_cnt++;
  151.       }
  152.       x++;
  153.       if (n_cnt[x][y]++ == 0)
  154.       {
  155.          ghost[0][ghost_cnt] = x;
  156.          ghost[1][ghost_cnt] = y;
  157.          ghost_cnt++;
  158.       }
  159.    }
  160. }
  161.  
  162.  
  163. void process_deaths(void)
  164. {
  165.    BYTE i, j, x, y;
  166.    WORD index;
  167.  
  168.    for (index = 0; index < death_cnt; index++)
  169.    {
  170.       x = death[0][index];
  171.       y = death[1][index];
  172.       for (i = x - 1; i < x + 2; i++)
  173.       for (j = y - 1; j < y + 2; j++)
  174.          n_cnt[i][j]--;
  175.    }
  176.    for (index = 0; index < death_cnt; index++)
  177.    {
  178.       x = death[0][index];
  179.       y = death[1][index];
  180.       if (n_cnt[x][y] != 0)
  181.       {
  182.          ghost[0][ghost_cnt] = x;
  183.          ghost[1][ghost_cnt] = y;
  184.          ghost_cnt++;
  185.       }
  186.    }
  187. }
  188.  
  189. void screen_update(void)
  190. {
  191.    BYTE x, y;
  192.    WORD index;
  193.  
  194.    for (index = 0; index < birth_cnt; index++)
  195.    {
  196.       x = birth[0][index];  y = birth[1][index];
  197.       putpixel(x, y, 1);
  198.    }
  199.    for (index = 0; index < death_cnt; index++)
  200.    {
  201.       x = death[0][index];  y = death[1][index];
  202.       putpixel(x, y, 2);
  203.    }
  204. }
  205.  
  206. void init_grid(void)
  207. {
  208.    BYTE x, y;
  209.    WORD index;
  210.  
  211.    for (x = 0; x < MAX_X; x++)
  212.    for (y = 0; y < MAX_Y; y++)
  213.       n_cnt[x][y] = 0;
  214.    birth[0][0] = 100;   birth[1][0] = 100;
  215.    birth[0][1] = 101;   birth[1][1] = 100;
  216.    birth[0][2] = 102;   birth[1][2] = 100;
  217.    birth[0][3] = 102;   birth[1][3] = 99;
  218.    birth[0][4] = 101;   birth[1][4] = 101;
  219.    birth_cnt = 5;   alive_cnt = 0;
  220.    ghost_cnt = 0;
  221.    screen_update();
  222.    for (index = 0; index < birth_cnt; index++)
  223.    {
  224.       x = birth[0][index];  y = birth[1][index];
  225.       n_cnt[x][y]++;
  226.    }
  227.    process_births();
  228.    for (index = 0; index < birth_cnt; index++)
  229.    {
  230.       x = birth[0][index];  y = birth[1][index];
  231.       n_cnt[x][y]--;
  232.    }
  233. }
  234.  
  235. void main(void)
  236. {
  237.    int step = FALSE;  int done = FALSE;
  238.    int generation = 0;
  239.    int gd = CGA;  int gm = CGAC0;
  240.    initgraph(&gd, &gm, "");
  241.    printf("Generation: 0    Run  Pause  Step  Quit");
  242.    init_grid();
  243.    gotoxy(1, 1);  getch();
  244.    while (!done)
  245.    {
  246.       gotoxy(13, 1);  printf("%d", ++generation);
  247.       locate_deaths();  locate_births();
  248.       process_births();  process_deaths();
  249.       screen_update();
  250.       if (kbhit() || step)
  251.       {
  252.          switch(toupper(getch()))
  253.          {
  254.             case 'Q': done = TRUE;
  255.                       break;
  256.             case 'P': getch();
  257.                       break;
  258.             case 'S': step = 1;
  259.                       break;
  260.             case 'R': step = 0;
  261.                       break;
  262.          }
  263.       }
  264.    }
  265.    closegraph();
  266. }
  267.  
  268.  
  269.  
  270.