X-Git-Url: https://git.cameronkatri.com/bsdgames-darwin.git/blobdiff_plain/88eff08098072c4b923af3a7180b14d6dfcf66ef..a9bc398e7d2831e9f265619e36ef03fe1320dce0:/worm/worm.c diff --git a/worm/worm.c b/worm/worm.c index a78f27fb..a6784083 100644 --- a/worm/worm.c +++ b/worm/worm.c @@ -1,4 +1,4 @@ -/* $NetBSD: worm.c,v 1.19 2000/05/08 07:56:06 mycroft Exp $ */ +/* $NetBSD: worm.c,v 1.23 2001/12/06 12:24:00 blymn Exp $ */ /* * Copyright (c) 1980, 1993 @@ -43,7 +43,7 @@ __COPYRIGHT("@(#) Copyright (c) 1980, 1993\n\ #if 0 static char sccsid[] = "@(#)worm.c 8.1 (Berkeley) 5/31/93"; #else -__RCSID("$NetBSD: worm.c,v 1.19 2000/05/08 07:56:06 mycroft Exp $"); +__RCSID("$NetBSD: worm.c,v 1.23 2001/12/06 12:24:00 blymn Exp $"); #endif #endif /* not lint */ @@ -80,6 +80,7 @@ int running = 0; int slow = 0; int score = 0; int start_len = LENGTH; +int visible_len; int lastch; char outbuf[BUFSIZ]; @@ -104,23 +105,32 @@ main(argc, argv) /* Revoke setgid privileges */ setgid(getgid()); - if (argc == 2) - start_len = atoi(argv[1]); - if ((start_len <= 0) || (start_len > 500)) - start_len = LENGTH; setbuf(stdout, outbuf); srand(getpid()); signal(SIGALRM, wake); signal(SIGINT, leave); signal(SIGQUIT, leave); initscr(); - crmode(); + cbreak(); noecho(); #ifdef KEY_LEFT keypad(stdscr, TRUE); #endif slow = (baudrate() <= 1200); clear(); + if (COLS < 18 || LINES < 5) { + /* + * Insufficient room for the line with " Worm" and the + * score if fewer than 18 columns; insufficient room for + * anything much if fewer than 5 lines. + */ + endwin(); + errx(1, "screen too small"); + } + if (argc == 2) + start_len = atoi(argv[1]); + if ((start_len <= 0) || (start_len > ((LINES-3) * (COLS-2)) / 3)) + start_len = LENGTH; stw = newwin(1, COLS-1, 0, 0); tv = newwin(LINES-1, COLS-1, 1, 0); box(tv, '*', '*'); @@ -152,14 +162,14 @@ void life() { struct body *bp, *np; - int i; + int i, j = 1; np = NULL; head = newlink(); if (head == NULL) err(1, NULL); - head->x = start_len+2; - head->y = 12; + head->x = start_len % (COLS-5) + 2; + head->y = LINES / 2; head->next = NULL; display(head, HEAD); for (i = 0, bp = head; i < start_len; i++, bp = np) { @@ -168,12 +178,19 @@ life() err(1, NULL); np->next = bp; bp->prev = np; - np->x = bp->x - 1; - np->y = bp->y; + if (((bp->x <= 2) && (j == 1)) || ((bp->x >= COLS-4) && (j == -1))) { + j *= -1; + np->x = bp->x; + np->y = bp->y + 1; + } else { + np->x = bp->x - j; + np->y = bp->y; + } display(np, BODY); } tail = np; tail->prev = NULL; + visible_len = start_len + 1; } void @@ -218,8 +235,15 @@ void newpos(bp) struct body * bp; { + if (visible_len == (LINES-3) * (COLS-3) - 1) { + endwin(); + + printf("\nYou won!\n"); + printf("Your final score was %d\n\n", score); + exit(0); + } do { - bp->y = rnd(LINES-3)+ 2; + bp->y = rnd(LINES-3)+ 1; bp->x = rnd(COLS-3) + 1; wmove(tv, bp->y, bp->x); } while(winch(tv) != ' '); @@ -295,6 +319,7 @@ process(ch) nh = tail->next; free(tail); tail = nh; + visible_len--; } else growing--; display(head, BODY); @@ -305,7 +330,7 @@ process(ch) prize(); score += growing; running = 0; - wmove(stw, 0, 68); + wmove(stw, 0, COLS - 12); wprintw(stw, "Score: %3d", score); wrefresh(stw); } @@ -320,6 +345,7 @@ process(ch) nh->x = x; display(nh, HEAD); head = nh; + visible_len++; if (!(slow && running)) { wmove(tv, head->y, head->x);