4.1cBSD/usr/doc/curses/twinkle1.gr
'-F
.ds =F twinkle1.c
.ds =M Nov 8 12:06 1980
'wh 0 vH
'wh -1i vF
\*(+K#\*(-K \*(+Kinclude\*(-K\h'|21n'<curses\&.h>
\*(+K#\*(-K \*(+Kinclude\*(-K\h'|21n'<signal\&.h>
\fI\h'\w' 'u-\w'/'u'/\fP\fI*\fP\c\c
'+C
\fI*\fP the idea \*(+Kfor\*(-K this program was a product of the imagination of
\fI*\fP Kurt Schoens\&. Not responsible \*(+Kfor\*(-K minds lost or stolen\&.
\fI*\fP\fI\h'\w' 'u-\w'/'u'/\fP\c
'-C
\*(+K#\*(-K \*(+Kdefine\*(-K\h'|21n'NCOLS\h'|31n'80
\*(+K#\*(-K \*(+Kdefine\*(-K\h'|21n'NLINES\h'|31n'24
\*(+K#\*(-K \*(+Kdefine\*(-K\h'|21n'MAXPATTERNS\h'|41n'4
\*(+Kstruct\*(-K locs \*(+K{\*(-K
\h'|11n'\*(+Kchar\*(-K\h'|21n'y, x;
\*(+K}\*(-K;
\*(+Ktypedef\*(-K \*(+Kstruct\*(-K locs\h'|31n'LOCS;
LOCS\h'|11n'Layout[NCOLS \fI*\fP NLINES];\h'|41n'\fI\h'\w' 'u-\w'/'u'/\fP\fI*\fP\c\c
'+C
current board layout \fI*\fP\fI\h'\w' 'u-\w'/'u'/\fP\c
'-C
\*(+Kint\*(-K\h'|11n'Pattern,\h'|41n'\fI\h'\w' 'u-\w'/'u'/\fP\fI*\fP\c\c
'+C
current pattern number \fI*\fP\fI\h'\w' 'u-\w'/'u'/\fP\c
'-C
\h'|11n'Numstars;\h'|41n'\fI\h'\w' 'u-\w'/'u'/\fP\fI*\fP\c\c
'+C
number of stars in pattern \fI*\fP\fI\h'\w' 'u-\w'/'u'/\fP\c
'-C
'FN main
main() \*(+K{\*(-K
\h'|11n'\*(+Kchar\*(-K\h'|31n'\fI*\fPgetenv();
\h'|11n'\*(+Kint\*(-K\h'|31n'die();
\h'|11n'srand(getpid());\h'|51n'\fI\h'\w' 'u-\w'/'u'/\fP\fI*\fP\c\c
'+C
initialize random sequence \fI*\fP\fI\h'\w' 'u-\w'/'u'/\fP\c
'-C
\h'|11n'initscr();
\h'|11n'signal(SIGINT, die);
\h'|11n'noecho();
\h'|11n'nonl();
\h'|11n'leaveok(stdscr, TRUE);
\h'|11n'scrollok(stdscr, FALSE);
\h'|11n'\*(+Kfor\*(-K (;;) \*(+K{\*(-K
\h'|21n'makeboard();\h'|51n'\fI\h'\w' 'u-\w'/'u'/\fP\fI*\fP\c\c
'+C
make the board setup \fI*\fP\fI\h'\w' 'u-\w'/'u'/\fP\c
'-C
\h'|21n'puton(\'\fI*\fP\');\h'|51n'\fI\h'\w' 'u-\w'/'u'/\fP\fI*\fP\c\c
'+C
put on \'\fI*\fP\'s \fI*\fP\fI\h'\w' 'u-\w'/'u'/\fP\c
'-C
\h'|21n'puton(\' \');\h'|51n'\fI\h'\w' 'u-\w'/'u'/\fP\fI*\fP\c\c
'+C
cover up with \' \'s \fI*\fP\fI\h'\w' 'u-\w'/'u'/\fP\c
'-C
\h'|11n'\*(+K}\*(-K
\*(+K}\*(-K\c\c
'-F
\fI\h'\w' 'u-\w'/'u'/\fP\fI*\fP\c\c
'+C
\fI*\fP On program exit, move the cursor to the lower left corner by
\fI*\fP direct addressing, since current location is not guaranteed\&.
\fI*\fP We lie and say we used to be at the upper right corner to guarantee
\fI*\fP absolute addressing\&.
\fI*\fP\fI\h'\w' 'u-\w'/'u'/\fP\c
'-C
'FN die
die() \*(+K{\*(-K
\h'|11n'signal(SIGINT, SIG\*_IGN);
\h'|11n'mvcur(0, COLS\*-1, LINES\*-1, 0);
\h'|11n'endwin();
\h'|11n'exit(0);
\*(+K}\*(-K\c\c
'-F
\fI\h'\w' 'u-\w'/'u'/\fP\fI*\fP\c\c
'+C
\fI*\fP Make the current board setup\&. It picks a random pattern and
\fI*\fP calls ison() to determine \*(+Kif\*(-K the character is on that pattern
\fI*\fP or not\&.
\fI*\fP\fI\h'\w' 'u-\w'/'u'/\fP\c
'-C
'FN makeboard
makeboard() \*(+K{\*(-K
\h'|11n'reg \*(+Kint\*(-K\h'|31n'y, x;
\h'|11n'reg LOCS\h'|31n'\fI*\fPlp;
\h'|11n'Pattern = rand() % MAXPATTERNS;
\h'|11n'lp = Layout;
\h'|11n'\*(+Kfor\*(-K (y = 0; y < NLINES; y++)
\h'|21n'\*(+Kfor\*(-K (x = 0; x < NCOLS; x++)
\h'|31n'\*(+Kif\*(-K (ison(y, x)) \*(+K{\*(-K
\h'|41n'lp\*->y = y;
\h'|41n'lp++\*->x = x;
\h'|31n'\*(+K}\*(-K
\h'|11n'Numstars = lp \*- Layout;
\*(+K}\*(-K\c\c
'-F
\fI\h'\w' 'u-\w'/'u'/\fP\fI*\fP\c\c
'+C
\fI*\fP Return TRUE \*(+Kif\*(-K (y, x) is on the current pattern\&.
\fI*\fP\fI\h'\w' 'u-\w'/'u'/\fP\c
'-C
'FN ison
ison(y, x)
reg \*(+Kint\*(-K\h'|11n'y, x; \*(+K{\*(-K
\h'|11n'\*(+Kswitch\*(-K (Pattern) \*(+K{\*(-K
\h'|11n' \*(+Kcase\*(-K 0:\h'|31n'\fI\h'\w' 'u-\w'/'u'/\fP\fI*\fP\c\c
'+C
alternating lines \fI*\fP\fI\h'\w' 'u-\w'/'u'/\fP\c
'-C
\h'|21n'\*(+Kreturn\*(-K !(y & 01);
\h'|11n' \*(+Kcase\*(-K 1:\h'|31n'\fI\h'\w' 'u-\w'/'u'/\fP\fI*\fP\c\c
'+C
box \fI*\fP\fI\h'\w' 'u-\w'/'u'/\fP\c
'-C
\h'|21n'\*(+Kif\*(-K (x >= LINES && y >= NCOLS)
\h'|31n'\*(+Kreturn\*(-K FALSE;
\h'|21n'\*(+Kif\*(-K (y < 3 || y >= NLINES \*- 3)
\h'|31n'\*(+Kreturn\*(-K TRUE;
\h'|21n'\*(+Kreturn\*(-K (x < 3 || x >= NCOLS \*- 3);
\h'|11n' \*(+Kcase\*(-K 2:\h'|31n'\fI\h'\w' 'u-\w'/'u'/\fP\fI*\fP\c\c
'+C
holy pattern! \fI*\fP\fI\h'\w' 'u-\w'/'u'/\fP\c
'-C
\h'|21n'\*(+Kreturn\*(-K ((x + y) & 01);
\h'|11n' \*(+Kcase\*(-K 3:\h'|31n'\fI\h'\w' 'u-\w'/'u'/\fP\fI*\fP\c\c
'+C
bar across center \fI*\fP\fI\h'\w' 'u-\w'/'u'/\fP\c
'-C
\h'|21n'\*(+Kreturn\*(-K (y >= 9 && y <= 15);
\h'|11n'\*(+K}\*(-K
\h'|11n'\fI\h'\w' 'u-\w'/'u'/\fP\fI*\fP\c\c
'+C
NOTREACHED \fI*\fP\fI\h'\w' 'u-\w'/'u'/\fP\c
'-C
\*(+K}\*(-K\c\c
'-F
'FN puton
puton(ch)
reg \*(+Kchar\*(-K\h'|21n'ch; \*(+K{\*(-K
\h'|11n'reg LOCS\h'|31n'\fI*\fPlp;
\h'|11n'reg \*(+Kint\*(-K\h'|31n'r;
\h'|11n'reg LOCS\h'|31n'\fI*\fPend;
\h'|11n'LOCS\h'|31n'temp;
\h'|11n'end = &Layout[Numstars];
\h'|11n'\*(+Kfor\*(-K (lp = Layout; lp < end; lp++) \*(+K{\*(-K
\h'|21n'r = rand() % Numstars;
\h'|21n'temp = \fI*\fPlp;
\h'|21n'\fI*\fPlp = Layout[r];
\h'|21n'Layout[r] = temp;
\h'|11n'\*(+K}\*(-K
\h'|11n'\*(+Kfor\*(-K (lp = Layout; lp < end; lp++) \*(+K{\*(-K
\h'|21n'mvaddch(lp\*->y, lp\*->x, ch);
\h'|21n'refresh();
\h'|11n'\*(+K}\*(-K
\*(+K}\*(-K\c\c
'-F