1 .\" Copyright (c) 1980, 1993
2 .\"	 The Regents of the University of California.  All rights reserved.
3 .\"
4 .\" %sccs.include.redist.roff%
5 .\"
6 .\"	@(#)twinkle1.c	8.1 (Berkeley) 06/08/93
7 .\"
8 # include	<curses.h>
9 # include	<signal.h>
10 
11 /*
12  * the idea for this program was a product of the imagination of
13  * Kurt Schoens.  Not responsible for minds lost or stolen.
14  */
15 
16 # define	NCOLS	80
17 # define	NLINES	24
18 # define	MAXPATTERNS	4
19 
20 typedef struct {
21 	int	y, x;
22 } LOCS;
23 
24 LOCS	Layout[NCOLS * NLINES];	/* current board layout */
25 
26 int	Pattern,		/* current pattern number */
27 	Numstars;		/* number of stars in pattern */
28 
29 char	*getenv();
30 
31 int	die();
32 
33 main()
34 {
35 	srand(getpid());		/* initialize random sequence */
36 
37 	initscr();
38 	signal(SIGINT, die);
39 	noecho();
40 	nonl();
41 	leaveok(stdscr, TRUE);
42 	scrollok(stdscr, FALSE);
43 
44 	for (;;) {
45 		makeboard();		/* make the board setup */
46 		puton('*');		/* put on '*'s */
47 		puton(' ');		/* cover up with ' 's */
48 	}
49 }
50 
51 /*
52  * On program exit, move the cursor to the lower left corner by
53  * direct addressing, since current location is not guaranteed.
54  * We lie and say we used to be at the upper right corner to guarantee
55  * absolute addressing.
56  */
57 die()
58 {
59 	signal(SIGINT, SIG_IGN);
60 	mvcur(0, COLS - 1, LINES - 1, 0);
61 	endwin();
62 	exit(0);
63 }
64 
65 
66 /*
67  * Make the current board setup.  It picks a random pattern and
68  * calls ison() to determine if the character is on that pattern
69  * or not.
70  */
71 makeboard()
72 {
73 	reg int		y, x;
74 	reg LOCS	*lp;
75 
76 	Pattern = rand() % MAXPATTERNS;
77 	lp = Layout;
78 	for (y = 0; y < NLINES; y++)
79 		for (x = 0; x < NCOLS; x++)
80 			if (ison(y, x)) {
81 				lp->y = y;
82 				lp->x = x;
83 				lp++;
84 			}
85 	Numstars = lp - Layout;
86 }
87 
88 /*
89  * Return TRUE if (y, x) is on the current pattern.
90  */
91 ison(y, x)
92 reg int	y, x; {
93 
94 	switch (Pattern) {
95 	  case 0:	/* alternating lines */
96 		return !(y & 01);
97 	  case 1:	/* box */
98 		if (x >= LINES && y >= NCOLS)
99 			return FALSE;
100 		if (y < 3 || y >= NLINES - 3)
101 			return TRUE;
102 		return (x < 3 || x >= NCOLS - 3);
103 	  case 2:	/* holy pattern! */
104 		return ((x + y) & 01);
105 	  case 3:	/* bar across center */
106 		return (y >= 9 && y <= 15);
107 	}
108 	/* NOTREACHED */
109 }
110 
111 puton(ch)
112 reg char	ch;
113 {
114 	reg LOCS	*lp;
115 	reg int		r;
116 	reg LOCS	*end;
117 	LOCS		temp;
118 
119 	end = &Layout[Numstars];
120 	for (lp = Layout; lp < end; lp++) {
121 		r = rand() % Numstars;
122 		temp = *lp;
123 		*lp = Layout[r];
124 		Layout[r] = temp;
125 	}
126 
127 	for (lp = Layout; lp < end; lp++) {
128 		mvaddch(lp->y, lp->x, ch);
129 		refresh();
130 	}
131 }
132