document distributed with 4.2BSD
[unix-history] / usr / src / lib / libcurses / PSD.doc / twinkle1.c
CommitLineData
99cf7158
KM
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
6b087f63 8static char sccsid[] = "@(#)twinkle1.c 5.1 (Berkeley) %G%";
99cf7158
KM
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
23struct locs {
24 char y, x;
25};
26
27typedef struct locs LOCS;
28
29LOCS Layout[NCOLS * NLINES]; /* current board layout */
30
31int Pattern, /* current pattern number */
32 Numstars; /* number of stars in pattern */
33
34main() {
35
36 char *getenv();
37 int die();
38
39 srand(getpid()); /* initialize random sequence */
40
41 initscr();
42 signal(SIGINT, die);
43 noecho();
44 nonl();
45 leaveok(stdscr, TRUE);
46 scrollok(stdscr, FALSE);
47
48 for (;;) {
49 makeboard(); /* make the board setup */
50 puton('*'); /* put on '*'s */
51 puton(' '); /* cover up with ' 's */
52 }
53}
54
55/*
56 * On program exit, move the cursor to the lower left corner by
57 * direct addressing, since current location is not guaranteed.
58 * We lie and say we used to be at the upper right corner to guarantee
59 * absolute addressing.
60 */
61die() {
62
63 signal(SIGINT, SIG_IGN);
64 mvcur(0, COLS-1, LINES-1, 0);
65 endwin();
66 exit(0);
67}
68
69
70/*
71 * Make the current board setup. It picks a random pattern and
72 * calls ison() to determine if the character is on that pattern
73 * or not.
74 */
75makeboard() {
76
77 reg int y, x;
78 reg LOCS *lp;
79
80 Pattern = rand() % MAXPATTERNS;
81 lp = Layout;
82 for (y = 0; y < NLINES; y++)
83 for (x = 0; x < NCOLS; x++)
84 if (ison(y, x)) {
85 lp->y = y;
86 lp++->x = x;
87 }
88 Numstars = lp - Layout;
89}
90
91/*
92 * Return TRUE if (y, x) is on the current pattern.
93 */
94ison(y, x)
95reg 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
114puton(ch)
115reg 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}