1 /*
2  * Copyright (c) 1980 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 
18 #ifndef lint
19 char copyright[] =
20 "@(#) Copyright (c) 1980 The Regents of the University of California.\n\
21  All rights reserved.\n";
22 #endif /* not lint */
23 
24 #ifndef lint
25 static char sccsid[] = "@(#)twinkle1.c	6.2 (Berkeley) 03/17/89";
26 #endif /* not lint */
27 
28 # include	<curses.h>
29 # include	<signal.h>
30 
31 /*
32  * the idea for this program was a product of the imagination of
33  * Kurt Schoens.  Not responsible for minds lost or stolen.
34  */
35 
36 # define	NCOLS	80
37 # define	NLINES	24
38 # define	MAXPATTERNS	4
39 
40 typedef struct {
41 	int	y, x;
42 } LOCS;
43 
44 LOCS	Layout[NCOLS * NLINES];	/* current board layout */
45 
46 int	Pattern,		/* current pattern number */
47 	Numstars;		/* number of stars in pattern */
48 
49 char	*getenv();
50 
51 int	die();
52 
53 main()
54 {
55 	srand(getpid());		/* initialize random sequence */
56 
57 	initscr();
58 	signal(SIGINT, die);
59 	noecho();
60 	nonl();
61 	leaveok(stdscr, TRUE);
62 	scrollok(stdscr, FALSE);
63 
64 	for (;;) {
65 		makeboard();		/* make the board setup */
66 		puton('*');		/* put on '*'s */
67 		puton(' ');		/* cover up with ' 's */
68 	}
69 }
70 
71 /*
72  * On program exit, move the cursor to the lower left corner by
73  * direct addressing, since current location is not guaranteed.
74  * We lie and say we used to be at the upper right corner to guarantee
75  * absolute addressing.
76  */
77 die()
78 {
79 	signal(SIGINT, SIG_IGN);
80 	mvcur(0, COLS - 1, LINES - 1, 0);
81 	endwin();
82 	exit(0);
83 }
84 
85 
86 /*
87  * Make the current board setup.  It picks a random pattern and
88  * calls ison() to determine if the character is on that pattern
89  * or not.
90  */
91 makeboard()
92 {
93 	reg int		y, x;
94 	reg LOCS	*lp;
95 
96 	Pattern = rand() % MAXPATTERNS;
97 	lp = Layout;
98 	for (y = 0; y < NLINES; y++)
99 		for (x = 0; x < NCOLS; x++)
100 			if (ison(y, x)) {
101 				lp->y = y;
102 				lp->x = x;
103 				lp++;
104 			}
105 	Numstars = lp - Layout;
106 }
107 
108 /*
109  * Return TRUE if (y, x) is on the current pattern.
110  */
111 ison(y, x)
112 reg int	y, x; {
113 
114 	switch (Pattern) {
115 	  case 0:	/* alternating lines */
116 		return !(y & 01);
117 	  case 1:	/* box */
118 		if (x >= LINES && y >= NCOLS)
119 			return FALSE;
120 		if (y < 3 || y >= NLINES - 3)
121 			return TRUE;
122 		return (x < 3 || x >= NCOLS - 3);
123 	  case 2:	/* holy pattern! */
124 		return ((x + y) & 01);
125 	  case 3:	/* bar across center */
126 		return (y >= 9 && y <= 15);
127 	}
128 	/* NOTREACHED */
129 }
130 
131 puton(ch)
132 reg char	ch;
133 {
134 	reg LOCS	*lp;
135 	reg int		r;
136 	reg LOCS	*end;
137 	LOCS		temp;
138 
139 	end = &Layout[Numstars];
140 	for (lp = Layout; lp < end; lp++) {
141 		r = rand() % Numstars;
142 		temp = *lp;
143 		*lp = Layout[r];
144 		Layout[r] = temp;
145 	}
146 
147 	for (lp = Layout; lp < end; lp++) {
148 		mvaddch(lp->y, lp->x, ch);
149 		refresh();
150 	}
151 }
152