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