home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 8 Other / 08-Other.zip / TEXTCA.ZIP / TEXTCA2.DOC next >
Text File  |  1991-02-02  |  2KB  |  41 lines

  1. The code below is the C source code fragment for an artificial life program that
  2. has been floating around recently.  It is of interest primarily to those (like myself)
  3. who see a connection between the way both natural life and digital processing
  4. can bring apparant order from randomness.
  5.  
  6. Textca2.exe is the OS/2 binary.  It is a mutli-threaded program which uses
  7. this code fragment as the main routine, and sets up a thread that polls
  8. the keyboard in the background.  Pressing the ESC or "q" keys exits the 
  9. program; any other ASCII key resets the rule table.  The output shows how
  10. the cells reproduce themselves in a life-like way.
  11.  
  12. main()
  13. {
  14.         int ruletable[8] = {0,1,1,1,1,0,0,0};    /* This is changeable. */
  15.         int i, nabecode, key, exitflag = 0;
  16.         int oldbuff[80], newbuff[80];
  17.  
  18.     for (i=0; i < 80; i++)  /* Empty out the buffers. */
  19.         {
  20.                 oldbuff[i] = 0;
  21.                 newbuff[i] = 0;
  22.         }
  23.         oldbuff[40] = 1;                /* Turn one cell on */
  24.         while (!exitflag)
  25.         {
  26.                 for (i=1; i<79; i++) /* Each cell looks to left and right */
  27.                 {                    /* Don't update the first or last cell. */
  28.                nabecode = 4 * oldbuff[i-1] + 2 * oldbuff[i] + oldbuff[i+1];
  29.                     newbuff[i] = ruletable[nabecode]; /* 0 <= nabecode <= 8 */
  30.                 }
  31.                 for (i=0; i<80; i++) /* We show all 80 cells, though we  only */
  32.                 {                    /* update the center 78 cells. */
  33.                        if (newbuff[i] == 1)
  34.                           printf("%c",219); /* 219 is ASCII number of a block */
  35.                           else printf(".");
  36.                           oldbuff[i] = newbuff[i];
  37.                 }
  38.         }
  39. }
  40.  
  41.