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