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