+/* $NetBSD: worm.c,v 1.8 1997/10/12 02:12:48 lukem Exp $ */
+
/*
- * Copyright (c) 1980 Regents of the University of California.
- * All rights reserved.
+ * Copyright (c) 1980, 1993
+ * The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* SUCH DAMAGE.
*/
+#include <sys/cdefs.h>
#ifndef lint
-char copyright[] =
-"@(#) Copyright (c) 1980 Regents of the University of California.\n\
- All rights reserved.\n";
+__COPYRIGHT("@(#) Copyright (c) 1980, 1993\n\
+ The Regents of the University of California. All rights reserved.\n");
#endif /* not lint */
#ifndef lint
-/*static char sccsid[] = "from: @(#)worm.c 5.8 (Berkeley) 2/28/91";*/
-static char rcsid[] = "$Id: worm.c,v 1.3 1993/08/10 02:05:36 mycroft Exp $";
+#if 0
+static char sccsid[] = "@(#)worm.c 8.1 (Berkeley) 5/31/93";
+#else
+__RCSID("$NetBSD: worm.c,v 1.8 1997/10/12 02:12:48 lukem Exp $");
+#endif
#endif /* not lint */
/*
#include <ctype.h>
#include <curses.h>
#include <signal.h>
+#include <stdlib.h>
+#include <termios.h>
+#include <unistd.h>
#define newlink() (struct body *) malloc(sizeof (struct body));
#define HEAD '@'
#define BODY 'o'
#define LENGTH 7
#define RUNLEN 8
-#define when break;case
-#define otherwise break;default
#define CNTRL(p) (p-'A'+1)
-#ifndef baudrate
-# define baudrate() _tty.sg_ospeed
-#endif
WINDOW *tv;
WINDOW *stw;
char lastch;
char outbuf[BUFSIZ];
-void leave(), wake(), suspend();
+void crash __P((void));
+void display __P((struct body *, char));
+int main __P((int, char **));
+void leave __P((int));
+void life __P((void));
+void newpos __P((struct body *));
+void process __P((char));
+void prize __P((void));
+int rnd __P((int));
+void setup __P((void));
+void suspend __P((int));
+void wake __P((int));
+int
main(argc, argv)
int argc;
char **argv;
}
}
+void
life()
{
- register struct body *bp, *np;
- register int i;
+ struct body *bp, *np;
+ int i;
+ np = NULL;
head = newlink();
head->x = start_len+2;
head->y = 12;
tail->prev = NULL;
}
+void
display(pos, chr)
-struct body *pos;
-char chr;
+ struct body *pos;
+ char chr;
{
wmove(tv, pos->y, pos->x);
waddch(tv, chr);
}
void
-leave()
+leave(dummy)
+ int dummy;
{
endwin();
exit(0);
}
void
-wake()
+wake(dummy)
+ int dummy;
{
signal(SIGALRM, wake);
fflush(stdout);
process(lastch);
}
+int
rnd(range)
+ int range;
{
return abs((rand()>>5)+(rand()>>5)) % range;
}
+void
newpos(bp)
-struct body * bp;
+ struct body * bp;
{
do {
bp->y = rnd(LINES-3)+ 2;
} while(winch(tv) != ' ');
}
+void
prize()
{
int value;
wrefresh(tv);
}
+void
process(ch)
-char ch;
+ char ch;
{
- register int x,y;
+ int x,y;
struct body *nh;
alarm(0);
y = head->y;
switch(ch)
{
- when 'h': x--;
- when 'j': y++;
- when 'k': y--;
- when 'l': x++;
- when 'H': x--; running = RUNLEN; ch = tolower(ch);
- when 'J': y++; running = RUNLEN/2; ch = tolower(ch);
- when 'K': y--; running = RUNLEN/2; ch = tolower(ch);
- when 'L': x++; running = RUNLEN; ch = tolower(ch);
- when '\f': setup(); return;
- when CNTRL('Z'): suspend(); return;
- when CNTRL('C'): crash(); return;
- when CNTRL('D'): crash(); return;
- otherwise: if (! running) alarm(1);
+ case 'h': x--; break;
+ case 'j': y++; break;
+ case 'k': y--; break;
+ case 'l': x++; break;
+ case 'H': x--; running = RUNLEN; ch = tolower(ch); break;
+ case 'J': y++; running = RUNLEN/2; ch = tolower(ch); break;
+ case 'K': y--; running = RUNLEN/2; ch = tolower(ch); break;
+ case 'L': x++; running = RUNLEN; ch = tolower(ch); break;
+ case '\f': setup(); return;
+ case CNTRL('Z'): suspend(0); return;
+ case CNTRL('C'): crash(); return;
+ case CNTRL('D'): crash(); return;
+ default: if (! running) alarm(1);
return;
}
lastch = ch;
alarm(1);
}
+void
crash()
{
sleep(2);
refresh();
printf("Well, you ran into something and the game is over.\n");
printf("Your final score was %d\n", score);
- leave();
+ leave(0);
}
void
-suspend()
+suspend(dummy)
+ int dummy;
{
- char *sh;
-
move(LINES-1, 0);
refresh();
endwin();
setup();
}
+void
setup()
{
clear();