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