Home | History | Annotate | Line # | Download | only in PSD.doc
twinkle1.c revision 1.3
      1 .\" Copyright (c) 1980, 1993
      2 .\"	 The Regents of the University of California.  All rights reserved.
      3 .\"
      4 .\" Redistribution and use in source and binary forms, with or without
      5 .\" modification, are permitted provided that the following conditions
      6 .\" are met:
      7 .\" 1. Redistributions of source code must retain the above copyright
      8 .\"    notice, this list of conditions and the following disclaimer.
      9 .\" 2. Redistributions in binary form must reproduce the above copyright
     10 .\"    notice, this list of conditions and the following disclaimer in the
     11 .\"    documentation and/or other materials provided with the distribution.
     12 .\" 3. All advertising materials mentioning features or use of this software
     13 .\"    must display the following acknowledgement:
     14 .\"	This product includes software developed by the University of
     15 .\"	California, Berkeley and its contributors.
     16 .\" 4. Neither the name of the University nor the names of its contributors
     17 .\"    may be used to endorse or promote products derived from this software
     18 .\"    without specific prior written permission.
     19 .\"
     20 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     21 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     24 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     26 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30 .\" SUCH DAMAGE.
     31 .\"
     32 .\"	@(#)twinkle1.c	8.1 (Berkeley) 6/8/93
     33 .\"
     34 # include	<curses.h>
     35 # include	<signal.h>
     36 
     37 /*
     38  * the idea for this program was a product of the imagination of
     39  * Kurt Schoens.  Not responsible for minds lost or stolen.
     40  */
     41 
     42 # define	NCOLS	80
     43 # define	NLINES	24
     44 # define	MAXPATTERNS	4
     45 
     46 typedef struct {
     47 	int	y, x;
     48 } LOCS;
     49 
     50 LOCS	Layout[NCOLS * NLINES];	/* current board layout */
     51 
     52 int	Pattern,		/* current pattern number */
     53 	Numstars;		/* number of stars in pattern */
     54 
     55 char	*getenv();
     56 
     57 int	die();
     58 
     59 main()
     60 {
     61 	srand(getpid());		/* initialize random sequence */
     62 
     63 	initscr();
     64 	signal(SIGINT, die);
     65 	noecho();
     66 	nonl();
     67 	leaveok(stdscr, TRUE);
     68 	scrollok(stdscr, FALSE);
     69 
     70 	for (;;) {
     71 		makeboard();		/* make the board setup */
     72 		puton('*');		/* put on '*'s */
     73 		puton(' ');		/* cover up with ' 's */
     74 	}
     75 }
     76 
     77 /*
     78  * On program exit, move the cursor to the lower left corner by
     79  * direct addressing, since current location is not guaranteed.
     80  * We lie and say we used to be at the upper right corner to guarantee
     81  * absolute addressing.
     82  */
     83 die()
     84 {
     85 	signal(SIGINT, SIG_IGN);
     86 	mvcur(0, COLS - 1, LINES - 1, 0);
     87 	endwin();
     88 	exit(0);
     89 }
     90 
     91 
     92 /*
     93  * Make the current board setup.  It picks a random pattern and
     94  * calls ison() to determine if the character is on that pattern
     95  * or not.
     96  */
     97 makeboard()
     98 {
     99 	reg int		y, x;
    100 	reg LOCS	*lp;
    101 
    102 	Pattern = rand() % MAXPATTERNS;
    103 	lp = Layout;
    104 	for (y = 0; y < NLINES; y++)
    105 		for (x = 0; x < NCOLS; x++)
    106 			if (ison(y, x)) {
    107 				lp->y = y;
    108 				lp->x = x;
    109 				lp++;
    110 			}
    111 	Numstars = lp - Layout;
    112 }
    113 
    114 /*
    115  * Return TRUE if (y, x) is on the current pattern.
    116  */
    117 ison(y, x)
    118 reg int	y, x; {
    119 
    120 	switch (Pattern) {
    121 	  case 0:	/* alternating lines */
    122 		return !(y & 01);
    123 	  case 1:	/* box */
    124 		if (x >= LINES && y >= NCOLS)
    125 			return FALSE;
    126 		if (y < 3 || y >= NLINES - 3)
    127 			return TRUE;
    128 		return (x < 3 || x >= NCOLS - 3);
    129 	  case 2:	/* holy pattern! */
    130 		return ((x + y) & 01);
    131 	  case 3:	/* bar across center */
    132 		return (y >= 9 && y <= 15);
    133 	}
    134 	/* NOTREACHED */
    135 }
    136 
    137 puton(ch)
    138 reg char	ch;
    139 {
    140 	reg LOCS	*lp;
    141 	reg int		r;
    142 	reg LOCS	*end;
    143 	LOCS		temp;
    144 
    145 	end = &Layout[Numstars];
    146 	for (lp = Layout; lp < end; lp++) {
    147 		r = rand() % Numstars;
    148 		temp = *lp;
    149 		*lp = Layout[r];
    150 		Layout[r] = temp;
    151 	}
    152 
    153 	for (lp = Layout; lp < end; lp++) {
    154 		mvaddch(lp->y, lp->x, ch);
    155 		refresh();
    156 	}
    157 }
    158