home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / 2DLIFE.C next >
C/C++ Source or Header  |  1997-07-05  |  3KB  |  114 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /*
  4. ** A quick "life" (2-d cellular automaton) implementation done in Turbo C 2.0 
  5. ** on the spur-of-the-moment by Jonathan Guthrie 9/20/1992 and donated to the 
  6. ** public domain.
  7. **
  8. ** In keeping with the guidelines of the C_ECHO, this program has been tested,
  9. ** and does seem to operate properly.
  10. */
  11.  
  12. #include <stdio.h>
  13. #include <conio.h>
  14. #include <stdlib.h>
  15. #include <time.h>
  16.  
  17. #ifndef random
  18.  #define random(num) (int)(((long)rand()*(num))/RAND_MAX)
  19. #endif
  20.  
  21. /*
  22. **  From VIDPORT.C, also in SNIPPETS
  23. */
  24.  
  25. void GotoXY(int col, int row);
  26. void ClrScrn(int vattrib);
  27.  
  28. #ifndef randomize
  29.  #define randomize() srand(((unsigned int)time(NULL))|1)
  30. #endif
  31.  
  32. #define     ROWS        24
  33. #define     COLS        80
  34. #define     GENERATIONS 10
  35.  
  36. int civ1[ROWS][COLS], civ2[ROWS][COLS];
  37.  
  38. void update_generation(int old[ROWS][COLS], int new[ROWS][COLS])
  39. {
  40.       int i, j, count;
  41.  
  42.       for (i = 0; i < ROWS; ++i)
  43.       {
  44.             for (j = 0; j < COLS; ++j)
  45.             {
  46.                   count = old[(i + ROWS - 1) % ROWS][(j + COLS - 1) % COLS] + 
  47.                         old[(i + ROWS - 1) % ROWS][j] +
  48.                         old[(i + ROWS - 1) % ROWS][(j + 1) % COLS] +
  49.                         old[i][(j + COLS - 1) % COLS] +
  50.                         old[i][(j + 1) % COLS] +
  51.                         old[(i + 1) % ROWS][(j + COLS - 1) % COLS] +
  52.                         old[(i + 1) % ROWS][j] +
  53.                         old[(i + 1) % ROWS][(j + 1) % COLS];
  54.  
  55.                   switch(count)
  56.                   {
  57.                   case 0:
  58.                   case 1:
  59.                   case 4:
  60.                   case 5:
  61.                   case 6:
  62.                   case 7:
  63.                   case 8:
  64.                         new[i][j] = 0;
  65.                         break;
  66.  
  67.                   case 2:
  68.                         new[i][j] = old[i][j];
  69.                         break;
  70.  
  71.                   case 3:
  72.                         new[i][j] = 1;
  73.                         break;
  74.                   }
  75.  
  76.                   GotoXY(j+1, i+1);
  77.                   putch(new[i][j] ? '*' : ' ');
  78.             } 
  79.       }
  80. }
  81.  
  82.  
  83. void initialize(void)
  84. {
  85.       int i, j;
  86.  
  87.       randomize();
  88.       ClrScrn(7);
  89.  
  90.       for (i = 0; i < ROWS; ++i)
  91.       {
  92.             for (j = 0; j < COLS; ++j)
  93.             {
  94.                   civ1[i][j] = random(2); 
  95.                   GotoXY(j+1, i+1);
  96.                   putch(civ1[i][j] ? '*' : ' ');
  97.             } 
  98.       }
  99. }
  100.  
  101.  
  102. int main(void)
  103. {
  104.       int i;
  105.  
  106.       initialize();
  107.       for (i = 0; i < GENERATIONS; ++i)
  108.       {
  109.             update_generation(civ1, civ2);
  110.             update_generation(civ2, civ1);
  111.       }
  112.       return EXIT_SUCCESS;
  113. }
  114.