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