home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / gnu / crssrc12 / twinkle1.c < prev    next >
C/C++ Source or Header  |  1993-07-29  |  3KB  |  136 lines

  1. /*
  2.  * Copyright 1980 Kenneth C. R. C. Arnold and The Regents of the
  3.  * University of California.  Permission is granted to freely
  4.  * distribute curses and its documentation provided that this
  5.  * notice is left intact.
  6.  */
  7.  
  8. #ifndef lint
  9. static char sccsid[] = "@(#)twinkle1.c    6.1 (Berkeley) 4/24/86";
  10. #endif not lint
  11.  
  12. # include    <curses.h>
  13. # include    <signal.h>
  14.  
  15. /*
  16.  * the idea for this program was a product of the imagination of
  17.  * Kurt Schoens.  Not responsible for minds lost or stolen.
  18.  */
  19.  
  20. # define    NCOLS    80
  21. # define    NLINES    24
  22. # define    MAXPATTERNS    4
  23.  
  24. typedef struct {
  25.     int    y, x;
  26. } LOCS;
  27.  
  28. LOCS    Layout[NCOLS * NLINES];    /* current board layout */
  29.  
  30. int    Pattern,        /* current pattern number */
  31.     Numstars;        /* number of stars in pattern */
  32.  
  33. char    *getenv();
  34.  
  35. int    die();
  36.  
  37. main()
  38. {
  39.     srand(getpid());        /* initialize random sequence */
  40.  
  41.     initscr();
  42.     signal(SIGINT, die);
  43.     noecho();
  44.     nonl();
  45.     leaveok(stdscr, TRUE);
  46.     scrollok(stdscr, FALSE);
  47.  
  48.     for (;;) {
  49.         makeboard();        /* make the board setup */
  50.         puton('*');        /* put on '*'s */
  51.         puton(' ');        /* cover up with ' 's */
  52.     }
  53. }
  54.  
  55. /*
  56.  * On program exit, move the cursor to the lower left corner by
  57.  * direct addressing, since current location is not guaranteed.
  58.  * We lie and say we used to be at the upper right corner to guarantee
  59.  * absolute addressing.
  60.  */
  61. die()
  62. {
  63.     signal(SIGINT, SIG_IGN);
  64.     mvcur(0, COLS - 1, LINES - 1, 0);
  65.     endwin();
  66.     exit(0);
  67. }
  68.  
  69.  
  70. /*
  71.  * Make the current board setup.  It picks a random pattern and
  72.  * calls ison() to determine if the character is on that pattern
  73.  * or not.
  74.  */
  75. makeboard()
  76. {
  77.     reg int        y, x;
  78.     reg LOCS    *lp;
  79.  
  80.     Pattern = rand() % MAXPATTERNS;
  81.     lp = Layout;
  82.     for (y = 0; y < NLINES; y++)
  83.         for (x = 0; x < NCOLS; x++)
  84.             if (ison(y, x)) {
  85.                 lp->y = y;
  86.                 lp->x = x;
  87.                 lp++;
  88.             }
  89.     Numstars = lp - Layout;
  90. }
  91.  
  92. /*
  93.  * Return TRUE if (y, x) is on the current pattern.
  94.  */
  95. ison(y, x)
  96. reg int    y, x; {
  97.  
  98.     switch (Pattern) {
  99.       case 0:    /* alternating lines */
  100.         return !(y & 01);
  101.       case 1:    /* box */
  102.         if (x >= LINES && y >= NCOLS)
  103.             return FALSE;
  104.         if (y < 3 || y >= NLINES - 3)
  105.             return TRUE;
  106.         return (x < 3 || x >= NCOLS - 3);
  107.       case 2:    /* holy pattern! */
  108.         return ((x + y) & 01);
  109.       case 3:    /* bar across center */
  110.         return (y >= 9 && y <= 15);
  111.     }
  112.     /* NOTREACHED */
  113. }
  114.  
  115. puton(ch)
  116. reg char    ch;
  117. {
  118.     reg LOCS    *lp;
  119.     reg int        r;
  120.     reg LOCS    *end;
  121.     LOCS        temp;
  122.  
  123.     end = &Layout[Numstars];
  124.     for (lp = Layout; lp < end; lp++) {
  125.         r = rand() % Numstars;
  126.         temp = *lp;
  127.         *lp = Layout[r];
  128.         Layout[r] = temp;
  129.     }
  130.  
  131.     for (lp = Layout; lp < end; lp++) {
  132.         mvaddch(lp->y, lp->x, ch);
  133.         refresh();
  134.     }
  135. }
  136.