home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / program / curses / twinkle.c < prev   
Encoding:
C/C++ Source or Header  |  1987-08-24  |  1.5 KB  |  96 lines

  1. /* sample application using curses calls, this takes about 25 seconds on the */
  2. /* Atari and over 6 minutes on a mainframe, using a vt100 on a 4800 baud     */
  3. /* line. Even on the mainframe it uses 24 seconds of computation time.       */
  4.  
  5. #include "portab.h"
  6. #include "curses.h"
  7.  
  8. #define NCOLS 80
  9. #define NLINES 24
  10. #define MAXPATT 4
  11.  
  12. struct locs {
  13.     char y,x ;
  14.     } ;
  15.  
  16. typedef struct locs LOCS ;
  17.  
  18. LOCS Layout[NCOLS*NLINES] ;
  19.  
  20. int    pat ;
  21. int    nstar ;
  22.  
  23. main()
  24. {
  25.     int i ;
  26.  
  27.     initscr() ;
  28.     leaveok(stdscr,1) ;
  29.     for ( i = 0 ; i < 10 ; i++ )
  30.         {
  31.         makeboard() ;
  32.         puton('*') ;
  33.         puton(' ') ;
  34.         } ;
  35.     endwin() ;
  36. }
  37.  
  38. makeboard()
  39. {
  40.     int y,x ;
  41.     LOCS *lp ;
  42.     
  43.     pat = rand() % MAXPATT ;
  44.     lp = Layout ;
  45.     for ( y = 0 ; y < NLINES ; y++ )
  46.         for ( x = 0 ; x < NCOLS ; x++ )
  47.             if ( ison(y,x) )
  48.                 {
  49.                 lp->y = y ;
  50.                 lp->x = x ;
  51.                 lp++ ;
  52.                 } ;
  53.     nstar = lp - Layout ;
  54. }
  55.  
  56. ison(y,x)
  57. int y,x ;
  58. {
  59.     switch ( pat )
  60.         {
  61.         case 0:
  62.             return(!(y & 0x01)) ;
  63.         case 1:
  64.             if ( x >= NLINES && y >= NCOLS )
  65.                 return(0) ;
  66.             if ( y < 3 || y >= NLINES - 3 )
  67.                 return(1) ;
  68.             return((x < 3 || x >= NCOLS - 3 )) ;
  69.         case 2:
  70.             return(((x+y) & 0x01)) ;
  71.         case 3:
  72.             return(( y>= 9 && y <= 15 )) ;
  73.         } ;
  74. }
  75.  
  76. puton(ch)
  77. char ch ;
  78. {
  79.     LOCS  *lp, *end, temp ;
  80.     int r ;
  81.     
  82.     end = &(Layout[nstar]) ;
  83.     for ( lp = Layout ; lp < end ; lp++ )
  84.         {
  85.         r = ((unsigned int) rand()) % nstar ;
  86.         temp = *lp ;
  87.         *lp = Layout[r] ;
  88.         Layout[r] = temp ;
  89.         } ;
  90.     for ( lp = Layout ; lp < end ; lp++ )
  91.         {
  92.         mvaddch(lp->y,lp->x,ch) ;
  93.         refresh() ;
  94.         } ;
  95. }
  96.