xref: /netbsd-src/lib/libcurses/PSD.doc/twinkle1.c (revision 38023541164cff097d5fadec63134189b1453b8c)
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 .\"	from: @(#)twinkle1.c	8.1 (Berkeley) 6/8/93
33 .\"	$Id: twinkle1.c,v 1.2 1993/11/09 04:09:57 cgd Exp $
34 .\"
35 # include	<curses.h>
36 # include	<signal.h>
37 
38 /*
39  * the idea for this program was a product of the imagination of
40  * Kurt Schoens.  Not responsible for minds lost or stolen.
41  */
42 
43 # define	NCOLS	80
44 # define	NLINES	24
45 # define	MAXPATTERNS	4
46 
47 typedef struct {
48 	int	y, x;
49 } LOCS;
50 
51 LOCS	Layout[NCOLS * NLINES];	/* current board layout */
52 
53 int	Pattern,		/* current pattern number */
54 	Numstars;		/* number of stars in pattern */
55 
56 char	*getenv();
57 
58 int	die();
59 
60 main()
61 {
62 	srand(getpid());		/* initialize random sequence */
63 
64 	initscr();
65 	signal(SIGINT, die);
66 	noecho();
67 	nonl();
68 	leaveok(stdscr, TRUE);
69 	scrollok(stdscr, FALSE);
70 
71 	for (;;) {
72 		makeboard();		/* make the board setup */
73 		puton('*');		/* put on '*'s */
74 		puton(' ');		/* cover up with ' 's */
75 	}
76 }
77 
78 /*
79  * On program exit, move the cursor to the lower left corner by
80  * direct addressing, since current location is not guaranteed.
81  * We lie and say we used to be at the upper right corner to guarantee
82  * absolute addressing.
83  */
84 die()
85 {
86 	signal(SIGINT, SIG_IGN);
87 	mvcur(0, COLS - 1, LINES - 1, 0);
88 	endwin();
89 	exit(0);
90 }
91 
92 
93 /*
94  * Make the current board setup.  It picks a random pattern and
95  * calls ison() to determine if the character is on that pattern
96  * or not.
97  */
98 makeboard()
99 {
100 	reg int		y, x;
101 	reg LOCS	*lp;
102 
103 	Pattern = rand() % MAXPATTERNS;
104 	lp = Layout;
105 	for (y = 0; y < NLINES; y++)
106 		for (x = 0; x < NCOLS; x++)
107 			if (ison(y, x)) {
108 				lp->y = y;
109 				lp->x = x;
110 				lp++;
111 			}
112 	Numstars = lp - Layout;
113 }
114 
115 /*
116  * Return TRUE if (y, x) is on the current pattern.
117  */
118 ison(y, x)
119 reg int	y, x; {
120 
121 	switch (Pattern) {
122 	  case 0:	/* alternating lines */
123 		return !(y & 01);
124 	  case 1:	/* box */
125 		if (x >= LINES && y >= NCOLS)
126 			return FALSE;
127 		if (y < 3 || y >= NLINES - 3)
128 			return TRUE;
129 		return (x < 3 || x >= NCOLS - 3);
130 	  case 2:	/* holy pattern! */
131 		return ((x + y) & 01);
132 	  case 3:	/* bar across center */
133 		return (y >= 9 && y <= 15);
134 	}
135 	/* NOTREACHED */
136 }
137 
138 puton(ch)
139 reg char	ch;
140 {
141 	reg LOCS	*lp;
142 	reg int		r;
143 	reg LOCS	*end;
144 	LOCS		temp;
145 
146 	end = &Layout[Numstars];
147 	for (lp = Layout; lp < end; lp++) {
148 		r = rand() % Numstars;
149 		temp = *lp;
150 		*lp = Layout[r];
151 		Layout[r] = temp;
152 	}
153 
154 	for (lp = Layout; lp < end; lp++) {
155 		mvaddch(lp->y, lp->x, ch);
156 		refresh();
157 	}
158 }
159