]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - worm/worm.c
1 /* $NetBSD: worm.c,v 1.26 2007/12/15 19:44:45 perry 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\n\
35 The Regents of the University of California. All rights reserved.\n");
40 static char sccsid
[] = "@(#)worm.c 8.1 (Berkeley) 5/31/93";
42 __RCSID("$NetBSD: worm.c,v 1.26 2007/12/15 19:44:45 perry Exp $");
47 * Worm. Written by Michael Toy
59 #define newlink() (struct body *) malloc(sizeof (struct body));
64 #define CNTRL(p) (p-'A'+1)
73 } *head
, *tail
, goody
;
78 int start_len
= LENGTH
;
83 void crash(void) __dead
;
84 void display(const struct body
*, char);
85 int main(int, char **);
86 void leave(int) __dead
;
88 void newpos(struct body
*);
101 /* Revoke setgid privileges */
104 setbuf(stdout
, outbuf
);
106 signal(SIGALRM
, wake
);
107 signal(SIGINT
, leave
);
108 signal(SIGQUIT
, leave
);
113 keypad(stdscr
, TRUE
);
115 slow
= (baudrate() <= 1200);
117 if (COLS
< 18 || LINES
< 5) {
119 * Insufficient room for the line with " Worm" and the
120 * score if fewer than 18 columns; insufficient room for
121 * anything much if fewer than 5 lines.
124 errx(1, "screen too small");
127 start_len
= atoi(argv
[1]);
128 if ((start_len
<= 0) || (start_len
> ((LINES
-3) * (COLS
-2)) / 3))
130 stw
= newwin(1, COLS
-1, 0, 0);
131 tv
= newwin(LINES
-1, COLS
-1, 1, 0);
134 scrollok(stw
, FALSE
);
136 wprintw(stw
, " Worm");
140 life(); /* Create the worm */
141 prize(); /* Put up a goal */
160 struct body
*bp
, *np
;
167 head
->x
= start_len
% (COLS
-5) + 2;
171 for (i
= 0, bp
= head
; i
< start_len
; i
++, bp
= np
) {
177 if (((bp
->x
<= 2) && (j
== 1)) || ((bp
->x
>= COLS
-4) && (j
== -1))) {
189 visible_len
= start_len
+ 1;
194 const struct body
*pos
;
197 wmove(tv
, pos
->y
, pos
->x
);
207 if (dummy
== 0){ /* called via crash() */
208 printf("\nWell, you ran into something and the game is over.\n");
209 printf("Your final score was %d\n\n", score
);
218 signal(SIGALRM
, wake
);
227 return abs((rand()>>5)+(rand()>>5)) % range
;
234 if (visible_len
== (LINES
-3) * (COLS
-3) - 1) {
237 printf("\nYou won!\n");
238 printf("Your final score was %d\n\n", score
);
242 bp
->y
= rnd(LINES
-3)+ 1;
243 bp
->x
= rnd(COLS
-3) + 1;
244 wmove(tv
, bp
->y
, bp
->x
);
245 } while(winch(tv
) != ' ');
255 waddch(tv
, value
+'0');
295 case 'H': x
--; running
= RUNLEN
; ch
= tolower(ch
); break;
296 case 'J': y
++; running
= RUNLEN
/2; ch
= tolower(ch
); break;
297 case 'K': y
--; running
= RUNLEN
/2; ch
= tolower(ch
); break;
298 case 'L': x
++; running
= RUNLEN
; ch
= tolower(ch
); break;
299 case '\f': setup(); return;
307 default: if (! running
) alarm(1);
314 tail
->next
->prev
= NULL
;
323 if (isdigit(ch
= winch(tv
)))
329 wmove(stw
, 0, COLS
- 12);
330 wprintw(stw
, "Score: %3d", score
);
333 else if(ch
!= ' ') crash();
345 if (!(slow
&& running
))
347 wmove(tv
, head
->y
, head
->x
);