home *** CD-ROM | disk | FTP | other *** search
/ Computer Club Elmshorn Atari PD / CCE_PD.iso / pc / 0600 / CCE_0636.ZIP / CCE_0636 / CURSES / CRSSRC12.ZOO / src / twinkle.c < prev    next >
C/C++ Source or Header  |  1991-09-27  |  2KB  |  110 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. /* see copyright notice in curses.c                                          */
  5. /* #include "portab.h" forbidden in MW C                                     */
  6.  
  7. #include "curses.h"
  8.  
  9. #define NCOLS 80
  10. #define NLINES 24
  11. #define MAXPATT 4
  12.  
  13. #ifndef MEGAMAX
  14. extern unsigned rand();
  15. #endif
  16.  
  17. struct locs {
  18.  char y,x ;
  19.  } ;
  20.  
  21. typedef struct locs LOCS ;
  22.  
  23. LOCS Layout[NCOLS*NLINES] ;;
  24.  
  25. int pat ;
  26. int nstar ;
  27.  
  28. main()
  29. {
  30.  int i ;
  31.  
  32.  initscr() ;
  33.  if (ERR) {
  34.    fprintf(stderr, "Unable to initialize\n");
  35.    exit(1);
  36.  }
  37.  leaveok(stdscr,1) ;
  38.  for ( i = 0 ; i < 10 ; i++ )
  39.   {
  40.   if (ERR) {
  41.     fprintf(stderr, "Error in makeboard loop\n");
  42.     exit(2);
  43.   }
  44.   makeboard() ;
  45.   puton('*') ;
  46.   puton(' ') ;
  47.   } ;
  48.  endwin() ;
  49. }
  50.  
  51. makeboard()
  52. {
  53.  int y,x ;
  54.  LOCS *lp ;
  55.  
  56.  pat = rand() % MAXPATT ;
  57.  lp = Layout ;
  58.  for ( y = 0 ; y < NLINES ; y++ )
  59.   for ( x = 0 ; x < NCOLS ; x++ )
  60.    if ( ison(y,x) )
  61.     {
  62.     lp->y = y ;
  63.     lp->x = x ;
  64.     lp++ ;
  65.     } ;
  66.  nstar = lp - Layout ;
  67. }
  68.  
  69. ison(y,x)
  70. int y,x ;
  71. {
  72.  switch ( pat )
  73.   {
  74.   case 0:
  75.    return(!(y & 0x01)) ;
  76.   case 1:
  77.    if ( x >= NLINES && y >= NCOLS )
  78.     return(0) ;
  79.    if ( y < 3 || y >= NLINES - 3 )
  80.     return(1) ;
  81.    return((x < 3 || x >= NCOLS - 3 )) ;
  82.   case 2:
  83.    return(((x+y) & 0x01)) ;
  84.   case 3:
  85.    return(( y>= 9 && y <= 15 )) ;
  86.   } ;
  87. }
  88.  
  89. puton(ch)
  90. char ch ;
  91. {
  92.  LOCS  *lp, *end, temp ;
  93.  int r ;
  94.  
  95.  end = &(Layout[nstar]) ;
  96.  for ( lp = Layout ; lp < end ; lp++ )
  97.   {
  98.   r = rand() % nstar ;
  99.   temp = *lp ;
  100.   *lp = Layout[r] ;
  101.   Layout[r] = temp ;
  102.   } ;
  103.  for ( lp = Layout ; lp < end ; lp++ )
  104.   {
  105.   mvaddch(lp->y,lp->x,ch) ;
  106.   refresh() ;
  107.   } ;
  108. }
  109.  
  110.