From af857c197b393612bf159ea798009c9b1bf5200a Mon Sep 17 00:00:00 2001 From: lukem Date: Fri, 10 Oct 1997 08:59:32 +0000 Subject: KNF & WARNSify, and only compile common_source once/* (compiles 25% faster) --- backgammon/backgammon/Makefile | 11 +- backgammon/backgammon/backgammon.6 | 6 +- backgammon/backgammon/backlocal.h | 48 +++ backgammon/backgammon/extra.c | 213 +++++++------- backgammon/backgammon/main.c | 579 +++++++++++++++++-------------------- backgammon/backgammon/move.c | 541 +++++++++++++++++----------------- backgammon/backgammon/text.c | 167 +++++------ backgammon/backgammon/version.c | 9 +- 8 files changed, 798 insertions(+), 776 deletions(-) create mode 100644 backgammon/backgammon/backlocal.h (limited to 'backgammon/backgammon') diff --git a/backgammon/backgammon/Makefile b/backgammon/backgammon/Makefile index ca311339..2a8d29b8 100644 --- a/backgammon/backgammon/Makefile +++ b/backgammon/backgammon/Makefile @@ -1,15 +1,8 @@ -# $NetBSD: Makefile,v 1.8 1997/05/08 21:11:05 gwr Exp $ +# $NetBSD: Makefile,v 1.9 1997/10/10 08:59:34 lukem Exp $ # @(#)Makefile 8.1 (Berkeley) 5/31/93 PROG= backgammon -CFLAGS+=-DV7 -I${.CURDIR}/../common_source -SRCS= allow.c board.c check.c extra.c fancy.c init.c main.c move.c \ - odds.c one.c save.c subs.c table.c text.c version.c +SRCS= extra.c main.c move.c text.c version.c MAN= backgammon.6 -DPADD= ${LIBTERM} ${LIBCOMPAT} -LDADD= -ltermcap -lcompat -.PATH: ${.CURDIR}/../common_source -HIDEGAME=hidegame -.include "../../Makefile.inc" .include diff --git a/backgammon/backgammon/backgammon.6 b/backgammon/backgammon/backgammon.6 index d11afac4..11b268c1 100644 --- a/backgammon/backgammon/backgammon.6 +++ b/backgammon/backgammon/backgammon.6 @@ -1,4 +1,4 @@ -.\" $NetBSD: backgammon.6,v 1.5 1996/11/12 08:06:56 mikel Exp $ +.\" $NetBSD: backgammon.6,v 1.6 1997/10/10 08:59:35 lukem Exp $ .\" .\" Copyright (c) 1980, 1993 .\" The Regents of the University of California. All rights reserved. @@ -40,7 +40,7 @@ .Nm backgammon .Nd the game of backgammon .Sh SYNOPSIS -.Nm backgammon +.Nm .Op Fl .Op Fl nrwb .Op Fl pr @@ -106,7 +106,7 @@ If .Ar term has capabilities for direct cursor movement (see .Xr termcap 5 ) -.Nm backgammon +.Nm ``fixes'' the board after each move, so the board does not need to be reprinted, unless the screen suffers some horrendous malady. diff --git a/backgammon/backgammon/backlocal.h b/backgammon/backgammon/backlocal.h new file mode 100644 index 00000000..792d796d --- /dev/null +++ b/backgammon/backgammon/backlocal.h @@ -0,0 +1,48 @@ +/* $NetBSD: backlocal.h,v 1.1 1997/10/10 08:59:35 lukem Exp $ */ + +/*- + * Copyright (c) 1997 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Luke Mewburn. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the NetBSD + * Foundation, Inc. and its contributors. + * 4. Neither the name of The NetBSD Foundation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +void dble __P((void)); +int dblgood __P((void)); +int eval __P((void)); +int freemen __P((int)); +void movcmp __P((void)); +void move __P((int)); +int movegood __P((void)); +void pickmove __P((void)); +int trapped __P((int, int)); +void trymove __P((int, int)); diff --git a/backgammon/backgammon/extra.c b/backgammon/backgammon/extra.c index 034c959f..5d76f86e 100644 --- a/backgammon/backgammon/extra.c +++ b/backgammon/backgammon/extra.c @@ -1,4 +1,4 @@ -/* $NetBSD: extra.c,v 1.3 1995/03/21 15:05:01 cgd Exp $ */ +/* $NetBSD: extra.c,v 1.4 1997/10/10 08:59:36 lukem Exp $ */ /* * Copyright (c) 1980, 1993 @@ -33,19 +33,20 @@ * SUCH DAMAGE. */ +#include #ifndef lint #if 0 static char sccsid[] = "@(#)extra.c 8.1 (Berkeley) 5/31/93"; #else -static char rcsid[] = "$NetBSD: extra.c,v 1.3 1995/03/21 15:05:01 cgd Exp $"; +__RCSID("$NetBSD: extra.c,v 1.4 1997/10/10 08:59:36 lukem Exp $"); #endif #endif /* not lint */ #include "back.h" +#include "backlocal.h" #ifdef DEBUG -#include -FILE *trace; +FILE *trace; #endif /* @@ -53,60 +54,57 @@ FILE *trace; * Have the current player double and ask opponent to accept. */ -dble () { - register int resp; /* response to y/n */ +void +dble() +{ + int resp; /* response to y/n */ - for (;;) { - writel (" doubles."); /* indicate double */ + for (;;) { + writel(" doubles."); /* indicate double */ - if (cturn == -pnum) { /* see if computer accepts */ - if (dblgood()) { /* guess not */ - writel (" Declined.\n"); + if (cturn == -pnum) { /* see if computer accepts */ + if (dblgood()) { /* guess not */ + writel(" Declined.\n"); nexturn(); - cturn *= -2; /* indicate loss */ + cturn *= -2; /* indicate loss */ return; - } else { /* computer accepts */ - writel (" Accepted.\n"); - gvalue *= 2; /* double game value */ + } else {/* computer accepts */ + writel(" Accepted.\n"); + gvalue *= 2; /* double game value */ dlast = cturn; if (tflag) gwrite(); return; } } + /* ask if player accepts */ + writel(" Does "); + writel(cturn == 1 ? color[2] : color[3]); + writel(" accept?"); - /* ask if player accepts */ - writel (" Does "); - writel (cturn == 1? color[2]: color[3]); - writel (" accept?"); - - /* get response from yorn, - * a "2" means he said "p" - * for print board. */ - if ((resp = yorn ('R')) == 2) { - writel (" Reprint.\n"); + /* get response from yorn, a "2" means he said "p" for print + * board. */ + if ((resp = yorn('R')) == 2) { + writel(" Reprint.\n"); buflush(); wrboard(); - writel (*Colorptr); + writel(*Colorptr); continue; } - - /* check response */ - if (resp) { - /* accepted */ + /* check response */ + if (resp) { + /* accepted */ gvalue *= 2; dlast = cturn; if (tflag) gwrite(); return; } - - nexturn (); /* declined */ + nexturn(); /* declined */ cturn *= -2; return; } } - /* * dblgood () * Returns 1 if the computer would double in this position. This @@ -118,44 +116,45 @@ dble () { * behind. */ -dblgood () { - register int n; /* accumulated judgment */ - register int OFFC = *offptr; /* no. of computer's men off */ - register int OFFO = *offopp; /* no. of player's men off */ +int +dblgood() +{ + int n; /* accumulated judgment */ + int OFFC = *offptr; /* no. of computer's men off */ + int OFFO = *offopp; /* no. of player's men off */ #ifdef DEBUG - register int i; + int i; if (trace == NULL) - trace = fopen ("bgtrace","w"); + trace = fopen("bgtrace", "w"); #endif - /* get real pip value */ - n = eval()*cturn; + /* get real pip value */ + n = eval() * cturn; #ifdef DEBUG - fputs ("\nDoubles:\nBoard: ",trace); + fputs("\nDoubles:\nBoard: ", trace); for (i = 0; i < 26; i++) - fprintf (trace," %d",board[i]); - fprintf (trace,"\n\tpip = %d, ",n); + fprintf(trace, " %d", board[i]); + fprintf(trace, "\n\tpip = %d, ", n); #endif - /* below adjusts pip value - * according to position - * judgments */ + /* below adjusts pip value according to position judgments */ - /* check men moving off - * board */ - if (OFFC > -15 || OFFO > -15) { - if (OFFC < 0 && OFFO < 0) { + /* check men moving off board */ + if (OFFC > -15 || OFFO > -15) { + if (OFFC < 0 && OFFO < 0) { OFFC += 15; OFFO += 15; - n +=((OFFC-OFFO)*7)/2; - } else if (OFFC < 0) { - OFFC += 15; - n -= OFFO*7/2; - } else if (OFFO < 0) { - OFFO += 15; - n += OFFC*7/2; - } + n += ((OFFC - OFFO) * 7) / 2; + } else + if (OFFC < 0) { + OFFC += 15; + n -= OFFO * 7 / 2; + } else + if (OFFO < 0) { + OFFO += 15; + n += OFFC * 7 / 2; + } if (OFFC < 8 && OFFO > 8) n -= 7; if (OFFC < 10 && OFFO > 10) @@ -168,92 +167,92 @@ dblgood () { n += 7; if (OFFO < 12 && OFFC > 12) n += 7; - n += ((OFFC-OFFO)*7)/2; + n += ((OFFC - OFFO) * 7) / 2; } - #ifdef DEBUG - fprintf (trace,"off = %d, ",n); + fprintf(trace, "off = %d, ", n); #endif - /* see if men are trapped */ + /* see if men are trapped */ n -= freemen(bar); n += freemen(home); - n += trapped(home,-cturn); - n -= trapped(bar,cturn); + n += trapped(home, -cturn); + n -= trapped(bar, cturn); #ifdef DEBUG - fprintf (trace,"free = %d\n",n); - fprintf (trace,"\tOFFC = %d, OFFO = %d\n",OFFC,OFFO); - fflush (trace); + fprintf(trace, "free = %d\n", n); + fprintf(trace, "\tOFFC = %d, OFFO = %d\n", OFFC, OFFO); + fflush(trace); #endif - /* double if 2-3 moves ahead */ - if (n > 10+rnum(7)) - return(1); + /* double if 2-3 moves ahead */ + if (n > 10 + rnum(7)) + return (1); return (0); } - -freemen (b) -int b; +int +freemen(b) + int b; { - register int i, inc, lim; + int i, inc, lim; - odds(0,0,0); + odds(0, 0, 0); if (board[b] == 0) return (0); - inc = (b == 0? 1: -1); - lim = (b == 0? 7: 18); - for (i = b+inc; i != lim; i += inc) - if (board[i]*inc < -1) - odds(abs(b-i),0,abs(board[b])); + inc = (b == 0 ? 1 : -1); + lim = (b == 0 ? 7 : 18); + for (i = b + inc; i != lim; i += inc) + if (board[i] * inc < -1) + odds(abs(b - i), 0, abs(board[b])); if (abs(board[b]) == 1) - return ((36-count())/5); - return (count()/5); + return ((36 - count()) / 5); + return (count() / 5); } - -trapped (n,inc) -int n, inc; +int +trapped(n, inc) + int n, inc; { - register int i, j, k; - int c, l, ct; + int i, j, k; + int c, l, ct; ct = 0; - l = n+7*inc; - for (i = n+inc; i != l; i += inc) { - odds (0,0,0); - c = abs(i-l); - if (board[i]*inc > 0) { + l = n + 7 * inc; + for (i = n + inc; i != l; i += inc) { + odds(0, 0, 0); + c = abs(i - l); + if (board[i] * inc > 0) { for (j = c; j < 13; j++) - if (board[i+inc*j]*inc < -1) { + if (board[i + inc * j] * inc < -1) { if (j < 7) - odds (j,0,1); + odds(j, 0, 1); for (k = 1; k < 7 && k < j; k++) - if (j-k < 7) - odds (k,j-k,1); + if (j - k < 7) + odds(k, j - k, 1); } - ct += abs(board[i])*(36-count()); + ct += abs(board[i]) * (36 - count()); } } - return (ct/5); + return (ct / 5); } - -eval () { - register int i, j; +int +eval() +{ + int i, j; for (j = i = 0; i < 26; i++) - j += (board[i] >= 0 ? i*board[i] : (25-i)*board[i]); + j += (board[i] >= 0 ? i * board[i] : (25 - i) * board[i]); if (off[1] >= 0) - j += 25*off[1]; + j += 25 * off[1]; else - j += 25*(off[1]+15); + j += 25 * (off[1] + 15); if (off[0] >= 0) - j -= 25*off[0]; + j -= 25 * off[0]; else - j -= 25*(off[0]+15); + j -= 25 * (off[0] + 15); return (j); } diff --git a/backgammon/backgammon/main.c b/backgammon/backgammon/main.c index 7554ff65..f58ccaf6 100644 --- a/backgammon/backgammon/main.c +++ b/backgammon/backgammon/main.c @@ -1,4 +1,4 @@ -/* $NetBSD: main.c,v 1.4 1995/04/29 00:44:03 mycroft Exp $ */ +/* $NetBSD: main.c,v 1.5 1997/10/10 08:59:37 lukem Exp $ */ /* * Copyright (c) 1980, 1993 @@ -33,184 +33,174 @@ * SUCH DAMAGE. */ +#include #ifndef lint -static char copyright[] = -"@(#) Copyright (c) 1980, 1993\n\ - The Regents of the University of California. 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 #if 0 static char sccsid[] = "@(#)main.c 8.1 (Berkeley) 5/31/93"; #else -static char rcsid[] = "$NetBSD: main.c,v 1.4 1995/04/29 00:44:03 mycroft Exp $"; +__RCSID("$NetBSD: main.c,v 1.5 1997/10/10 08:59:37 lukem Exp $"); #endif -#endif /* not lint */ +#endif /* not lint */ -#include #include "back.h" +#include "backlocal.h" -#define MVPAUSE 5 /* time to sleep when stuck */ -#define MAXUSERS 35 /* maximum number of users */ +#define MVPAUSE 5 /* time to sleep when stuck */ +#define MAXUSERS 35 /* maximum number of users */ -char *instr[]; /* text of instructions */ -char *message[]; /* update message */ -speed_t ospeed; /* tty output speed */ +char *instr[]; /* text of instructions */ +char *message[]; /* update message */ +speed_t ospeed; /* tty output speed */ -char *helpm[] = { /* help message */ +char *helpm[] = { /* help message */ "Enter a space or newline to roll, or", " R to reprint the board\tD to double", " S to save the game\tQ to quit", 0 }; -char *contin[] = { /* pause message */ +char *contin[] = { /* pause message */ "(Type a newline to continue.)", "", 0 }; - +#if 0 static char user1a[] = - "Sorry, you cannot play backgammon when there are more than "; +"Sorry, you cannot play backgammon when there are more than "; static char user1b[] = - " users\non the system."; +" users\non the system."; static char user2a[] = - "\nThere are now more than "; +"\nThere are now more than "; static char user2b[] = - " users on the system, so you cannot play\nanother game. "; -static char rules[] = "\nDo you want the rules of the game?"; -static char noteach[] = "Teachgammon not available!\n\007"; -static char need[] = "Do you need instructions for this program?"; -static char askcol[] = - "Enter 'r' to play red, 'w' to play white, 'b' to play both:"; -static char rollr[] = "Red rolls a "; -static char rollw[] = ". White rolls a "; -static char rstart[] = ". Red starts.\n"; -static char wstart[] = ". White starts.\n"; -static char toobad1[] = "Too bad, "; -static char unable[] = " is unable to use that roll.\n"; -static char toobad2[] = ". Too bad, "; -static char cantmv[] = " can't move.\n"; -static char bgammon[] = "Backgammon! "; -static char gammon[] = "Gammon! "; -static char again[] = ".\nWould you like to play again?"; -static char svpromt[] = "Would you like to save this game?"; - -static char password[] = "losfurng"; -static char pbuf[10]; - -main (argc,argv) -int argc; -char **argv; - +" users on the system, so you cannot play\nanother game. "; +#endif +static char rules[] = "\nDo you want the rules of the game?"; +static char noteach[] = "Teachgammon not available!\n\007"; +static char need[] = "Do you need instructions for this program?"; +static char askcol[] = +"Enter 'r' to play red, 'w' to play white, 'b' to play both:"; +static char rollr[] = "Red rolls a "; +static char rollw[] = ". White rolls a "; +static char rstart[] = ". Red starts.\n"; +static char wstart[] = ". White starts.\n"; +static char toobad1[] = "Too bad, "; +static char unable[] = " is unable to use that roll.\n"; +static char toobad2[] = ". Too bad, "; +static char cantmv[] = " can't move.\n"; +static char bgammon[] = "Backgammon! "; +static char gammon[] = "Gammon! "; +static char again[] = ".\nWould you like to play again?"; +static char svpromt[] = "Would you like to save this game?"; + +static char password[] = "losfurng"; +static char pbuf[10]; + +int +main(argc, argv) + int argc; + char **argv; { - register int i; /* non-descript index */ - register int l; /* non-descript index */ - register char c; /* non-descript character storage */ - long t; /* time for random num generator */ + int i; /* non-descript index */ + int l; /* non-descript index */ + char c; /* non-descript character storage */ + long t; /* time for random num generator */ /* initialization */ - bflag = 2; /* default no board */ - signal (2, getout); /* trap interrupts */ - if (tcgetattr (0, &old) == -1) /* get old tty mode */ - errexit ("backgammon(gtty)"); + bflag = 2; /* default no board */ + signal(2, getout); /* trap interrupts */ + if (tcgetattr(0, &old) == -1) /* get old tty mode */ + errexit("backgammon(gtty)"); noech = old; noech.c_lflag &= ~ECHO; raw = noech; - raw.c_lflag &= ~ICANON; /* set up modes */ - ospeed = cfgetospeed (&old); /* for termlib */ - - /* check user count */ -# ifdef CORY - if (ucount() > MAXUSERS) { - writel (user1a); - wrint (MAXUSERS); - writel (user1b); - getout(); + raw.c_lflag &= ~ICANON; /* set up modes */ + ospeed = cfgetospeed(&old); /* for termlib */ + + /* check user count */ +#ifdef CORY + if (ucount() > MAXUSERS) { + writel(user1a); + wrint(MAXUSERS); + writel(user1b); + getout(0); } -# endif - - /* get terminal - * capabilities, and - * decide if it can - * cursor address */ - tflag = getcaps (getenv ("TERM")); - /* use whole screen - * for text */ +#endif + + /* get terminal capabilities, and decide if it can cursor address */ + tflag = getcaps(getenv("TERM")); + /* use whole screen for text */ if (tflag) begscr = 0; t = time(0); - srandom(t); /* 'random' seed */ + srandom(t); /* 'random' seed */ #ifdef V7 - while (*++argv != 0) /* process arguments */ + while (*++argv != 0) /* process arguments */ #else - while (*++argv != -1) /* process arguments */ + while (*++argv != -1) /* process arguments */ #endif - getarg (&argv); + getarg(&argv); args[acnt] = '\0'; - if (tflag) { /* clear screen */ - noech.c_oflag &= ~(ONLCR|OXTABS); - raw.c_oflag &= ~(ONLCR|OXTABS); + if (tflag) { /* clear screen */ + noech.c_oflag &= ~(ONLCR | OXTABS); + raw.c_oflag &= ~(ONLCR | OXTABS); clear(); } - fixtty (&raw); /* go into raw mode */ - - /* check if restored - * game and save flag - * for later */ - if (rfl = rflag) { - text (message); /* print message */ - text (contin); - wrboard(); /* print board */ - /* if new game, pretend - * to be a non-restored - * game */ + fixtty(&raw); /* go into raw mode */ + + /* check if restored game and save flag for later */ + if ((rfl = rflag) != 0) { + text(message); /* print message */ + text(contin); + wrboard(); /* print board */ + /* if new game, pretend to be a non-restored game */ if (cturn == 0) rflag = 0; - } else { - rscore = wscore = 0; /* zero score */ - text (message); /* update message - * without pausing */ + } else { + rscore = wscore = 0; /* zero score */ + text(message); /* update message without pausing */ - if (aflag) { /* print rules */ - writel (rules); - if (yorn(0)) { + if (aflag) { /* print rules */ + writel(rules); + if (yorn(0)) { - fixtty (&old); /* restore tty */ - execl (TEACH,"teachgammon",args,0); + fixtty(&old); /* restore tty */ + execl(TEACH, "teachgammon", args, 0); - tflag = 0; /* error! */ - writel (noteach); + tflag = 0; /* error! */ + writel(noteach); exit(1); - } else { /* if not rules, then - * instructions */ - writel (need); - if (yorn(0)) { /* print instructions */ + } else {/* if not rules, then instructions */ + writel(need); + if (yorn(0)) { /* print instructions */ clear(); - text (instr); + text(instr); } } } + init(); /* initialize board */ - init(); /* initialize board */ - - if (pnum == 2) { /* ask for color(s) */ - writec ('\n'); - writel (askcol); - while (pnum == 2) { + if (pnum == 2) {/* ask for color(s) */ + writec('\n'); + writel(askcol); + while (pnum == 2) { c = readc(); - switch (c) { + switch (c) { - case 'R': /* red */ + case 'R': /* red */ pnum = -1; break; - case 'W': /* white */ + case 'W': /* white */ pnum = 1; break; - case 'B': /* both */ + case 'B': /* both */ pnum = 0; break; @@ -218,91 +208,85 @@ char **argv; if (iroll) break; if (tflag) - curmove (curr,0); + curmove(curr, 0); else - writec ('\n'); - writel ("Password:"); - signal (14,getout); + writec('\n'); + writel("Password:"); + signal(14, getout); cflag = 1; - alarm (10); - for (i = 0; i < 10; i++) { + alarm(10); + for (i = 0; i < 10; i++) { pbuf[i] = readc(); if (pbuf[i] == '\n') break; } if (i == 10) while (readc() != '\n'); - alarm (0); + alarm(0); cflag = 0; if (i < 10) pbuf[i] = '\0'; for (i = 0; i < 9; i++) if (pbuf[i] != password[i]) - getout(); + getout(0); iroll = 1; if (tflag) - curmove (curr,0); + curmove(curr, 0); else - writec ('\n'); - writel (askcol); + writec('\n'); + writel(askcol); break; - default: /* error */ - writec ('\007'); + default: /* error */ + writec('\007'); } } - } else if (!aflag) - /* pause to read - * message */ - text (contin); + } else + if (!aflag) + /* pause to read message */ + text(contin); - wrboard(); /* print board */ + wrboard(); /* print board */ if (tflag) - curmove (18,0); + curmove(18, 0); else - writec ('\n'); + writec('\n'); } - /* limit text to bottom - * of screen */ + /* limit text to bottom of screen */ if (tflag) begscr = 17; - for (;;) { /* begin game! */ - /* initial roll if - * needed */ - if ((! rflag) || raflag) + for (;;) { /* begin game! */ + /* initial roll if needed */ + if ((!rflag) || raflag) roll(); - /* perform ritual of - * first roll */ - if (! rflag) { + /* perform ritual of first roll */ + if (!rflag) { if (tflag) - curmove (17,0); - while (D0 == D1) /* no doubles */ + curmove(17, 0); + while (D0 == D1) /* no doubles */ roll(); - /* print rolls */ - writel (rollr); - writec (D0+'0'); - writel (rollw); - writec (D1+'0'); + /* print rolls */ + writel(rollr); + writec(D0 + '0'); + writel(rollw); + writec(D1 + '0'); - /* winner goes first */ - if (D0 > D1) { - writel (rstart); + /* winner goes first */ + if (D0 > D1) { + writel(rstart); cturn = 1; - } else { - writel (wstart); + } else { + writel(wstart); cturn = -1; } } + /* initalize variables according to whose turn it is */ - /* initalize variables - * according to whose - * turn it is */ - - if (cturn == 1) { /* red */ + if (cturn == 1) { /* red */ home = 25; bar = 0; inptr = &in[1]; @@ -312,7 +296,7 @@ char **argv; Colorptr = &color[1]; colorptr = &color[3]; colen = 3; - } else { /* white */ + } else { /* white */ home = 0; bar = 25; inptr = &in[0]; @@ -324,272 +308,255 @@ char **argv; colen = 5; } - /* do first move - * (special case) */ - if (! (rflag && raflag)) { - if (cturn == pnum) /* computer's move */ - move (0); - else { /* player's move */ + /* do first move (special case) */ + if (!(rflag && raflag)) { + if (cturn == pnum) /* computer's move */ + move(0); + else { /* player's move */ mvlim = movallow(); - /* reprint roll */ + /* reprint roll */ if (tflag) - curmove (cturn == -1? 18: 19,0); + curmove(cturn == -1 ? 18 : 19, 0); proll(); - getmove(); /* get player's move */ + getmove(); /* get player's move */ } } - if (tflag) { - curmove (17,0); + if (tflag) { + curmove(17, 0); cline(); begscr = 18; } - - /* no longer any diff- - * erence between normal - * game and recovered - * game. */ + /* no longer any diff- erence between normal game and + * recovered game. */ rflag = 0; - /* move as long as it's - * someone's turn */ - while (cturn == 1 || cturn == -1) { + /* move as long as it's someone's turn */ + while (cturn == 1 || cturn == -1) { - /* board maintainence */ + /* board maintainence */ if (tflag) - refresh(); /* fix board */ + refresh(); /* fix board */ else - /* redo board if -p */ + /* redo board if -p */ if (cturn == bflag || bflag == 0) wrboard(); - /* do computer's move */ - if (cturn == pnum) { - move (1); + /* do computer's move */ + if (cturn == pnum) { + move(1); - /* see if double - * refused */ + /* see if double refused */ if (cturn == -2 || cturn == 2) break; - /* check for winning - * move */ - if (*offopp == 15) { + /* check for winning move */ + if (*offopp == 15) { cturn *= -2; break; } continue; } + /* (player's move) */ - /* (player's move) */ - - /* clean screen if - * safe */ - if (tflag && hflag) { - curmove (20,0); - clend (); + /* clean screen if safe */ + if (tflag && hflag) { + curmove(20, 0); + clend(); hflag = 1; } - - /* if allowed, give him - * a chance to double */ - if (dlast != cturn && gvalue < 64) { + /* if allowed, give him a chance to double */ + if (dlast != cturn && gvalue < 64) { if (tflag) - curmove (cturn == -1? 18: 19,0); - writel (*Colorptr); + curmove(cturn == -1 ? 18 : 19, 0); + writel(*Colorptr); c = readc(); - /* character cases */ - switch (c) { + /* character cases */ + switch (c) { - /* reprint board */ + /* reprint board */ case 'R': wrboard(); break; - /* save game */ + /* save game */ case 'S': raflag = 1; - save (1); + save(1); break; - /* quit */ + /* quit */ case 'Q': quit(); break; - /* double */ + /* double */ case 'D': dble(); break; - /* roll */ + /* roll */ case ' ': case '\n': roll(); - writel (" rolls "); - writec (D0+'0'); - writec (' '); - writec (D1+'0'); - writel (". "); - - /* see if he can move */ - if ( (mvlim = movallow()) == 0) { - - /* can't move */ - writel (toobad1); - writel (*colorptr); - writel (unable); - if (tflag) { - if (pnum) { + writel(" rolls "); + writec(D0 + '0'); + writec(' '); + writec(D1 + '0'); + writel(". "); + + /* see if he can move */ + if ((mvlim = movallow()) == 0) { + + /* can't move */ + writel(toobad1); + writel(*colorptr); + writel(unable); + if (tflag) { + if (pnum) { buflush(); - sleep (MVPAUSE); + sleep(MVPAUSE); } } nexturn(); break; } - - /* get move */ + /* get move */ getmove(); - /* okay to clean - * screen */ + /* okay to clean screen */ hflag = 1; break; - /* invalid character */ + /* invalid character */ default: - /* print help message */ + /* print help message */ if (tflag) - curmove (20,0); + curmove(20, 0); else - writec ('\n'); - text (helpm); + writec('\n'); + text(helpm); if (tflag) - curmove (cturn == -1? 18: 19,0); + curmove(cturn == -1 ? 18 : 19, 0); else - writec ('\n'); + writec('\n'); - /* don't erase */ + /* don't erase */ hflag = 0; } - } else { /* couldn't double */ + } else {/* couldn't double */ - /* print roll */ + /* print roll */ roll(); if (tflag) - curmove (cturn == -1? 18: 19,0); - proll (); + curmove(cturn == -1 ? 18 : 19, 0); + proll(); - /* can he move? */ - if ((mvlim = movallow()) == 0) { + /* can he move? */ + if ((mvlim = movallow()) == 0) { - /* he can't */ - writel (toobad2); - writel (*colorptr); - writel (cantmv); + /* he can't */ + writel(toobad2); + writel(*colorptr); + writel(cantmv); buflush(); - sleep (MVPAUSE); + sleep(MVPAUSE); nexturn(); continue; } - - /* get move */ + /* get move */ getmove(); } } - /* don't worry about who - * won if quit */ + /* don't worry about who won if quit */ if (cturn == 0) break; - /* fix cturn = winner */ + /* fix cturn = winner */ cturn /= -2; - /* final board pos. */ + /* final board pos. */ if (tflag) refresh(); - /* backgammon? */ + /* backgammon? */ mflag = 0; - l = bar+7*cturn; + l = bar + 7 * cturn; for (i = bar; i != l; i += cturn) - if (board[i]*cturn) mflag++; + if (board[i] * cturn) + mflag++; - /* compute game value */ + /* compute game value */ if (tflag) - curmove (20,0); - if (*offopp == 15) { - if (mflag) { - writel (bgammon); + curmove(20, 0); + if (*offopp == 15) { + if (mflag) { + writel(bgammon); gvalue *= 3; - } - else if (*offptr <= 0) { - writel (gammon); - gvalue *= 2; - } + } else + if (*offptr <= 0) { + writel(gammon); + gvalue *= 2; + } } - - /* report situation */ - if (cturn == -1) { - writel ("Red wins "); + /* report situation */ + if (cturn == -1) { + writel("Red wins "); rscore += gvalue; } else { - writel ("White wins "); + writel("White wins "); wscore += gvalue; } - wrint (gvalue); - writel (" point"); + wrint(gvalue); + writel(" point"); if (gvalue > 1) - writec ('s'); - writel (".\n"); + writec('s'); + writel(".\n"); - /* write score */ + /* write score */ wrscore(); - /* check user count */ -# ifdef CORY - if (ucount() > MAXUSERS) { - writel (user2a); - wrint (MAXUSERS); - writel (user2b); + /* check user count */ +#ifdef CORY + if (ucount() > MAXUSERS) { + writel(user2a); + wrint(MAXUSERS); + writel(user2b); rfl = 1; break; } -# endif +#endif - /* see if he wants - * another game */ - writel (again); - if ((i = yorn ('S')) == 0) + /* see if he wants another game */ + writel(again); + if ((i = yorn('S')) == 0) break; init(); - if (i == 2) { - writel (" Save.\n"); + if (i == 2) { + writel(" Save.\n"); cturn = 0; - save (0); + save(0); } - - /* yes, reset game */ + /* yes, reset game */ wrboard(); } /* give him a chance to save if game was recovered */ - if (rfl && cturn) { - writel (svpromt); - if (yorn (0)) { - /* re-initialize for - * recovery */ + if (rfl && cturn) { + writel(svpromt); + if (yorn(0)) { + /* re-initialize for recovery */ init(); cturn = 0; save(0); } } - - /* leave peacefully */ - getout (); + /* leave peacefully */ + getout(0); + /* NOTREACHED */ + return (0); } diff --git a/backgammon/backgammon/move.c b/backgammon/backgammon/move.c index f96c57a1..f83ebf31 100644 --- a/backgammon/backgammon/move.c +++ b/backgammon/backgammon/move.c @@ -1,4 +1,4 @@ -/* $NetBSD: move.c,v 1.5 1995/04/29 00:44:05 mycroft Exp $ */ +/* $NetBSD: move.c,v 1.6 1997/10/10 08:59:37 lukem Exp $ */ /* * Copyright (c) 1980, 1993 @@ -33,297 +33,302 @@ * SUCH DAMAGE. */ +#include #ifndef lint #if 0 static char sccsid[] = "@(#)move.c 8.1 (Berkeley) 5/31/93"; #else -static char rcsid[] = "$NetBSD: move.c,v 1.5 1995/04/29 00:44:05 mycroft Exp $"; +__RCSID("$NetBSD: move.c,v 1.6 1997/10/10 08:59:37 lukem Exp $"); #endif #endif /* not lint */ #include "back.h" +#include "backlocal.h" #ifdef DEBUG -#include -FILE *trace; -static char tests[20]; +FILE *trace; +static char tests[20]; #endif -struct BOARD { /* structure of game position */ - int b_board[26]; /* board position */ - int b_in[2]; /* men in */ - int b_off[2]; /* men off */ - int b_st[4], b_fn[4]; /* moves */ +struct BOARD { /* structure of game position */ + int b_board[26]; /* board position */ + int b_in[2]; /* men in */ + int b_off[2]; /* men off */ + int b_st[4], b_fn[4]; /* moves */ - struct BOARD *b_next; /* forward queue pointer */ + struct BOARD *b_next; /* forward queue pointer */ }; struct BOARD *freeq = 0; struct BOARD *checkq = 0; -struct BOARD *bsave(); -struct BOARD *nextfree(); - - /* these variables are values for the - * candidate move */ -static int ch; /* chance of being hit */ -static int op; /* computer's open men */ -static int pt; /* comp's protected points */ -static int em; /* farthest man back */ -static int frc; /* chance to free comp's men */ -static int frp; /* chance to free pl's men */ - - /* these values are the values for the - * move chosen (so far) */ -static int chance; /* chance of being hit */ -static int openmen; /* computer's open men */ -static int points; /* comp's protected points */ -static int endman; /* farthest man back */ -static int barmen; /* men on bar */ -static int menin; /* men in inner table */ -static int menoff; /* men off board */ -static int oldfrc; /* chance to free comp's men */ -static int oldfrp; /* chance to free pl's men */ - -static int cp[5]; /* candidate start position */ -static int cg[5]; /* candidate finish position */ - -static int race; /* game reduced to a race */ - -move (okay) -int okay; /* zero if first move */ + + /* these variables are values for the candidate move */ +static int ch; /* chance of being hit */ +static int op; /* computer's open men */ +static int pt; /* comp's protected points */ +static int em; /* farthest man back */ +static int frc; /* chance to free comp's men */ +static int frp; /* chance to free pl's men */ + + /* these values are the values for the move chosen (so far) */ +static int chance; /* chance of being hit */ +static int openmen; /* computer's open men */ +static int points; /* comp's protected points */ +static int endman; /* farthest man back */ +static int barmen; /* men on bar */ +static int menin; /* men in inner table */ +static int menoff; /* men off board */ +static int oldfrc; /* chance to free comp's men */ +static int oldfrp; /* chance to free pl's men */ + +static int cp[5]; /* candidate start position */ +static int cg[5]; /* candidate finish position */ + +static int race; /* game reduced to a race */ + + +static int bcomp __P((struct BOARD *, struct BOARD *)); +static struct BOARD *bsave __P((void)); +static void binsert __P((struct BOARD *)); +static void boardcopy __P((struct BOARD *)); +static void makefree __P((struct BOARD *)); +static void mvcheck __P((struct BOARD *, struct BOARD *)); +static struct BOARD *nextfree __P((void)); + + +void +move(okay) + int okay; /* zero if first move */ { - register int i; /* index */ - register int l; /* last man */ - - if (okay) { - /* see if comp should double */ - if (gvalue < 64 && dlast != cturn && dblgood()) { - writel (*Colorptr); - dble(); /* double */ - /* return if declined */ + int i; /* index */ + int l; /* last man */ + + l = 0; + if (okay) { + /* see if comp should double */ + if (gvalue < 64 && dlast != cturn && dblgood()) { + writel(*Colorptr); + dble(); /* double */ + /* return if declined */ if (cturn != 1 && cturn != -1) return; } roll(); } - race = 0; - for (i = 0; i < 26; i++) { + for (i = 0; i < 26; i++) { if (board[i] < 0) l = i; } - for (i = 0; i < l; i++) { + for (i = 0; i < l; i++) { if (board[i] > 0) break; } if (i == l) race = 1; - /* print roll */ + /* print roll */ if (tflag) - curmove (cturn == -1? 18: 19,0); - writel (*Colorptr); - writel (" rolls "); - writec (D0+'0'); - writec (' '); - writec (D1+'0'); - /* make tty interruptable - * while thinking */ + curmove(cturn == -1 ? 18 : 19, 0); + writel(*Colorptr); + writel(" rolls "); + writec(D0 + '0'); + writec(' '); + writec(D1 + '0'); + /* make tty interruptable while thinking */ if (tflag) cline(); - fixtty (&noech); + fixtty(&noech); - /* find out how many moves */ + /* find out how many moves */ mvlim = movallow(); - if (mvlim == 0) { - writel (" but cannot use it.\n"); + if (mvlim == 0) { + writel(" but cannot use it.\n"); nexturn(); - fixtty (&raw); + fixtty(&raw); return; } - - /* initialize */ + /* initialize */ for (i = 0; i < 4; i++) cp[i] = cg[i] = 0; - /* strategize */ - trymove (0,0); + /* strategize */ + trymove(0, 0); pickmove(); - /* print move */ - writel (" and moves "); - for (i = 0; i < mvlim; i++) { + /* print move */ + writel(" and moves "); + for (i = 0; i < mvlim; i++) { if (i > 0) - writec (','); - wrint (p[i] = cp[i]); - writec ('-'); - wrint (g[i] = cg[i]); - makmove (i); + writec(','); + wrint(p[i] = cp[i]); + writec('-'); + wrint(g[i] = cg[i]); + makmove(i); } - writec ('.'); + writec('.'); - /* print blots hit */ + /* print blots hit */ if (tflag) - curmove (20,0); + curmove(20, 0); else - writec ('\n'); + writec('\n'); for (i = 0; i < mvlim; i++) if (h[i]) wrhit(g[i]); - /* get ready for next move */ + /* get ready for next move */ nexturn(); - if (!okay) { + if (!okay) { buflush(); - sleep (3); + sleep(3); } - fixtty (&raw); /* no more tty interrupt */ + fixtty(&raw); /* no more tty interrupt */ } - -trymove (mvnum,swapped) -register int mvnum; /* number of move (rel zero) */ -int swapped; /* see if swapped also tested */ +void +trymove(mvnum, swapped) + int mvnum; /* number of move (rel zero) */ + int swapped; /* see if swapped also tested */ { - register int pos; /* position on board */ - register int rval; /* value of roll */ + int pos; /* position on board */ + int rval; /* value of roll */ - /* if recursed through all dice - * values, compare move */ - if (mvnum == mvlim) { - binsert (bsave()); + /* if recursed through all dice values, compare move */ + if (mvnum == mvlim) { + binsert(bsave()); return; } - - /* make sure dice in always - * same order */ + /* make sure dice in always same order */ if (d0 == swapped) swap; - /* choose value for this move */ + /* choose value for this move */ rval = dice[mvnum != 0]; - /* find all legitimate moves */ - for (pos = bar; pos != home; pos += cturn) { - /* fix order of dice */ + /* find all legitimate moves */ + for (pos = bar; pos != home; pos += cturn) { + /* fix order of dice */ if (d0 == swapped) swap; - /* break if stuck on bar */ + /* break if stuck on bar */ if (board[bar] != 0 && pos != bar) break; - /* on to next if not occupied */ - if (board[pos]*cturn <= 0) + /* on to next if not occupied */ + if (board[pos] * cturn <= 0) continue; - /* set up arrays for move */ + /* set up arrays for move */ p[mvnum] = pos; - g[mvnum] = pos+rval*cturn; - if (g[mvnum]*cturn >= home) { + g[mvnum] = pos + rval * cturn; + if (g[mvnum] * cturn >= home) { if (*offptr < 0) break; g[mvnum] = home; } - /* try to move */ - if (makmove (mvnum)) + /* try to move */ + if (makmove(mvnum)) continue; else - trymove (mvnum+1,2); - /* undo move to try another */ - backone (mvnum); + trymove(mvnum + 1, 2); + /* undo move to try another */ + backone(mvnum); } - /* swap dice and try again */ + /* swap dice and try again */ if ((!swapped) && D0 != D1) - trymove (0,1); + trymove(0, 1); } - -struct BOARD * -bsave () { - register int i; /* index */ - struct BOARD *now; /* current position */ - now = nextfree (); /* get free BOARD */ +static struct BOARD * +bsave() +{ + int i; /* index */ + struct BOARD *now; /* current position */ + + now = nextfree(); /* get free BOARD */ - /* store position */ + /* store position */ for (i = 0; i < 26; i++) now->b_board[i] = board[i]; now->b_in[0] = in[0]; now->b_in[1] = in[1]; now->b_off[0] = off[0]; now->b_off[1] = off[1]; - for (i = 0; i < mvlim; i++) { + for (i = 0; i < mvlim; i++) { now->b_st[i] = p[i]; now->b_fn[i] = g[i]; } return (now); } - -binsert (new) -struct BOARD *new; /* item to insert */ + +static void +binsert(new) + struct BOARD *new; /* item to insert */ { - register struct BOARD *p = checkq; /* queue pointer */ - register int result; /* comparison result */ + struct BOARD *p = checkq; /* queue pointer */ + int result; /* comparison result */ - if (p == 0) { /* check if queue empty */ + if (p == 0) { /* check if queue empty */ checkq = p = new; p->b_next = 0; return; } - - result = bcomp (new,p); /* compare to first element */ - if (result < 0) { /* insert in front */ + result = bcomp(new, p); /* compare to first element */ + if (result < 0) { /* insert in front */ new->b_next = p; checkq = new; return; } - if (result == 0) { /* duplicate entry */ - mvcheck (p,new); - makefree (new); + if (result == 0) { /* duplicate entry */ + mvcheck(p, new); + makefree(new); return; } - - while (p->b_next != 0) { /* traverse queue */ - result = bcomp (new,p->b_next); - if (result < 0) { /* found place */ + while (p->b_next != 0) {/* traverse queue */ + result = bcomp(new, p->b_next); + if (result < 0) { /* found place */ new->b_next = p->b_next; p->b_next = new; return; } - if (result == 0) { /* duplicate entry */ - mvcheck (p->b_next,new); - makefree (new); + if (result == 0) { /* duplicate entry */ + mvcheck(p->b_next, new); + makefree(new); return; } p = p->b_next; } - /* place at end of queue */ + /* place at end of queue */ p->b_next = new; new->b_next = 0; } - -bcomp (a,b) -struct BOARD *a; -struct BOARD *b; + +static int +bcomp(a, b) + struct BOARD *a; + struct BOARD *b; { - register int *aloc = a->b_board; /* pointer to board a */ - register int *bloc = b->b_board; /* pointer to board b */ - register int i; /* index */ - int result; /* comparison result */ + int *aloc = a->b_board; /* pointer to board a */ + int *bloc = b->b_board; /* pointer to board b */ + int i; /* index */ + int result; /* comparison result */ - for (i = 0; i < 26; i++) { /* compare boards */ - result = cturn*(aloc[i]-bloc[i]); + for (i = 0; i < 26; i++) { /* compare boards */ + result = cturn * (aloc[i] - bloc[i]); if (result) - return (result); /* found inequality */ + return (result); /* found inequality */ } - return (0); /* same position */ + return (0); /* same position */ } - -mvcheck (incumbent,candidate) -register struct BOARD *incumbent; -register struct BOARD *candidate; + +static void +mvcheck(incumbent, candidate) + struct BOARD *incumbent; + struct BOARD *candidate; { - register int i; - register int result; + int i; + int result; - for (i = 0; i < mvlim; i++) { - result = cturn*(candidate->b_st[i]-incumbent->b_st[i]); + for (i = 0; i < mvlim; i++) { + result = cturn * (candidate->b_st[i] - incumbent->b_st[i]); if (result > 0) return; if (result < 0) @@ -331,28 +336,30 @@ register struct BOARD *candidate; } if (i == mvlim) return; - for (i = 0; i < mvlim; i++) { + for (i = 0; i < mvlim; i++) { incumbent->b_st[i] = candidate->b_st[i]; incumbent->b_fn[i] = candidate->b_fn[i]; } } - -makefree (dead) -struct BOARD *dead; /* dead position */ + +void +makefree(dead) + struct BOARD *dead; /* dead position */ { - dead->b_next = freeq; /* add to freeq */ + dead->b_next = freeq; /* add to freeq */ freeq = dead; } -struct BOARD * -nextfree () { - struct BOARD *new; +static struct BOARD * +nextfree() +{ + struct BOARD *new; - if (freeq == 0) { - new = (struct BOARD *)calloc (1,sizeof (struct BOARD)); - if (new == 0) { - writel ("\nOut of memory\n"); - getout(); + if (freeq == 0) { + new = (struct BOARD *) calloc(1, sizeof(struct BOARD)); + if (new == 0) { + writel("\nOut of memory\n"); + getout(0); } } else { new = freeq; @@ -362,100 +369,104 @@ nextfree () { new->b_next = 0; return (new); } - -pickmove () { - /* current game position */ - register struct BOARD *now = bsave(); - register struct BOARD *next; /* next move */ + +void +pickmove() +{ + /* current game position */ + struct BOARD *now = bsave(); + struct BOARD *next; /* next move */ #ifdef DEBUG if (trace == NULL) - trace = fopen ("bgtrace","w"); - fprintf (trace,"\nRoll: %d %d%s\n",D0,D1,race? " (race)": ""); - fflush (trace); + trace = fopen("bgtrace", "w"); + fprintf(trace, "\nRoll: %d %d%s\n", D0, D1, race ? " (race)" : ""); + fflush(trace); #endif - do { /* compare moves */ - boardcopy (checkq); + do { /* compare moves */ + boardcopy(checkq); next = checkq->b_next; - makefree (checkq); + makefree(checkq); checkq = next; movcmp(); } while (checkq != 0); - boardcopy (now); + boardcopy(now); } - -boardcopy (s) -register struct BOARD *s; /* game situation */ + +static void +boardcopy(s) + struct BOARD *s; /* game situation */ { - register int i; /* index */ + int i; /* index */ for (i = 0; i < 26; i++) board[i] = s->b_board[i]; - for (i = 0; i < 2; i++) { + for (i = 0; i < 2; i++) { in[i] = s->b_in[i]; off[i] = s->b_off[i]; } - for (i = 0; i < mvlim; i++) { + for (i = 0; i < mvlim; i++) { p[i] = s->b_st[i]; g[i] = s->b_fn[i]; } } - -movcmp () { - register int i; - register int c; + +void +movcmp() +{ + int i; #ifdef DEBUG if (trace == NULL) - trace = fopen ("bgtrace","w"); + trace = fopen("bgtrace", "w"); #endif - odds (0,0,0); - if (!race) { + odds(0, 0, 0); + if (!race) { ch = op = pt = 0; - for (i = 1; i < 25; i++) { + for (i = 1; i < 25; i++) { if (board[i] == cturn) - ch = canhit (i,1); - op += abs (bar-i); + ch = canhit(i, 1); + op += abs(bar - i); } - for (i = bar+cturn; i != home; i += cturn) - if (board[i]*cturn > 1) - pt += abs(bar-i); - frc = freemen (bar)+trapped (bar,cturn); - frp = freemen (home)+trapped (home,-cturn); + for (i = bar + cturn; i != home; i += cturn) + if (board[i] * cturn > 1) + pt += abs(bar - i); + frc = freemen(bar) + trapped(bar, cturn); + frp = freemen(home) + trapped(home, -cturn); } for (em = bar; em != home; em += cturn) - if (board[em]*cturn > 0) + if (board[em] * cturn > 0) break; - em = abs(home-em); + em = abs(home - em); #ifdef DEBUG - fputs ("Board: ",trace); + fputs("Board: ", trace); for (i = 0; i < 26; i++) - fprintf (trace, " %d",board[i]); + fprintf(trace, " %d", board[i]); if (race) - fprintf (trace,"\n\tem = %d\n",em); + fprintf(trace, "\n\tem = %d\n", em); else - fprintf (trace, - "\n\tch = %d, pt = %d, em = %d, frc = %d, frp = %d\n", - ch,pt,em,frc,frp); - fputs ("\tMove: ",trace); + fprintf(trace, + "\n\tch = %d, pt = %d, em = %d, frc = %d, frp = %d\n", + ch, pt, em, frc, frp); + fputs("\tMove: ", trace); for (i = 0; i < mvlim; i++) - fprintf (trace," %d-%d",p[i],g[i]); - fputs ("\n",trace); - fflush (trace); - strcpy (tests,""); + fprintf(trace, " %d-%d", p[i], g[i]); + fputs("\n", trace); + fflush(trace); + strcpy(tests, ""); #endif - if ((cp[0] == 0 && cg[0] == 0) || movegood()) { + if ((cp[0] == 0 && cg[0] == 0) || movegood()) { #ifdef DEBUG - fprintf (trace,"\t[%s] ... wins.\n",tests); - fflush (trace); + fprintf(trace, "\t[%s] ... wins.\n", tests); + fflush(trace); #endif - for (i = 0; i < mvlim; i++) { + for (i = 0; i < mvlim; i++) { cp[i] = p[i]; cg[i] = g[i]; } - if (!race) { + if (!race) { chance = ch; openmen = op; points = pt; @@ -468,90 +479,92 @@ movcmp () { menoff = *offptr; } #ifdef DEBUG - else { - fprintf (trace,"\t[%s] ... loses.\n",tests); - fflush (trace); + else { + fprintf(trace, "\t[%s] ... loses.\n", tests); + fflush(trace); } #endif } - -movegood () { - register int n; + +int +movegood() +{ + int n; if (*offptr == 15) return (1); if (menoff == 15) return (0); - if (race) { + if (race) { #ifdef DEBUG - strcat (tests,"o"); + strcat(tests, "o"); #endif - if (*offptr-menoff) + if (*offptr - menoff) return (*offptr > menoff); #ifdef DEBUG - strcat (tests,"e"); + strcat(tests, "e"); #endif - if (endman-em) + if (endman - em) return (endman > em); #ifdef DEBUG - strcat (tests,"i"); + strcat(tests, "i"); #endif if (menin == 15) return (0); if (*inptr == 15) return (1); #ifdef DEBUG - strcat (tests,"i"); + strcat(tests, "i"); #endif - if (*inptr-menin) + if (*inptr - menin) return (*inptr > menin); return (rnum(2)); - } else { - n = barmen-abs(board[home]); + } else { + n = barmen - abs(board[home]); #ifdef DEBUG - strcat (tests,"c"); + strcat(tests, "c"); #endif - if (abs(chance-ch)+25*n > rnum(150)) - return (n? (n < 0): (ch < chance)); + if (abs(chance - ch) + 25 * n > rnum(150)) + return (n ? (n < 0) : (ch < chance)); #ifdef DEBUG - strcat (tests,"o"); + strcat(tests, "o"); #endif - if (*offptr-menoff) + if (*offptr - menoff) return (*offptr > menoff); #ifdef DEBUG - strcat (tests,"o"); + strcat(tests, "o"); #endif - if (abs(openmen-op) > 7+rnum(12)) + if (abs(openmen - op) > 7 + rnum(12)) return (openmen > op); #ifdef DEBUG - strcat (tests,"b"); + strcat(tests, "b"); #endif if (n) return (n < 0); #ifdef DEBUG - strcat (tests,"e"); + strcat(tests, "e"); #endif - if (abs(endman-em) > rnum(2)) + if (abs(endman - em) > rnum(2)) return (endman > em); #ifdef DEBUG - strcat (tests,"f"); + strcat(tests, "f"); #endif - if (abs(frc-oldfrc) > rnum(2)) + if (abs(frc - oldfrc) > rnum(2)) return (frc < oldfrc); #ifdef DEBUG - strcat (tests,"p"); + strcat(tests, "p"); #endif - if (abs(n = pt-points) > rnum(4)) + if (abs(n = pt - points) > rnum(4)) return (n > 0); #ifdef DEBUG - strcat (tests,"i"); + strcat(tests, "i"); #endif - if (*inptr-menin) + if (*inptr - menin) return (*inptr > menin); #ifdef DEBUG - strcat (tests,"f"); + strcat(tests, "f"); #endif - if (abs(frp-oldfrp) > rnum(2)) + if (abs(frp - oldfrp) > rnum(2)) return (frp > oldfrp); return (rnum(2)); } diff --git a/backgammon/backgammon/text.c b/backgammon/backgammon/text.c index 69467994..b6685a42 100644 --- a/backgammon/backgammon/text.c +++ b/backgammon/backgammon/text.c @@ -1,4 +1,4 @@ -/* $NetBSD: text.c,v 1.4 1995/04/29 00:44:06 mycroft Exp $ */ +/* $NetBSD: text.c,v 1.5 1997/10/10 08:59:38 lukem Exp $ */ /* * Copyright (c) 1980, 1993 @@ -33,106 +33,107 @@ * SUCH DAMAGE. */ +#include #ifndef lint #if 0 static char sccsid[] = "@(#)text.c 8.1 (Berkeley) 5/31/93"; #else -static char rcsid[] = "$NetBSD: text.c,v 1.4 1995/04/29 00:44:06 mycroft Exp $"; +__RCSID("$NetBSD: text.c,v 1.5 1997/10/10 08:59:38 lukem Exp $"); #endif #endif /* not lint */ #include "back.h" -char *instr[] = { - " If you did not notice by now, this program reacts to things as", - "soon as you type them, without waiting for a newline. This means that", - "the special characters RUBOUT, ESC, and CONTROL-D, will not perform", - "their special functions during most of this program. The program", - "should usually stop when a RUBOUT is typed, but occasionally it will", - "ignore RUBOUTs until it is waiting for input.\n", - " These instructions are presented in small chunks designed not to", - "roll off the top of your screen. When the characters '-->' are print-", - "ed, no more data will be printed until a space or newline is typed.", - "In this way, you can finish one section before continuing to another.", - "Like this:", - "", - " The two sides are colored 'red' and 'white.' The computer may play", - "one side, or if there are two players, the computer can merely act as", - "a gamekeeper, letting the players make the moves. Once you tell the", - "computer what color(s) you want to play, the decision remains in ef-", - "fect until you quit the program, even if you play more than one game,", - "since the program keeps a running score.\n", - " The program will prompt for a move in one of two ways. If the", - "player has the opportunity to double, then merely his color will be", - "typed out. The player can now do one of several things. He can dou-", - "ble by typing a 'd', he can roll by typing a space (' ') or newline,", - "or if he is not sure, he can reprint the board by typing a 'r'.\n", - " If the player cannot double, his roll will be thrust in front of", - "him, followed by the request 'Move:', asking for a move but not giving", - "him the chance to double. He can still ask for the board by typing", - "'r'. In either of these two states, the player can quit by typing 'q'", - "or save the game by typing 's'. In either case, the player will be", - "asked to verify, in case there was some error. The program then ends", - "immediately, after first saving the file if so requested.", - "", - " A player can move one of his men using two forms of input. The", - "first form is -, where is the starting position, and is", - "the finishing position of the player's man. For example, if white", - "wanted to move a piece from position 13 to position 8, his move could", - "be entered as 13-8. The second form is / where is the", - "starting position, an is the roll actually made. Hence, white", - "could have entered as 13/5 instead of 13-8.\n", - " A player must move each roll of the dice separately. For example,", - "if a player rolled 4 3, and wanted to move from 13 to 6, he could", - "enter it as 13/4,9/3 or 13/3,10/4 or 13-10,10-6 or 13-9,9-6, but not", - "13-6. The last two entries can be shortened to 13-10-6 and 13-9-6.", - "If you want to move more than one piece from the same position, such", - "as 13-10,13-9, you can abbreviate this using the / format as by", - "entering more than one , or 13/34. A player can use both forms for", - "the same roll, e.g. 13/3,13-9, and separates individual moves by ei-", - "ther a comma or a space. The letter 'b' represents the bar, and the", - "letter 'h' represents a player's home. You could also enter the", - "number that would be in the position of the bar, 25 or 0 as appropri-", - "ate. Use a newline at the end of your moves for a turn.", - "", - " As you type in your move, if a character does not make sense under", - "the above constrictions, a bell will sound instead of the character,", - "and it will be ignored. You may kill lines and erase characters as", - "you would normally, but don't be surprised if they look different than", - "usual. Also, if you have entered one or more of your rolls, and you", - "wish to see what the move looks like so far, type a 'r' to see what it", - "looks like. This cannot be done in the middle of a move (e.g., after", - "a '-' or '/'). After the printing board, the program will go back to", - "inputting your move and you can backspace or kill just as if you had", - "just typed in your input.\n", - " Now you should be ready to begin the game. Good luck!", - "", - 0}; - - -text (t) -char **t; +char *instr[] = { + " If you did not notice by now, this program reacts to things as", + "soon as you type them, without waiting for a newline. This means that", + "the special characters RUBOUT, ESC, and CONTROL-D, will not perform", + "their special functions during most of this program. The program", + "should usually stop when a RUBOUT is typed, but occasionally it will", + "ignore RUBOUTs until it is waiting for input.\n", + " These instructions are presented in small chunks designed not to", + "roll off the top of your screen. When the characters '-->' are print-", + "ed, no more data will be printed until a space or newline is typed.", + "In this way, you can finish one section before continuing to another.", + "Like this:", + "", + " The two sides are colored 'red' and 'white.' The computer may play", + "one side, or if there are two players, the computer can merely act as", + "a gamekeeper, letting the players make the moves. Once you tell the", + "computer what color(s) you want to play, the decision remains in ef-", + "fect until you quit the program, even if you play more than one game,", + "since the program keeps a running score.\n", + " The program will prompt for a move in one of two ways. If the", + "player has the opportunity to double, then merely his color will be", + "typed out. The player can now do one of several things. He can dou-", + "ble by typing a 'd', he can roll by typing a space (' ') or newline,", + "or if he is not sure, he can reprint the board by typing a 'r'.\n", + " If the player cannot double, his roll will be thrust in front of", + "him, followed by the request 'Move:', asking for a move but not giving", + "him the chance to double. He can still ask for the board by typing", + "'r'. In either of these two states, the player can quit by typing 'q'", + "or save the game by typing 's'. In either case, the player will be", + "asked to verify, in case there was some error. The program then ends", + "immediately, after first saving the file if so requested.", + "", + " A player can move one of his men using two forms of input. The", + "first form is -, where is the starting position, and is", + "the finishing position of the player's man. For example, if white", + "wanted to move a piece from position 13 to position 8, his move could", + "be entered as 13-8. The second form is / where is the", + "starting position, an is the roll actually made. Hence, white", + "could have entered as 13/5 instead of 13-8.\n", + " A player must move each roll of the dice separately. For example,", + "if a player rolled 4 3, and wanted to move from 13 to 6, he could", + "enter it as 13/4,9/3 or 13/3,10/4 or 13-10,10-6 or 13-9,9-6, but not", + "13-6. The last two entries can be shortened to 13-10-6 and 13-9-6.", + "If you want to move more than one piece from the same position, such", + "as 13-10,13-9, you can abbreviate this using the / format as by", + "entering more than one , or 13/34. A player can use both forms for", + "the same roll, e.g. 13/3,13-9, and separates individual moves by ei-", + "ther a comma or a space. The letter 'b' represents the bar, and the", + "letter 'h' represents a player's home. You could also enter the", + "number that would be in the position of the bar, 25 or 0 as appropri-", + "ate. Use a newline at the end of your moves for a turn.", + "", + " As you type in your move, if a character does not make sense under", + "the above constrictions, a bell will sound instead of the character,", + "and it will be ignored. You may kill lines and erase characters as", + "you would normally, but don't be surprised if they look different than", + "usual. Also, if you have entered one or more of your rolls, and you", + "wish to see what the move looks like so far, type a 'r' to see what it", + "looks like. This cannot be done in the middle of a move (e.g., after", + "a '-' or '/'). After the printing board, the program will go back to", + "inputting your move and you can backspace or kill just as if you had", + "just typed in your input.\n", + " Now you should be ready to begin the game. Good luck!", + "", +0}; +int +text(t) + char **t; { - register int i; - register char *s, *a; + int i; + char *s, *a; - fixtty (&noech); - while (*t != 0) { + fixtty(&noech); + while (*t != 0) { s = a = *t; for (i = 0; *a != '\0'; i--) a++; - if (i) { - writel (s); - writec ('\n'); - } else { - writel ("-->"); - fixtty (&raw); + if (i) { + writel(s); + writec('\n'); + } else { + writel("-->"); + fixtty(&raw); while ((i = readc()) != ' ' && i != '\n'); - fixtty (&noech); + fixtty(&noech); clear(); } t++; } - fixtty (&raw); + fixtty(&raw); + return (0); } diff --git a/backgammon/backgammon/version.c b/backgammon/backgammon/version.c index 504c5960..23ebb2e6 100644 --- a/backgammon/backgammon/version.c +++ b/backgammon/backgammon/version.c @@ -1,4 +1,4 @@ -/* $NetBSD: version.c,v 1.3 1995/03/21 15:05:16 cgd Exp $ */ +/* $NetBSD: version.c,v 1.4 1997/10/10 08:59:39 lukem Exp $ */ /* * Copyright (c) 1980, 1987, 1993 @@ -33,15 +33,16 @@ * SUCH DAMAGE. */ +#include #ifndef lint #if 0 static char sccsid[] = "@(#)version.c 8.1 (Berkeley) 5/31/93"; #else -static char rcsid[] = "$NetBSD: version.c,v 1.3 1995/03/21 15:05:16 cgd Exp $"; +__RCSID("$NetBSD: version.c,v 1.4 1997/10/10 08:59:39 lukem Exp $"); #endif #endif /* not lint */ -char *message[] = { - "Last updated on Saturday, January 11, 1986.", +char *message[] = { + "Last updated on Friday, October 10, 1997.", 0 }; -- cgit v1.2.3-56-ge451