home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / C / R_LA4_02.ZIP / LIFE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1988-01-19  |  5.2 KB  |  151 lines

  1. /* LIFE.C - From page 576 of "Microsoft C Programming for       */
  2. /* the IBM" by Robert Lafore. Conway's Game of Life.            */
  3. /****************************************************************/
  4.  
  5. #define MAXCOL 80
  6. #define MAXROW 50
  7. #define ERASE "\x1B[2J"          /* code to erase screen */
  8.  
  9. main()
  10. {
  11. static char array[MAXCOL][MAXROW];  /* array echoes screen */
  12. void putscr(), getinit();
  13. char neigh, getscr();
  14. int col, row;
  15. int count = 0;                      /* generation counter */
  16.  
  17.    getinit();                       /* get initial cells */
  18.    while (1)  {                     /* once per generation */
  19.       for(col = 1; col < MAXCOL - 1; col++)  /*transfer screen*/
  20.          for(row = 1; row < MAXROW - 1; row++)  /*to array*/
  21.             array[col][row] = getscr(col, row);
  22.       printf(ERASE);
  23.       printf("\x1B[24;1H");         /* position cursor */
  24.       printf("Generation %d", ++count);    /*print generation*/
  25.       for(col = 1; col < MAXCOL - 1; col++)     /*for every cell*/
  26.          for(row = 1; row < MAXROW - 1; row++)  {
  27.             neigh =  array[col-1][row-1] +    /*find total no.*/
  28.                      array[col  ][row-1] +      /* of neighbors*/
  29.                      array[col+1][row-1] +
  30.                      array[col-1][row  ] +
  31.                      array[col+1][row  ] +
  32.                      array[col-1][row+1] +
  33.                      array[col  ][row+1] +
  34.                      array[col+1][row+1];
  35.             if(array[col][row] == 0)  {      /*if no cell*/
  36.                if(neigh == 3)                /* & 3 neighbors*/
  37.                   putscr(col, row);          /*a cell is born*/
  38.             }
  39.             else if(neigh == 2 || neigh == 3) /*if 2 or 3 neighbors*/
  40.                putscr(col, row);             /*cell lives*/
  41.          }                                   /*done one cell*/
  42.       for(col = 0; col < MAXCOL; col++)      /*clear array*/
  43.          for(row = 0; row < MAXROW; row++)
  44.             array[col][row] = 0;
  45.    }        /*end while*/  /*done generation*/
  46. }        /*end main*/
  47.  
  48. /* getinit() */   /* gets initial elements, puts them on screen */
  49. #define END 79
  50. #define INSERT 82
  51. #define C_UPUP 72
  52. #define C_DOWN 80
  53. #define C_LEFT 75
  54. #define C_RITE 77
  55. void getinit()
  56. {
  57. int col,row;
  58. char keycode;
  59. void putscr();
  60.  
  61.    printf(ERASE);
  62.    printf("\x1B[22;1H");               /*position cursor*/
  63.    printf("Move cursor with arrow keys.\n");
  64.    printf("Place cell with [Ins] key, [End] when done.");
  65.    col = 40;
  66.    row = 25;
  67.    while(keycode != END)  {            /*return on [End] */
  68.       if(kbhit())                      /*true if key struck*/
  69.          if(getch() == 0)  {           /*extended code*/
  70.             switch((keycode = getch()))  {   /*read code*/
  71.                case C_UPUP:
  72.                   --row;
  73.                   break;
  74.                case C_DOWN:
  75.                   ++row;
  76.                   break;
  77.                case C_RITE:
  78.                   ++col;
  79.                   break;
  80.                case C_LEFT:
  81.                   --col;
  82.                   break;
  83.                case INSERT:
  84.                   putscr(col, row);
  85.                   break;
  86.             }     /*end switch*/
  87.          }     /*end if*/
  88.    }     /*end while*/
  89. }     /*end getinit() */
  90.  
  91. /* putscr() */    /*puts small square in screen mem at col, row*/
  92.                   /*cols from 0 to 79, rows from 0 to 49*/
  93. #define TOP '\xDF'      /*upper half of char*/
  94. #define BOT '\xDC'      /*lower half of char*/
  95. #define BOTH '\xDB'     /*full char rectangle*/
  96.  
  97. void putscr(col, row)
  98. int col, row;
  99. {
  100. char newch, ch;
  101. int far *farptr;                    /*ptr to screen mem*/
  102. int actrow;                         /*actual row num*/
  103. int topbot;                         /*top or bott of row*/
  104.  
  105.    farptr = (int far *) 0xB0000000;
  106.    actrow = row >> 1;               /*row divided by 2*/
  107.    topbot = row & 0x0001;           /*odd or even row*/
  108.    ch = *(farptr + actrow*80 + col);   /*get actual char*/
  109.    if(topbot)                       /*if we're placing bot*/
  110.       if(ch == TOP || ch == BOTH)
  111.          newch = BOTH;
  112.       else
  113.          newch = BOT;
  114.    else                             /*if we're placing top*/
  115.       if(ch == BOT || ch == BOTH)
  116.          newch = BOTH;
  117.       else
  118.          newch = TOP;
  119.    *(farptr + actrow*80 + col) = (newch & 0x00ff) | 0x0700;
  120. }
  121.  
  122. /* getscr() */    /*returns 1 or 0 from screen location col, row*/
  123.                   /*cols from 0 to 79, rows from 0 to 49 */
  124. #define TOP '\xDF'      /*upper half of char*/
  125. #define BOT '\xDC'      /*lower half of char*/
  126. #define BOTH '\xDB'     /*full char rectangle*/
  127. #define TRUE 1
  128. #define FALSE 0
  129.  
  130. char getscr(col, row)
  131. int col, row;
  132. {
  133. char ch;
  134. int far *farptr;                    /*ptr to screen mem*/
  135. int actrow;                         /*actual row num*/
  136. int bottom;                         /*top or bott of row*/
  137.  
  138.    farptr = (int far *) 0xB0000000;
  139.    actrow = row >> 1;               /*row divided by 2*/
  140.    bottom = row & 0x0001;           /*odd or even row*/
  141.    ch = *(farptr + actrow*80 + col);   /*get actual char*/
  142.    if(ch == BOTH)
  143.       return(TRUE);
  144.    else
  145.       if((bottom && ch == BOT) || (!bottom && ch == TOP))
  146.          return(TRUE);
  147.       else
  148.          return(FALSE);
  149. }
  150.  
  151.