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