twinkle1.c revision 1.4 1 1.4 perry .\" $NetBSD: twinkle1.c,v 1.4 1998/01/09 04:12:16 perry Exp $
2 1.4 perry .\"
3 1.1 cgd .\" Copyright (c) 1980, 1993
4 1.1 cgd .\" The Regents of the University of California. All rights reserved.
5 1.1 cgd .\"
6 1.1 cgd .\" Redistribution and use in source and binary forms, with or without
7 1.1 cgd .\" modification, are permitted provided that the following conditions
8 1.1 cgd .\" are met:
9 1.1 cgd .\" 1. Redistributions of source code must retain the above copyright
10 1.1 cgd .\" notice, this list of conditions and the following disclaimer.
11 1.1 cgd .\" 2. Redistributions in binary form must reproduce the above copyright
12 1.1 cgd .\" notice, this list of conditions and the following disclaimer in the
13 1.1 cgd .\" documentation and/or other materials provided with the distribution.
14 1.1 cgd .\" 3. All advertising materials mentioning features or use of this software
15 1.1 cgd .\" must display the following acknowledgement:
16 1.1 cgd .\" This product includes software developed by the University of
17 1.1 cgd .\" California, Berkeley and its contributors.
18 1.1 cgd .\" 4. Neither the name of the University nor the names of its contributors
19 1.1 cgd .\" may be used to endorse or promote products derived from this software
20 1.1 cgd .\" without specific prior written permission.
21 1.1 cgd .\"
22 1.1 cgd .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.1 cgd .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.1 cgd .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.1 cgd .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.1 cgd .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.1 cgd .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.1 cgd .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.1 cgd .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.1 cgd .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.1 cgd .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.1 cgd .\" SUCH DAMAGE.
33 1.1 cgd .\"
34 1.3 cgd .\" @(#)twinkle1.c 8.1 (Berkeley) 6/8/93
35 1.1 cgd .\"
36 1.1 cgd # include <curses.h>
37 1.1 cgd # include <signal.h>
38 1.1 cgd
39 1.1 cgd /*
40 1.1 cgd * the idea for this program was a product of the imagination of
41 1.1 cgd * Kurt Schoens. Not responsible for minds lost or stolen.
42 1.1 cgd */
43 1.1 cgd
44 1.1 cgd # define NCOLS 80
45 1.1 cgd # define NLINES 24
46 1.1 cgd # define MAXPATTERNS 4
47 1.1 cgd
48 1.1 cgd typedef struct {
49 1.1 cgd int y, x;
50 1.1 cgd } LOCS;
51 1.1 cgd
52 1.1 cgd LOCS Layout[NCOLS * NLINES]; /* current board layout */
53 1.1 cgd
54 1.1 cgd int Pattern, /* current pattern number */
55 1.1 cgd Numstars; /* number of stars in pattern */
56 1.1 cgd
57 1.1 cgd char *getenv();
58 1.1 cgd
59 1.1 cgd int die();
60 1.1 cgd
61 1.1 cgd main()
62 1.1 cgd {
63 1.1 cgd srand(getpid()); /* initialize random sequence */
64 1.1 cgd
65 1.1 cgd initscr();
66 1.1 cgd signal(SIGINT, die);
67 1.1 cgd noecho();
68 1.1 cgd nonl();
69 1.1 cgd leaveok(stdscr, TRUE);
70 1.1 cgd scrollok(stdscr, FALSE);
71 1.1 cgd
72 1.1 cgd for (;;) {
73 1.1 cgd makeboard(); /* make the board setup */
74 1.1 cgd puton('*'); /* put on '*'s */
75 1.1 cgd puton(' '); /* cover up with ' 's */
76 1.1 cgd }
77 1.1 cgd }
78 1.1 cgd
79 1.1 cgd /*
80 1.1 cgd * On program exit, move the cursor to the lower left corner by
81 1.1 cgd * direct addressing, since current location is not guaranteed.
82 1.1 cgd * We lie and say we used to be at the upper right corner to guarantee
83 1.1 cgd * absolute addressing.
84 1.1 cgd */
85 1.1 cgd die()
86 1.1 cgd {
87 1.1 cgd signal(SIGINT, SIG_IGN);
88 1.1 cgd mvcur(0, COLS - 1, LINES - 1, 0);
89 1.1 cgd endwin();
90 1.1 cgd exit(0);
91 1.1 cgd }
92 1.1 cgd
93 1.1 cgd
94 1.1 cgd /*
95 1.1 cgd * Make the current board setup. It picks a random pattern and
96 1.1 cgd * calls ison() to determine if the character is on that pattern
97 1.1 cgd * or not.
98 1.1 cgd */
99 1.1 cgd makeboard()
100 1.1 cgd {
101 1.1 cgd reg int y, x;
102 1.1 cgd reg LOCS *lp;
103 1.1 cgd
104 1.1 cgd Pattern = rand() % MAXPATTERNS;
105 1.1 cgd lp = Layout;
106 1.1 cgd for (y = 0; y < NLINES; y++)
107 1.1 cgd for (x = 0; x < NCOLS; x++)
108 1.1 cgd if (ison(y, x)) {
109 1.1 cgd lp->y = y;
110 1.1 cgd lp->x = x;
111 1.1 cgd lp++;
112 1.1 cgd }
113 1.1 cgd Numstars = lp - Layout;
114 1.1 cgd }
115 1.1 cgd
116 1.1 cgd /*
117 1.1 cgd * Return TRUE if (y, x) is on the current pattern.
118 1.1 cgd */
119 1.1 cgd ison(y, x)
120 1.1 cgd reg int y, x; {
121 1.1 cgd
122 1.1 cgd switch (Pattern) {
123 1.1 cgd case 0: /* alternating lines */
124 1.1 cgd return !(y & 01);
125 1.1 cgd case 1: /* box */
126 1.1 cgd if (x >= LINES && y >= NCOLS)
127 1.1 cgd return FALSE;
128 1.1 cgd if (y < 3 || y >= NLINES - 3)
129 1.1 cgd return TRUE;
130 1.1 cgd return (x < 3 || x >= NCOLS - 3);
131 1.1 cgd case 2: /* holy pattern! */
132 1.1 cgd return ((x + y) & 01);
133 1.1 cgd case 3: /* bar across center */
134 1.1 cgd return (y >= 9 && y <= 15);
135 1.1 cgd }
136 1.1 cgd /* NOTREACHED */
137 1.1 cgd }
138 1.1 cgd
139 1.1 cgd puton(ch)
140 1.1 cgd reg char ch;
141 1.1 cgd {
142 1.1 cgd reg LOCS *lp;
143 1.1 cgd reg int r;
144 1.1 cgd reg LOCS *end;
145 1.1 cgd LOCS temp;
146 1.1 cgd
147 1.1 cgd end = &Layout[Numstars];
148 1.1 cgd for (lp = Layout; lp < end; lp++) {
149 1.1 cgd r = rand() % Numstars;
150 1.1 cgd temp = *lp;
151 1.1 cgd *lp = Layout[r];
152 1.1 cgd Layout[r] = temp;
153 1.1 cgd }
154 1.1 cgd
155 1.1 cgd for (lp = Layout; lp < end; lp++) {
156 1.1 cgd mvaddch(lp->y, lp->x, ch);
157 1.1 cgd refresh();
158 1.1 cgd }
159 1.1 cgd }
160