162779Sbostic .\" Copyright (c) 1980, 1993
262779Sbostic .\"	 The Regents of the University of California.  All rights reserved.
348164Sbostic .\"
448164Sbostic .\" %sccs.include.redist.roff%
548164Sbostic .\"
6*62780Sbostic .\"	@(#)twinkle1.c	8.1 (Berkeley) 06/08/93
748164Sbostic .\"
827334Smckusick # include	<curses.h>
927334Smckusick # include	<signal.h>
1027334Smckusick 
1127334Smckusick /*
1227334Smckusick  * the idea for this program was a product of the imagination of
1327334Smckusick  * Kurt Schoens.  Not responsible for minds lost or stolen.
1427334Smckusick  */
1527334Smckusick 
1627334Smckusick # define	NCOLS	80
1727334Smckusick # define	NLINES	24
1827334Smckusick # define	MAXPATTERNS	4
1927334Smckusick 
2027336Smckusick typedef struct {
2127336Smckusick 	int	y, x;
2227336Smckusick } LOCS;
2327334Smckusick 
2427334Smckusick LOCS	Layout[NCOLS * NLINES];	/* current board layout */
2527334Smckusick 
2627334Smckusick int	Pattern,		/* current pattern number */
2727334Smckusick 	Numstars;		/* number of stars in pattern */
2827334Smckusick 
2927336Smckusick char	*getenv();
3027334Smckusick 
3127336Smckusick int	die();
3227334Smckusick 
3327336Smckusick main()
3427336Smckusick {
3527334Smckusick 	srand(getpid());		/* initialize random sequence */
3627334Smckusick 
3727334Smckusick 	initscr();
3827334Smckusick 	signal(SIGINT, die);
3927334Smckusick 	noecho();
4027334Smckusick 	nonl();
4127334Smckusick 	leaveok(stdscr, TRUE);
4227334Smckusick 	scrollok(stdscr, FALSE);
4327334Smckusick 
4427334Smckusick 	for (;;) {
4527334Smckusick 		makeboard();		/* make the board setup */
4627334Smckusick 		puton('*');		/* put on '*'s */
4727334Smckusick 		puton(' ');		/* cover up with ' 's */
4827334Smckusick 	}
4927334Smckusick }
5027334Smckusick 
5127334Smckusick /*
5227334Smckusick  * On program exit, move the cursor to the lower left corner by
5327334Smckusick  * direct addressing, since current location is not guaranteed.
5427334Smckusick  * We lie and say we used to be at the upper right corner to guarantee
5527334Smckusick  * absolute addressing.
5627334Smckusick  */
5727336Smckusick die()
5827336Smckusick {
5927334Smckusick 	signal(SIGINT, SIG_IGN);
6027336Smckusick 	mvcur(0, COLS - 1, LINES - 1, 0);
6127334Smckusick 	endwin();
6227334Smckusick 	exit(0);
6327334Smckusick }
6427334Smckusick 
6527334Smckusick 
6627334Smckusick /*
6727334Smckusick  * Make the current board setup.  It picks a random pattern and
6827334Smckusick  * calls ison() to determine if the character is on that pattern
6927334Smckusick  * or not.
7027334Smckusick  */
7127336Smckusick makeboard()
7227336Smckusick {
7327334Smckusick 	reg int		y, x;
7427334Smckusick 	reg LOCS	*lp;
7527334Smckusick 
7627334Smckusick 	Pattern = rand() % MAXPATTERNS;
7727334Smckusick 	lp = Layout;
7827334Smckusick 	for (y = 0; y < NLINES; y++)
7927334Smckusick 		for (x = 0; x < NCOLS; x++)
8027334Smckusick 			if (ison(y, x)) {
8127334Smckusick 				lp->y = y;
8227336Smckusick 				lp->x = x;
8327336Smckusick 				lp++;
8427334Smckusick 			}
8527334Smckusick 	Numstars = lp - Layout;
8627334Smckusick }
8727334Smckusick 
8827334Smckusick /*
8927334Smckusick  * Return TRUE if (y, x) is on the current pattern.
9027334Smckusick  */
9127334Smckusick ison(y, x)
9227334Smckusick reg int	y, x; {
9327334Smckusick 
9427334Smckusick 	switch (Pattern) {
9527334Smckusick 	  case 0:	/* alternating lines */
9627334Smckusick 		return !(y & 01);
9727334Smckusick 	  case 1:	/* box */
9827334Smckusick 		if (x >= LINES && y >= NCOLS)
9927334Smckusick 			return FALSE;
10027334Smckusick 		if (y < 3 || y >= NLINES - 3)
10127334Smckusick 			return TRUE;
10227334Smckusick 		return (x < 3 || x >= NCOLS - 3);
10327334Smckusick 	  case 2:	/* holy pattern! */
10427334Smckusick 		return ((x + y) & 01);
10527334Smckusick 	  case 3:	/* bar across center */
10627334Smckusick 		return (y >= 9 && y <= 15);
10727334Smckusick 	}
10827334Smckusick 	/* NOTREACHED */
10927334Smckusick }
11027334Smckusick 
11127334Smckusick puton(ch)
11227336Smckusick reg char	ch;
11327336Smckusick {
11427334Smckusick 	reg LOCS	*lp;
11527334Smckusick 	reg int		r;
11627334Smckusick 	reg LOCS	*end;
11727334Smckusick 	LOCS		temp;
11827334Smckusick 
11927334Smckusick 	end = &Layout[Numstars];
12027334Smckusick 	for (lp = Layout; lp < end; lp++) {
12127334Smckusick 		r = rand() % Numstars;
12227334Smckusick 		temp = *lp;
12327334Smckusick 		*lp = Layout[r];
12427334Smckusick 		Layout[r] = temp;
12527334Smckusick 	}
12627334Smckusick 
12727334Smckusick 	for (lp = Layout; lp < end; lp++) {
12827334Smckusick 		mvaddch(lp->y, lp->x, ch);
12927334Smckusick 		refresh();
13027334Smckusick 	}
13127334Smckusick }
132