home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_300 / 388_01 / curses / twinkle.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-03  |  1.3 KB  |  94 lines

  1. /*
  2.  *    twinkle.c
  3.  */
  4.  
  5. #include "curses.h"
  6.  
  7. #define NDISPLAY    10
  8. #define NCOLS         80
  9. #define NLINES         25
  10. #define MAXPATT     4
  11.  
  12. typedef struct {
  13.     char y, x;
  14. } LOCS;
  15.  
  16.  
  17. LOCS Layout[ NCOLS*NLINES ];
  18.  
  19. int    pat;
  20. int    nstar;
  21.  
  22.  
  23. int ison( y, x )
  24.     int y, x;
  25. {
  26.     switch( pat ){
  27.     case 0:     /* every odd line is solid */
  28.         return( !(y & 0x01) );
  29.     case 1:     /* box shape - 3 line/col boarder */
  30.         if( x >= NLINES && y >= NCOLS ) 
  31.             return( 0 );
  32.         if( y < 3 || y >= NLINES - 3 ) 
  33.             return( 1 );
  34.         return( x < 3 || x >= NCOLS - 3 );
  35.     case 2:     /* fill  screen every other dot */
  36.         return( (x + y) & 0x01 );
  37.     case 3:     /* solid bar in the middle */
  38.         return( y >= 9 && y <= 15 );
  39.     }
  40. }
  41.  
  42.  
  43. void makeboard()
  44. {
  45.     int y, x;
  46.     LOCS *lp;
  47.     
  48.     pat = rand() % MAXPATT;
  49.     lp = Layout;
  50.     for( y = 0; y < NLINES; y++ )
  51.         for( x = 0; x < NCOLS; x++ )
  52.             if( ison( y, x ) ){
  53.                 lp->y = y;
  54.                 lp->x = x;
  55.                 lp++;
  56.             }
  57.     nstar = lp - Layout;
  58. }
  59.  
  60.  
  61. void puton( ch )
  62.     char ch;
  63. {
  64.     LOCS *lp, *end, temp;
  65.     int r;
  66.     
  67.     end = &Layout[ nstar ];
  68.     for( lp = Layout; lp < end; lp++ ){
  69.         r = rand() % nstar;
  70.         temp = *lp;
  71.         *lp = Layout[ r ];
  72.         Layout[ r ] = temp;
  73.     }
  74.     for( lp = Layout; lp < end; lp++ ){
  75.         mvaddch( lp->y, lp->x, ch );
  76.         refresh();
  77.     }
  78. }
  79.  
  80.  
  81. void main()
  82. {
  83.     int i;
  84.  
  85.     initscr();
  86.     scrollok( stdscr, 0);
  87.     for( i = 0; i < NDISPLAY; i++ ){
  88.         makeboard();
  89.         puton( '*' );
  90.         puton( ' ' );
  91.     }
  92.     endwin();
  93. }
  94.