From 0f353936c4565fbbea15240b34a5daf0882e58f1 Mon Sep 17 00:00:00 2001 From: "Eric S. Raymond" Date: Mon, 15 Nov 1993 18:01:23 +0000 Subject: [PATCH] Froze 2.0 release. --- Makefile | 6 ++-- README | 9 ++++-- bs.c | 93 +++++++++++++++++++++++++++++++++++++++++++------------- bs.xml | 11 ++++--- 4 files changed, 90 insertions(+), 29 deletions(-) diff --git a/Makefile b/Makefile index 071dd06..bfddf2c 100644 --- a/Makefile +++ b/Makefile @@ -1,15 +1,17 @@ -# Makefile for Battleships +# Makefile for bs, the Battleships game # Flags for use with the Linux ncurses package (recommended) # CFLAGS = -DNDEBUG -I/usr/local/include -L/usr/local/lib # TERMLIB = -lncurses +# CC = gcc # Flags for use with stock curses CFLAGS = -DNDEBUG TERMLIB = -lcurses +CC = gcc bs: bs.c - cc $(CFLAGS) -o bs bs.c $(TERMLIB) + $(CC) $(CFLAGS) -o bs bs.c $(TERMLIB) lint: lint bs.c -lcurses diff --git a/README b/README index a77286e..fb992d7 100644 --- a/README +++ b/README @@ -1,3 +1,5 @@ + BS 2.0 + Battleships is an intrinsically silly game, but I couldn't resist fixing this sucker. It now has a purely visual interface (you place ships and call for shots by moving the cursor around the board using the standard yuhjklbn keys). @@ -12,7 +14,8 @@ it back, compile with -DPENGUIN. One strike against featureitis... The code now lints as clean as the broken SysV curses lint library will let it. Some #ifdefs in the code should result in the right things being done for -BSD or USG systems. They key off A_UNDERLINE. +BSD or USG systems. They key off A_UNDERLINE. If you're using the Linux +ncurses library, tell the makefile. This was probably a waste of a day or so. But what the hack -- rewriting the strategy robot as an FSM was fun, and maybe the interface will set a good @@ -28,6 +31,8 @@ stuffed in suits and condemned to write COBOL for the rest of their days... O.K., I feel better now that I've got that off my chest... +November 1993: I've added function key support, and ANSI/POSIXized the code. + Eric S. Raymond - esr@snark.uu.net + esr@snark.thyrsus.com diff --git a/bs.c b/bs.c index 8e2a8b7..bca0b95 100644 --- a/bs.c +++ b/bs.c @@ -4,9 +4,12 @@ * with improved user interface, autoconfiguration and code cleanup * by Eric S. Raymond * v1.2 with color support and minor portability fixes, November 1990 + * v2.0 featuring strict ANSI/POSIX conformance, November 1993. */ +#define _POSIX_SOURCE #include +#include #include #include #include @@ -40,6 +43,8 @@ extern char *strchr(), *strcpy(); extern long time(); extern void exit(); +static bool checkplace(); + /* * Constants for tuning the random-fire algorithm. It prefers moves that * diagonal-stripe the board with a stripe separation of srchstep. If @@ -102,7 +107,7 @@ static char destroy[] = "Destroyer"; static char ptboat[] = "PT Boat"; static char name[40]; -static char dftname[] = "Stranger"; +static char dftname[] = "stranger"; /* direction constants */ #define E 0 @@ -210,12 +215,17 @@ static void intro() (void)signal(SIGQUIT,uninitgame); if(tmpname = getlogin()) + { (void)strcpy(name,tmpname); + name[0] = toupper(name[0]); + } else (void)strcpy(name,dftname); - name[0] = toupper(name[0]); (void)initscr(); +#ifdef KEY_MIN + keypad(stdscr, TRUE); +#endif /* KEY_MIN */ (void)saveterm(); (void)nonl(); (void)cbreak(); @@ -301,6 +311,12 @@ int vis; ss->hits = 0; } +static int rnd(n) +int n; +{ + return(((rand() & 0x7FFF) % n)); +} + static void randomplace(b, ss) /* generate a valid random ship placement into px,py */ int b; @@ -408,6 +424,7 @@ static void initgame() ss = (ship_t *)NULL; do { extern char *strchr(); + static char getcoord(); char c, docked[SHIPTYPES + 2], *cp = docked; /* figure which ships still wait to be placed */ @@ -512,12 +529,6 @@ static void initgame() (void) getch(); } -static int rnd(n) -int n; -{ - return(((rand() & 0x7FFF) % n)); -} - static int getcoord(atcpu) int atcpu; { @@ -543,14 +554,54 @@ int atcpu; switch(c = getch()) { - case 'k': case '8': ny = cury+BDEPTH-1; nx = curx; break; - case 'j': case '2': ny = cury+1; nx = curx; break; - case 'h': case '4': ny = cury; nx = curx+BWIDTH-1; break; - case 'l': case '6': ny = cury; nx = curx+1; break; - case 'y': case '7': ny = cury+BDEPTH-1; nx = curx+BWIDTH-1; break; - case 'b': case '1': ny = cury+1; nx = curx+BWIDTH-1; break; - case 'u': case '9': ny = cury+BDEPTH-1; nx = curx+1; break; - case 'n': case '3': ny = cury+1; nx = curx+1; break; + case 'k': case '8': +#ifdef KEY_MIN + case KEY_UP: +#endif /* KEY_MIN */ + ny = cury+BDEPTH-1; nx = curx; + break; + case 'j': case '2': +#ifdef KEY_MIN + case KEY_DOWN: +#endif /* KEY_MIN */ + ny = cury+1; nx = curx; + break; + case 'h': case '4': +#ifdef KEY_MIN + case KEY_LEFT: +#endif /* KEY_MIN */ + ny = cury; nx = curx+BWIDTH-1; + break; + case 'l': case '6': +#ifdef KEY_MIN + case KEY_RIGHT: +#endif /* KEY_MIN */ + ny = cury; nx = curx+1; + break; + case 'y': case '7': +#ifdef KEY_MIN + case KEY_A1: +#endif /* KEY_MIN */ + ny = cury+BDEPTH-1; nx = curx+BWIDTH-1; + break; + case 'b': case '1': +#ifdef KEY_MIN + case KEY_C1: +#endif /* KEY_MIN */ + ny = cury+1; nx = curx+BWIDTH-1; + break; + case 'u': case '9': +#ifdef KEY_MIN + case KEY_A3: +#endif /* KEY_MIN */ + ny = cury+BDEPTH-1; nx = curx+1; + break; + case 'n': case '3': +#ifdef KEY_MIN + case KEY_C3: +#endif /* KEY_MIN */ + ny = cury+1; nx = curx+1; + break; case FF: nx = curx; ny = cury; (void)clearok(stdscr, TRUE); @@ -598,7 +649,7 @@ int y, x; return(collide); } -static int checkplace(b, ss, vis) +static bool checkplace(b, ss, vis) int b; ship_t *ss; int vis; @@ -610,7 +661,7 @@ int vis; yend = ss->y + ss->length * yincr[ss->dir]; if (!ONBOARD(xend, yend)) { - if(vis) + if (vis) switch(rnd(3)) { case 0: @@ -637,16 +688,16 @@ int vis; error("There's already a ship there"); break; case 1: - error("Collision alert! Aaaaaagh!"); + error("Collision alert! Aaaaaagh!"); break; case 2: error("Er, Admiral, what about the other ship?"); break; } - return(0); + return(FALSE); } } - return(1); + return(TRUE); } static int awinna() diff --git a/bs.xml b/bs.xml index 38cfe82..00aa063 100644 --- a/bs.xml +++ b/bs.xml @@ -1,4 +1,4 @@ -.TH BATTLESHIPS 6 "Aug 23, 1989" +.TH BATTLESHIPS 6 "Nov 15 1993" .SH NAME bs \- battleships game .SH SYNOPSIS @@ -7,7 +7,8 @@ battle [ -b | -s ] [ -c ] This program allows you to play the familiar Battleships game against the computer on a 10x10 board. The interface is visual and largely self-explanatory; you place your ships and pick your shots by moving the -cursor around the `sea' with the rogue/hack motion keys hjklyubn. +cursor around the `sea' with the rogue/hack motion keys hjklyubn. If +your UNIX has a modern (non-BSD) curses, your arrow keys will also work. .PP Note that when selecting a ship to place, you must type the capital letter (these are, after all, capital ships). During ship placement, the `r' command @@ -35,8 +36,10 @@ Normally, ships must be separated by at least one square of open water. The -c option disables this check and allows them to close-pack. .PP The algorithm the computer uses once it has found a ship to sink is provably -optimal. The dispersion criterion for the random-fire algorithm may not be. +optimal. The dispersion criterion for the random-fire algorithm may not be. .SH AUTHORS Originally written by one Bruce Holloway in 1986. Salvo mode added by Chuck A. DeGaul (cbosgd!cad). Visual user interface, `closepack' option, code rewrite -and manual page by Eric S. Raymond August 1989. +and manual page by Eric S. Raymond August +1989. Keypad support and ANSI/POSIX conformance, November '93. + -- 2.43.0