1 /* $NetBSD: worm.c,v 1.31 2015/08/17 17:17:01 dholland 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.31 2015/08/17 17:17:01 dholland Exp $");
47 * Worm. Written by Michael Toy
63 #define CNTRL(p) (p-'A'+1)
74 static struct body
*head
, *tail
, goody
;
75 static int growing
= 0;
76 static int running
= 0;
79 static int start_len
= LENGTH
;
80 static int visible_len
;
82 static char outbuf
[BUFSIZ
];
84 int main(int, char **);
85 static void crash(void) __dead
;
86 static void display(const struct body
*, char);
87 static void leave(int) __dead
;
88 static void life(void);
89 static void newpos(struct body
*);
90 static void process(int);
91 static void prize(void);
93 static void setup(void);
94 static void wake(int);
101 b
= malloc(sizeof(*b
));
103 err(EXIT_FAILURE
, NULL
);
109 main(int argc
, char **argv
)
112 /* Revoke setgid privileges */
115 setbuf(stdout
, outbuf
);
117 signal(SIGALRM
, wake
);
118 signal(SIGINT
, leave
);
119 signal(SIGQUIT
, leave
);
121 errx(0, "couldn't initialize screen");
125 keypad(stdscr
, TRUE
);
127 slow
= (baudrate() <= 1200);
129 if (COLS
< 18 || LINES
< 5) {
131 * Insufficient room for the line with " Worm" and the
132 * score if fewer than 18 columns; insufficient room for
133 * anything much if fewer than 5 lines.
136 errx(1, "screen too small");
139 start_len
= atoi(argv
[1]);
140 if ((start_len
<= 0) || (start_len
> ((LINES
-3) * (COLS
-2)) / 3))
142 stw
= newwin(1, COLS
-1, 0, 0);
143 tv
= newwin(LINES
-1, COLS
-1, 1, 0);
146 scrollok(stw
, FALSE
);
148 wprintw(stw
, " Worm");
152 life(); /* Create the worm */
153 prize(); /* Put up a goal */
172 struct body
*bp
, *np
;
177 head
->x
= start_len
% (COLS
-5) + 2;
181 for (i
= 0, bp
= head
; i
< start_len
; i
++, bp
= np
) {
185 if (((bp
->x
<= 2) && (j
== 1)) || ((bp
->x
>= COLS
-4) && (j
== -1))) {
197 visible_len
= start_len
+ 1;
201 display(const struct body
*pos
, char chr
)
203 wmove(tv
, pos
->y
, pos
->x
);
212 if (dummy
== 0){ /* called via crash() */
213 printf("\nWell, you ran into something and the game is over.\n");
214 printf("Your final score was %d\n\n", score
);
222 signal(SIGALRM
, wake
);
230 return abs((rand()>>5)+(rand()>>5)) % range
;
234 newpos(struct body
*bp
)
236 if (visible_len
== (LINES
-3) * (COLS
-3) - 1) {
239 printf("\nYou won!\n");
240 printf("Your final score was %d\n\n", score
);
244 bp
->y
= rnd(LINES
-3)+ 1;
245 bp
->x
= rnd(COLS
-3) + 1;
246 wmove(tv
, bp
->y
, bp
->x
);
247 } while(winch(tv
) != ' ');
257 waddch(tv
, value
+'0');
296 case 'H': x
--; running
= RUNLEN
; ch
= tolower(ch
); break;
297 case 'J': y
++; running
= RUNLEN
/2; ch
= tolower(ch
); break;
298 case 'K': y
--; running
= RUNLEN
/2; ch
= tolower(ch
); break;
299 case 'L': x
++; running
= RUNLEN
; ch
= tolower(ch
); break;
300 case '\f': setup(); return;
308 default: if (! running
) alarm(1);
315 tail
->next
->prev
= NULL
;
324 if (isdigit(ch
= winch(tv
)))
330 wmove(stw
, 0, COLS
- 12);
331 wprintw(stw
, "Score: %3d", score
);
334 else if(ch
!= ' ') crash();
344 if (!(slow
&& running
))
346 wmove(tv
, head
->y
, head
->x
);