1 /* $NetBSD: worm.c,v 1.28 2008/08/08 16:10:47 drochner Exp $ */
4 * Copyright (c) 1980, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
34 __COPYRIGHT("@(#) Copyright (c) 1980, 1993\
35 The Regents of the University of California. All rights reserved.");
40 static char sccsid
[] = "@(#)worm.c 8.1 (Berkeley) 5/31/93";
42 __RCSID("$NetBSD: worm.c,v 1.28 2008/08/08 16:10:47 drochner Exp $");
47 * Worm. Written by Michael Toy
59 #define newlink() (struct body *) malloc(sizeof (struct body));
64 #define CNTRL(p) (p-'A'+1)
75 static struct body
*head
, *tail
, goody
;
76 static int growing
= 0;
77 static int running
= 0;
80 static int start_len
= LENGTH
;
81 static int visible_len
;
83 static char outbuf
[BUFSIZ
];
85 int main(int, char **);
86 static void crash(void) __dead
;
87 static void display(const struct body
*, char);
88 static void leave(int) __dead
;
89 static void life(void);
90 static void newpos(struct body
*);
91 static void process(int);
92 static void prize(void);
94 static void setup(void);
95 static void wake(int);
103 /* Revoke setgid privileges */
106 setbuf(stdout
, outbuf
);
108 signal(SIGALRM
, wake
);
109 signal(SIGINT
, leave
);
110 signal(SIGQUIT
, leave
);
112 errx(0, "couldn't initialize screen");
116 keypad(stdscr
, TRUE
);
118 slow
= (baudrate() <= 1200);
120 if (COLS
< 18 || LINES
< 5) {
122 * Insufficient room for the line with " Worm" and the
123 * score if fewer than 18 columns; insufficient room for
124 * anything much if fewer than 5 lines.
127 errx(1, "screen too small");
130 start_len
= atoi(argv
[1]);
131 if ((start_len
<= 0) || (start_len
> ((LINES
-3) * (COLS
-2)) / 3))
133 stw
= newwin(1, COLS
-1, 0, 0);
134 tv
= newwin(LINES
-1, COLS
-1, 1, 0);
137 scrollok(stw
, FALSE
);
139 wprintw(stw
, " Worm");
143 life(); /* Create the worm */
144 prize(); /* Put up a goal */
163 struct body
*bp
, *np
;
170 head
->x
= start_len
% (COLS
-5) + 2;
174 for (i
= 0, bp
= head
; i
< start_len
; i
++, bp
= np
) {
180 if (((bp
->x
<= 2) && (j
== 1)) || ((bp
->x
>= COLS
-4) && (j
== -1))) {
192 visible_len
= start_len
+ 1;
197 const struct body
*pos
;
200 wmove(tv
, pos
->y
, pos
->x
);
210 if (dummy
== 0){ /* called via crash() */
211 printf("\nWell, you ran into something and the game is over.\n");
212 printf("Your final score was %d\n\n", score
);
221 signal(SIGALRM
, wake
);
230 return abs((rand()>>5)+(rand()>>5)) % range
;
237 if (visible_len
== (LINES
-3) * (COLS
-3) - 1) {
240 printf("\nYou won!\n");
241 printf("Your final score was %d\n\n", score
);
245 bp
->y
= rnd(LINES
-3)+ 1;
246 bp
->x
= rnd(COLS
-3) + 1;
247 wmove(tv
, bp
->y
, bp
->x
);
248 } while(winch(tv
) != ' ');
258 waddch(tv
, value
+'0');
298 case 'H': x
--; running
= RUNLEN
; ch
= tolower(ch
); break;
299 case 'J': y
++; running
= RUNLEN
/2; ch
= tolower(ch
); break;
300 case 'K': y
--; running
= RUNLEN
/2; ch
= tolower(ch
); break;
301 case 'L': x
++; running
= RUNLEN
; ch
= tolower(ch
); break;
302 case '\f': setup(); return;
310 default: if (! running
) alarm(1);
317 tail
->next
->prev
= NULL
;
326 if (isdigit(ch
= winch(tv
)))
332 wmove(stw
, 0, COLS
- 12);
333 wprintw(stw
, "Score: %3d", score
);
336 else if(ch
!= ' ') crash();
348 if (!(slow
&& running
))
350 wmove(tv
, head
->y
, head
->x
);