[comp.sources.games] v03i095: backgammon - 4.3BSD backgammon game, Part02/03

games-request@tekred.TEK.COM (03/10/88)

Submitted by: John Gilmore <hoptoad.UUCP!gnu@cgl.ucsf.edu>
Comp.sources.games: Volume 3, Issue 95
Archive-name: backgammon/Part02


#! /bin/sh
# This is a shell archive.  Remove anything before this line, then unpack
# it by saving it into a file and typing "sh file".  To overwrite existing
# files, type "sh file -c".  You can also feed this as standard input via
# unshar, or by typing "sh <file", e.g..  If this archive is complete, you
# will see the following message at the end:
#		"End of archive 2 (of 3)."
# Contents:  Makefile back.h backgammon.1 extra.c subs.c table.c text.c
#   ttext1.c ttext2.c version.c
# Wrapped by billr@saab on Wed Mar  9 09:37:14 1988
PATH=/bin:/usr/bin:/usr/ucb ; export PATH
if test -f Makefile -a "${1}" != "-c" ; then 
  echo shar: Will not over-write existing file \"Makefile\"
else
echo shar: Extracting \"Makefile\" \(4644 characters\)
sed "s/^X//" >Makefile <<'END_OF_Makefile'
X#
X# Copyright (c) 1987 Regents of the University of California.
X# All rights reserved.
X#
X# Redistribution and use in source and binary forms are permitted
X# provided that this notice is preserved and that due credit is given
X# to the University of California at Berkeley. The name of the University
X# may not be used to endorse or promote products derived from this
X# software without specific prior written permission. This software
X# is provided ``as is'' without express or implied warranty.
X#
X#	@(#)Makefile	5.12 (Berkeley) 2/16/88
X#
XLIBC=	/lib/libc.a
XLIBS=	-ltermlib
XCFLAGS=	-O -DV7 #-g
XSRCS=	allow.c board.c check.c data.c extra.c fancy.c init.c main.c move.c \
X	odds.c one.c save.c subs.c table.c teach.c text.c ttext1.c ttext2.c \
X	tutor.c version.c
XBSRCS=	allow.c board.c check.c extra.c fancy.c init.c main.c move.c \
X	odds.c one.c save.c subs.c table.c text.c version.c
XBOBJS=	allow.o board.o check.o extra.o fancy.o init.o main.o move.o \
X	odds.o one.o save.o subs.o table.o text.o version.o
XTSRCS=	allow.c board.c check.c data.c fancy.c init.c odds.c one.c save.c \
X	subs.c table.c teach.c ttext1.c ttext2.c tutor.c
XTOBJS=	allow.o board.o check.o data.o fancy.o init.o odds.o one.o save.o \
X	subs.o table.o teach.o ttext1.o ttext2.o tutor.o
X
Xall: backgammon teachgammon
X
X# Backgammon program
Xbackgammon: ${BOBJS} ${LIBC}
X	${CC} ${CFLAGS} -o $@ ${BOBJS} ${LIBS}
X
X# Backgammon rules and tutorial
Xteachgammon: ${TOBJS} ${LIBC}
X	${CC} ${CFLAGS} -o $@ ${TOBJS} ${LIBS}
X
Xclean: FRC
X	rm -f ${TOBJS} ${BOBJS} core teachgammon backgammon
X
Xdepend: FRC
X	mkdep ${CFLAGS} ${SRCS}
X
Xinstall: FRC
X	install -s -o games -g bin -m 700 backgammon ${DESTDIR}/usr/games/hide
X	install -s -o games -g bin -m 700 teachgammon ${DESTDIR}/usr/games/hide
X	(cd ${DESTDIR}/usr/games; rm -f backgammon; ln -s dm backgammon; chown games.bin backgammon)
X	(cd ${DESTDIR}/usr/games; rm -f teachgammon; ln -s dm teachgammon; chown games.bin teachgammon)
X
Xlint: FRC
X	lint ${CFLAGS} ${BRCS}
X	lint ${CFLAGS} ${TSRCS}
X
Xtags: FRC
X	ctags ${SRCS}
X
XFRC:
X
X# DO NOT DELETE THIS LINE -- mkdep uses it.
X# DO NOT PUT ANYTHING AFTER THIS LINE, IT WILL GO AWAY.
X
Xallow.o: allow.c back.h /usr/include/sgtty.h /usr/include/sys/ioctl.h
Xallow.o: /usr/include/sys/ttychars.h /usr/include/sys/ttydev.h
Xboard.o: board.c back.h /usr/include/sgtty.h /usr/include/sys/ioctl.h
Xboard.o: /usr/include/sys/ttychars.h /usr/include/sys/ttydev.h
Xcheck.o: check.c back.h /usr/include/sgtty.h /usr/include/sys/ioctl.h
Xcheck.o: /usr/include/sys/ttychars.h /usr/include/sys/ttydev.h
Xdata.o: data.c tutor.h
Xextra.o: extra.c back.h /usr/include/sgtty.h /usr/include/sys/ioctl.h
Xextra.o: /usr/include/sys/ttychars.h /usr/include/sys/ttydev.h
Xfancy.o: fancy.c back.h /usr/include/sgtty.h /usr/include/sys/ioctl.h
Xfancy.o: /usr/include/sys/ttychars.h /usr/include/sys/ttydev.h
Xinit.o: init.c /usr/include/sgtty.h /usr/include/sys/ioctl.h
Xinit.o: /usr/include/sys/ttychars.h /usr/include/sys/ttydev.h
Xmain.o: main.c /usr/include/stdio.h back.h /usr/include/sgtty.h
Xmain.o: /usr/include/sys/ioctl.h /usr/include/sys/ttychars.h
Xmain.o: /usr/include/sys/ttydev.h
Xmove.o: move.c back.h /usr/include/sgtty.h /usr/include/sys/ioctl.h
Xmove.o: /usr/include/sys/ttychars.h /usr/include/sys/ttydev.h
Xodds.o: odds.c back.h /usr/include/sgtty.h /usr/include/sys/ioctl.h
Xodds.o: /usr/include/sys/ttychars.h /usr/include/sys/ttydev.h
Xone.o: one.c back.h /usr/include/sgtty.h /usr/include/sys/ioctl.h
Xone.o: /usr/include/sys/ttychars.h /usr/include/sys/ttydev.h
Xsave.o: save.c back.h /usr/include/sgtty.h /usr/include/sys/ioctl.h
Xsave.o: /usr/include/sys/ttychars.h /usr/include/sys/ttydev.h
Xsubs.o: subs.c /usr/include/stdio.h back.h /usr/include/sgtty.h
Xsubs.o: /usr/include/sys/ioctl.h /usr/include/sys/ttychars.h
Xsubs.o: /usr/include/sys/ttydev.h
Xtable.o: table.c back.h /usr/include/sgtty.h /usr/include/sys/ioctl.h
Xtable.o: /usr/include/sys/ttychars.h /usr/include/sys/ttydev.h
Xteach.o: teach.c back.h /usr/include/sgtty.h /usr/include/sys/ioctl.h
Xteach.o: /usr/include/sys/ttychars.h /usr/include/sys/ttydev.h
Xtext.o: text.c back.h /usr/include/sgtty.h /usr/include/sys/ioctl.h
Xtext.o: /usr/include/sys/ttychars.h /usr/include/sys/ttydev.h
Xttext1.o: ttext1.c back.h /usr/include/sgtty.h /usr/include/sys/ioctl.h
Xttext1.o: /usr/include/sys/ttychars.h /usr/include/sys/ttydev.h
Xttext2.o: ttext2.c back.h /usr/include/sgtty.h /usr/include/sys/ioctl.h
Xttext2.o: /usr/include/sys/ttychars.h /usr/include/sys/ttydev.h
Xtutor.o: tutor.c back.h /usr/include/sgtty.h /usr/include/sys/ioctl.h
Xtutor.o: /usr/include/sys/ttychars.h /usr/include/sys/ttydev.h tutor.h
Xversion.o: version.c
X
X# IF YOU PUT ANYTHING HERE IT WILL GO AWAY
END_OF_Makefile
if test 4644 -ne `wc -c <Makefile`; then
    echo shar: \"Makefile\" unpacked with wrong size!
fi
# end of overwriting check
fi
if test -f back.h -a "${1}" != "-c" ; then 
  echo shar: Will not over-write existing file \"back.h\"
else
echo shar: Extracting \"back.h\" \(3757 characters\)
sed "s/^X//" >back.h <<'END_OF_back.h'
X/*
X * Copyright (c) 1980 Regents of the University of California.
X * All rights reserved.
X *
X * Redistribution and use in source and binary forms are permitted
X * provided that this notice is preserved and that due credit is given
X * to the University of California at Berkeley. The name of the University
X * may not be used to endorse or promote products derived from this
X * software without specific prior written permission. This software
X * is provided ``as is'' without express or implied warranty.
X *
X *	@(#)back.h	5.2 (Berkeley) 2/16/88
X */
X
X#include <sgtty.h>
X
X#define rnum(r)	(random()%r)
X#define D0	dice[0]
X#define D1	dice[1]
X#define swap	{D0 ^= D1; D1 ^= D0; D0 ^= D1; d0 = 1-d0;}
X
X/*
X *
X * Some numerical conventions:
X *
X *	Arrays have white's value in [0], red in [1].
X *	Numeric values which are one color or the other use
X *	-1 for white, 1 for red.
X *	Hence, white will be negative values, red positive one.
X *	This makes a lot of sense since white is going in decending
X *	order around the board, and red is ascending.
X *
X */
X
Xchar	EXEC[];			/* object for main program */
Xchar	TEACH[];		/* object for tutorial program */
X
Xint	pnum;			/* color of player:
X					-1 = white
X					 1 = red
X					 0 = both
X					 2 = not yet init'ed */
Xchar	args[100];		/* args passed to teachgammon and back */
Xint	acnt;			/* length of args */
Xint	aflag;			/* flag to ask for rules or instructions */
Xint	bflag;			/* flag for automatic board printing */
Xint	cflag;			/* case conversion flag */
Xint	hflag;			/* flag for cleaning screen */
Xint	mflag;			/* backgammon flag */
Xint	raflag;			/* 'roll again' flag for recovered game */
Xint	rflag;			/* recovered game flag */
Xint	tflag;			/* cursor addressing flag */
Xint	rfl;			/* saved value of rflag */
Xint	iroll;			/* special flag for inputting rolls */
Xint	board[26];		/* board:  negative values are white,
X				   positive are red */
Xint	dice[2];		/* value of dice */
Xint	mvlim;			/* 'move limit':  max. number of moves */
Xint	mvl;			/* working copy of mvlim */
Xint	p[5];			/* starting position of moves */
Xint	g[5];			/* ending position of moves (goals) */
Xint	h[4];			/* flag for each move if a man was hit */
Xint	cturn;			/* whose turn it currently is:
X					-1 = white
X					 1 = red
X					 0 = just quitted
X					-2 = white just lost
X					 2 = red just lost */
Xint	d0;			/* flag if dice have been reversed from
X				   original position */
Xint	table[6][6];		/* odds table for possible rolls */
Xint	rscore;			/* red's score */
Xint	wscore;			/* white's score */
Xint	gvalue;			/* value of game (64 max.) */
Xint	dlast;			/* who doubled last (0 = neither) */
Xint	bar;			/* position of bar for current player */
Xint	home;			/* position of home for current player */
Xint	off[2];			/* number of men off board */
Xint	*offptr;		/* pointer to off for current player */
Xint	*offopp;		/* pointer to off for opponent */
Xint	in[2];			/* number of men in inner table */
Xint	*inptr;			/* pointer to in for current player */
Xint	*inopp;			/* pointer to in for opponent */
X
Xint	ncin;			/* number of characters in cin */
Xchar	cin[100];		/* input line of current move
X				   (used for reconstructing input after
X				   a backspace) */
X
Xchar	*color[];
X				/* colors as strings */
Xchar	**colorptr;		/* color of current player */
Xchar	**Colorptr;		/* color of current player, capitalized */
Xint	colen;			/* length of color of current player */
X
Xstruct sgttyb	tty;		/* tty information buffer */
Xint		old;		/* original tty status */
Xint		noech;		/* original tty status without echo */
Xint		raw;		/* raw tty status, no echo */
X
Xint	curr;			/* row position of cursor */
Xint	curc;			/* column position of cursor */
Xint	begscr;			/* 'beginning' of screen
X				   (not including board) */
X
Xint	getout();		/* function to exit backgammon cleanly */
END_OF_back.h
if test 3757 -ne `wc -c <back.h`; then
    echo shar: \"back.h\" unpacked with wrong size!
fi
# end of overwriting check
fi
if test -f backgammon.1 -a "${1}" != "-c" ; then 
  echo shar: Will not over-write existing file \"backgammon.1\"
else
echo shar: Extracting \"backgammon.1\" \(3707 characters\)
sed "s/^X//" >backgammon.1 <<'END_OF_backgammon.1'
X.\" Copyright (c) 1980 Regents of the University of California.
X.\" All rights reserved.
X.\"
X.\" Redistribution and use in source and binary forms are permitted
X.\" provided that this notice is preserved and that due credit is given
X.\" to the University of California at Berkeley. The name of the University
X.\" may not be used to endorse or promote products derived from this
X.\" software without specific prior written permission. This software
X.\" is provided ``as is'' without express or implied warranty.
X.\"
X.\"	@(#)backgammon.6	6.3 (Berkeley) 2/26/88
X.\"
X.TH BACKGAMMON 6  "February 26, 1988"
X.UC 5
X.SH NAME
Xbackgammon \- the game of backgammon
X.SH SYNOPSIS
X.B backgammon
X[ - ] [ n r w b pr pw pb t\c
X.I term
Xs\c
X.I file
X]
X.SH DESCRIPTION
XThis program lets you play backgammon against the computer
Xor against a "friend".
XAll commands only are one letter,
Xso you don't need to type a carriage return,
Xexcept at the end of a move.
XThe program is mostly self documenting,
Xso that a question mark (?) will usually get some help.
XIf you answer `y' when the program asks if you want the rules,
Xyou will get text explaining the rules of the game,
Xsome hints on strategy,
Xinstruction on how to use the program,
Xand a tutorial consisting of a practice game against the computer.
XA description of how to use the program can be
Xobtained by answering `y' when it asks if you want instructions.
X.PP
XThe possible arguments for backgammon
X(most are unnecessary but some are very convenient)
Xconsist of:
X.ne 11
X.PP
X.na
X.TP 8
X.B n
Xdon't ask for rules or instructions
X.TP 8
X.B r
Xplayer is red (implies n)
X.TP 8
X.B w
Xplayer is white (implies n)
X.TP 8
X.B b
Xtwo players, red and white (implies n)
X.TP 8
X.B pr
Xprint the board before red's turn
X.TP 8
X.B pw
Xprint the board before white's turn
X.TP 8
X.B pb
Xprint the board before both player's turn
X.TP 8
X.B t\fiterm
Xterminal is type
X.IR term ,
Xuses /etc/termcap
X.TP 8
X.B s\fifile
Xrecover previously saved game from
X.IR file .
X(This can also be done by executing the saved file,
Xi.e., typing its name in as a command)
X.ad
X.PP
XArguments may be optionally preceded by a `-'.
XSeveral arguments may be concatenated together,
Xbut not after `s' or `t' arguments,
Xsince they can be followed by an arbitrary string.
XAny unrecognized arguments are ignored.
XAn argument of a lone `-' gets a description of possible arguments.
X.PP
XIf
X.IR term
Xhas capabilities for direct cursor movement (see
X.IR termcap (5))
X.IR backgammon
X``fixes'' the board after each move,
Xso the board does not need to be reprinted,
Xunless the screen suffers some horrendous malady.
XAlso, any `p' option will be ignored.
X(The `t' option is not necessary unless the terminal type does not match
Xthe entry in the /etc/termcap data base.)
X.SH QUICK\ REFERENCE
XWhen the program prompts by typing only your color,
Xtype a space or carriage return to roll, or
X.ne 5
X.PP
X.na
X.TP 8
X.B d
Xto double
X.TP 8
X.B p
Xto print the board
X.TP 8
X.B q
Xto quit
X.TP 8
X.B s
Xto save the game for later
X.PP
X.i0
X.ad
XWhen the program prompts with 'Move:', type
X.ne 4
X.PP
X.na
X.TP 8
X.B p
Xto print the board
X.TP 8
X.B q
Xto quit
X.TP 8
X.B s
Xto save the game
X.ad
X.i0
X.PP
Xor a
X.IR move ,
Xwhich is a sequence of
X.ne 4
X.PP
X.na
X.TP 8
X.B s-f
Xmove from
X.BR s
Xto
X.BR f
X.TP 8
X.B s/r
Xmove one man on
X.BR s
Xthe roll
X.BR r
X.ad
X.PP
Xseparated by commas or spaces and ending with a newline.
XAvailable abbreviations are
X.ne 4
X.PP
X.na
X.TP 10
X.B s-f1-f2
Xmeans
X.BR s-f1,f1-f2
X.TP 10
X.B s/r1r2
Xmeans
X.BR s/r1,s/r2
X.ad
X.PP
XUse `b' for bar and `h' for home,
Xor 0 or 25 as appropriate.
X.SH AUTHOR
XAlan Char
X.SH FILES
X.TP 25
X/usr/games/teachgammon
X\- rules and tutorial
X.br
X.TP 25
X/etc/termcap
X\- terminal capabilities
X.SH BUGS
X.PP
XThe program's strategy needs much work.
X
END_OF_backgammon.1
if test 3707 -ne `wc -c <backgammon.1`; then
    echo shar: \"backgammon.1\" unpacked with wrong size!
fi
# end of overwriting check
fi
if test -f extra.c -a "${1}" != "-c" ; then 
  echo shar: Will not over-write existing file \"extra.c\"
else
echo shar: Extracting \"extra.c\" \(4833 characters\)
sed "s/^X//" >extra.c <<'END_OF_extra.c'
X/*
X * Copyright (c) 1980 Regents of the University of California.
X * All rights reserved.
X *
X * Redistribution and use in source and binary forms are permitted
X * provided that this notice is preserved and that due credit is given
X * to the University of California at Berkeley. The name of the University
X * may not be used to endorse or promote products derived from this
X * software without specific prior written permission. This software
X * is provided ``as is'' without express or implied warranty.
X */
X
X#ifndef lint
Xstatic char sccsid[] = "@(#)extra.c	5.2 (Berkeley) 2/16/88";
X#endif /* not lint */
X
X#include "back.h"
X
X#ifdef DEBUG
X#include <stdio.h>
XFILE	*trace;
X#endif
X
X/*
X * dble()
X *	Have the current player double and ask opponent to accept.
X */
X
Xdble ()  {
X	register int	resp;			/* response to y/n */
X
X	for (;;)  {
X		writel (" doubles.");		/* indicate double */
X
X		if (cturn == -pnum)  {		/* see if computer accepts */
X			if (dblgood())  {	    /* guess not */
X				writel ("  Declined.\n");
X				nexturn();
X				cturn *= -2;	    /* indicate loss */
X				return;
X			} else  {		    /* computer accepts */
X				writel ("  Accepted.\n");
X				gvalue *= 2;	    /* double game value */
X				dlast = cturn;
X				if (tflag)
X					gwrite();
X				return;
X			}
X		}
X
X						/* ask if player accepts */
X		writel ("  Does ");
X		writel (cturn == 1? color[2]: color[3]);
X		writel (" accept?");
X
X						/* get response from yorn,
X						 * a "2" means he said "p"
X						 * for print board. */
X		if ((resp = yorn ('R')) == 2)  {
X			writel ("  Reprint.\n");
X			buflush();
X			wrboard();
X			writel (*Colorptr);
X			continue;
X		}
X
X						/* check response */
X		if (resp)  {
X						    /* accepted */
X			gvalue *= 2;
X			dlast = cturn;
X			if (tflag)
X				gwrite();
X			return;
X		}
X
X		nexturn ();			/* declined */
X		cturn *= -2;
X		return;
X	}
X}
X
X/*
X * dblgood ()
X *	Returns 1 if the computer would double in this position.  This
X * is not an exact science.  The computer will decline a double that he
X * would have made.  Accumulated judgments are kept in the variable n,
X * which is in "pips", i.e., the position of each man summed over all
X * men, with opponent's totals negative.  Thus, n should have a positive
X * value of 7 for each move ahead, or a negative value of 7 for each one
X * behind.
X */
X
Xdblgood ()  {
X	register int	n;			/* accumulated judgment */
X	register int	OFFC = *offptr;		/* no. of computer's men off */
X	register int	OFFO = *offopp;		/* no. of player's men off */
X
X#ifdef DEBUG
X	register int	i;
X	if (trace == NULL)
X		trace = fopen ("bgtrace","w");
X#endif
X
X						/* get real pip value */
X	n = eval()*cturn;
X#ifdef DEBUG
X	fputs ("\nDoubles:\nBoard: ",trace);
X	for (i = 0; i < 26; i++)
X		fprintf (trace," %d",board[i]);
X	fprintf (trace,"\n\tpip = %d, ",n);
X#endif
X
X						/* below adjusts pip value
X						 * according to position
X						 * judgments */
X
X						/* check men moving off
X						 * board */
X	if (OFFC > -15 || OFFO > -15)  {
X		if (OFFC < 0 && OFFO < 0)  {
X			OFFC += 15;
X			OFFO += 15;
X			n +=((OFFC-OFFO)*7)/2;
X		} else if (OFFC < 0)  {
X			OFFC += 15;
X			n -= OFFO*7/2;
X		} else if (OFFO < 0)  {
X			OFFO += 15;
X			n += OFFC*7/2;
X		}
X		if (OFFC < 8 && OFFO > 8)
X			n -= 7;
X		if (OFFC < 10 && OFFO > 10)
X			n -= 7;
X		if (OFFC < 12 && OFFO > 12)
X			n -= 7;
X		if (OFFO < 8 && OFFC > 8)
X			n += 7;
X		if (OFFO < 10 && OFFC > 10)
X			n += 7;
X		if (OFFO < 12 && OFFC > 12)
X			n += 7;
X		n += ((OFFC-OFFO)*7)/2;
X	}
X
X#ifdef DEBUG
X	fprintf (trace,"off = %d, ",n);
X#endif
X
X						/* see if men are trapped */
X	n -= freemen(bar);
X	n += freemen(home);
X	n += trapped(home,-cturn);
X	n -= trapped(bar,cturn);
X
X#ifdef DEBUG
X	fprintf (trace,"free = %d\n",n);
X	fprintf (trace,"\tOFFC = %d, OFFO = %d\n",OFFC,OFFO);
X	fflush (trace);
X#endif
X
X						/* double if 2-3 moves ahead */
X	if (n > 10+rnum(7))
X		return(1);
X	return (0);
X}
X
Xfreemen (b)
Xint	b;
X
X{
X	register int	i, inc, lim;
X
X	odds(0,0,0);
X	if (board[b] == 0)
X		return (0);
X	inc = (b == 0? 1: -1);
X	lim = (b == 0? 7: 18);
X	for (i = b+inc; i != lim; i += inc)
X		if (board[i]*inc < -1)
X			odds(abs(b-i),0,abs(board[b]));
X	if (abs(board[b]) == 1)
X		return ((36-count())/5);
X	return (count()/5);
X}
X
Xtrapped (n,inc)
Xint	n, inc;
X
X{
X	register int	i, j, k;
X	int		c, l, ct;
X
X	ct = 0;
X	l = n+7*inc;
X	for (i = n+inc; i != l; i += inc)  {
X		odds (0,0,0);
X		c = abs(i-l);
X		if (board[i]*inc > 0)  {
X			for (j = c; j < 13; j++)
X				if (board[i+inc*j]*inc < -1)  {
X					if (j < 7)
X						odds (j,0,1);
X					for (k = 1; k < 7 && k < j; k++)
X						if (j-k < 7)
X							odds (k,j-k,1);
X				}
X			ct += abs(board[i])*(36-count());
X		}
X	}
X	return (ct/5);
X}
X
Xeval ()  {
X
X	register int	i, j;
X
X	for (j = i = 0; i < 26; i++)
X		j += (board[i] >= 0 ? i*board[i] : (25-i)*board[i]);
X
X	if (off[1] >= 0)
X		j += 25*off[1];
X	else
X		j += 25*(off[1]+15);
X
X	if (off[0] >= 0)
X		j -= 25*off[0];
X	else
X		j -= 25*(off[0]+15);
X	return (j);
X}
END_OF_extra.c
if test 4833 -ne `wc -c <extra.c`; then
    echo shar: \"extra.c\" unpacked with wrong size!
fi
# end of overwriting check
fi
if test -f subs.c -a "${1}" != "-c" ; then 
  echo shar: Will not over-write existing file \"subs.c\"
else
echo shar: Extracting \"subs.c\" \(7449 characters\)
sed "s/^X//" >subs.c <<'END_OF_subs.c'
X/*
X * Copyright (c) 1980 Regents of the University of California.
X * All rights reserved.
X *
X * Redistribution and use in source and binary forms are permitted
X * provided that this notice is preserved and that due credit is given
X * to the University of California at Berkeley. The name of the University
X * may not be used to endorse or promote products derived from this
X * software without specific prior written permission. This software
X * is provided ``as is'' without express or implied warranty.
X */
X
X#ifndef lint
Xstatic char sccsid[] = "@(#)subs.c	5.3 (Berkeley) 2/16/88";
X#endif /* not lint */
X
X#include <stdio.h>
X#include "back.h"
X
Xint	buffnum;
Xchar	outbuff[BUFSIZ];
X
Xstatic char	plred[] = "Player is red, computer is white.";
Xstatic char	plwhite[] = "Player is white, computer is red.";
Xstatic char	nocomp[] = "(No computer play.)";
X
Xchar  *descr[] = {
X	"Usage:  backgammon [-] [n r w b pr pw pb t3a]\n",
X	"\t-\tgets this list\n\tn\tdon't ask for rules or instructions",
X	"\tr\tplayer is red (implies n)\n\tw\tplayer is white (implies n)",
X	"\tb\ttwo players, red and white (implies n)",
X	"\tpr\tprint the board before red's turn",
X	"\tpw\tprint the board before white's turn",
X	"\tpb\tprint the board before both player's turn",
X	"\tterm\tterminal is a term",
X	"\tsfile\trecover saved game from file",
X	0
X};
X
Xerrexit (s)
Xregister char	*s;
X{
X	write (2,"\n",1);
X	perror (s);
X	getout();
X}
X
Xstrset (s1,s2)
Xregister char	*s1, *s2;
X{
X	while ( (*s1++ = *s2++) != '\0');
X}
X
Xaddbuf (c)
Xregister char	c;
X
X{
X	buffnum++;
X	if (buffnum == BUFSIZ)  {
X		if (write(1,outbuff,BUFSIZ) != BUFSIZ)
X			errexit ("addbuf (write):");
X		buffnum = 0;
X	}
X	outbuff[buffnum] = c;
X}
X
Xbuflush ()  {
X	if (buffnum < 0)
X		return;
X	buffnum++;
X	if (write (1,outbuff,buffnum) != buffnum)
X		errexit ("buflush (write):");
X	buffnum = -1;
X}
X
Xreadc () {
X	char	c;
X
X	if (tflag)  {
X		cline();
X		newpos();
X	}
X	buflush();
X	if (read(0,&c,1) != 1)
X		errexit ("readc");
X#ifdef WHY_IS_THIS_HARDWIRED_IN_HERE
X	if (c == '\177')
X		getout();
X#endif
X	if (c == '\033' || c == '\015')
X		return ('\n');
X	if (cflag)
X		return (c);
X	if (c == '\014')
X		return ('R');
X	if (c >= 'a' && c <= 'z')
X		return (c & 0137);
X	return (c);
X}
X
Xwritec (c)
Xchar	c;
X{
X	if (tflag)
X		fancyc (c);
X	else
X		addbuf (c);
X}
X
Xwritel (l)
Xregister char	*l;
X{
X#ifdef DEBUG
X	register char	*s;
X
X	if (trace == NULL)
X		trace = fopen ("bgtrace","w");
X	
X	fprintf (trace,"writel: \"");
X	for (s = l; *s; s++) {
X		if (*s < ' ' || *s == '\177')
X			fprintf (trace,"^%c",(*s)^0100);
X		else
X			putc (*s,trace);
X	}
X	fprintf (trace,"\"\n");
X	fflush (trace);
X#endif
X
X	while (*l)
X		writec (*l++);
X}
X
Xproll ()   {
X	if (d0)
X		swap;
X	if (cturn == 1)
X		writel ("Red's roll:  ");
X	else
X		writel ("White's roll:  ");
X	writec (D0+'0');
X	writec ('\040');
X	writec (D1+'0');
X	if (tflag)
X		cline();
X}
X
Xwrint (n)
Xint	n;
X{
X	register int	i, j, t;
X
X	for (i = 4; i > 0; i--)  {
X		t = 1;
X		for (j = 0; j<i; j++)
X			t *= 10;
X		if (n > t-1)
X			writec ((n/t)%10+'0');
X	}
X	writec (n%10+'0');
X}
X
Xgwrite()  {
X	register int	r, c;
X
X	if (tflag)  {
X		r = curr;
X		c = curc;
X		curmove (16,0);
X	}
X
X	if (gvalue > 1)  {
X		writel ("Game value:  ");
X		wrint (gvalue);
X		writel (".  ");
X		if (dlast == -1)
X			writel (color[0]);
X		else
X			writel (color[1]);
X		writel (" doubled last.");
X	} else  {
X		switch (pnum)  {
X		case -1:			    /* player is red */
X			writel (plred);
X			break;
X		case 0:				    /* player is both colors */
X			writel (nocomp);
X			break;
X		case 1:				    /* player is white */
X			writel (plwhite);
X		}
X	}
X
X	if (rscore || wscore)  {
X		writel ("  ");
X		wrscore();
X	}
X
X	if (tflag)  {
X		cline();
X		curmove (r,c);
X	}
X}
X
Xquit ()  {
X	register int	i;
X
X	if (tflag)  {
X		curmove (20,0);
X		clend();
X	} else
X		writec ('\n');
X	writel ("Are you sure you want to quit?");
X	if (yorn (0))  {
X		if (rfl)  {
X			writel ("Would you like to save this game?");
X			if (yorn(0))
X				save(0);
X		}
X		cturn = 0;
X		return (1);
X	}
X	return (0);
X}
X
Xyorn (special)
Xregister char	special;			/* special response */
X{
X	register char	c;
X	register int	i;
X
X	i = 1;
X	while ( (c = readc()) != 'Y' && c != 'N')  {
X		if (special && c == special)
X			return (2);
X		if (i)  {
X			if (special)  {
X				writel ("  (Y, N, or ");
X				writec (special);
X				writec (')');
X			} else
X				writel ("  (Y or N)");
X			i = 0;
X		} else
X			writec ('\007');
X	}
X	if (c == 'Y')
X		writel ("  Yes.\n");
X	else
X		writel ("  No.\n");
X	if (tflag)
X		buflush();
X	return (c == 'Y');
X}
X
Xwrhit (i)
Xregister int	i;
X{
X	writel ("Blot hit on ");
X	wrint (i);
X	writec ('.');
X	writec ('\n');
X}
X
Xnexturn ()  {
X	register int	c;
X
X	cturn = -cturn;
X	c = cturn/abs(cturn);
X	home = bar;
X	bar = 25-bar;
X	offptr += c;
X	offopp -= c;
X	inptr += c;
X	inopp -= c;
X	Colorptr += c;
X	colorptr += c;
X}
X
Xgetarg (arg)
Xregister char	***arg;
X
X{
X	register char	**s;
X
X	/* process arguments here.  dashes are ignored, nbrw are ignored
X	   if the game is being recovered */
X
X	s = *arg;
X	while (s[0][0] == '-') {
X		switch (s[0][1])  {
X
X		/* don't ask if rules or instructions needed */
X		case 'n':
X			if (rflag)
X				break;
X			aflag = 0;
X			args[acnt++] = 'n';
X			break;
X
X		/* player is both read and white */
X		case 'b':
X			if (rflag)
X				break;
X			pnum = 0;
X			aflag = 0;
X			args[acnt++] = 'b';
X			break;
X
X		/* player is red */
X		case 'r':
X			if (rflag)
X				break;
X			pnum = -1;
X			aflag = 0;
X			args[acnt++] = 'r';
X			break;
X
X		/* player is white */
X		case 'w':
X			if (rflag)
X				break;
X			pnum = 1;
X			aflag = 0;
X			args[acnt++] = 'w';
X			break;
X
X		/* print board after move according to following character */
X		case 'p':
X			if (s[0][2] != 'r' && s[0][2] != 'w' && s[0][2] != 'b')
X				break;
X			args[acnt++] = 'p';
X			args[acnt++] = s[0][2];
X			if (s[0][2] == 'r')
X				bflag = 1;
X			if (s[0][2] == 'w')
X				bflag = -1;
X			if (s[0][2] == 'b')
X				bflag = 0;
X			break;
X
X		case 't':
X			if (s[0][2] == '\0') {	/* get terminal caps */
X				s++;
X				tflag = getcaps (*s);
X			} else
X				tflag = getcaps (&s[0][2]);
X			break;
X
X		case 's':
X			s++;
X			/* recover file */
X			recover (s[0]);
X			break;
X		}
X		s++;
X	}
X	if (s[0] != 0)
X		recover(s[0]);
X}
X
Xinit ()  {
X	register int	i;
X	for (i = 0; i < 26;)
X		board[i++] = 0;
X	board[1] = 2;
X	board[6] = board[13] = -5;
X	board[8] = -3;
X	board[12] = board[19] = 5;
X	board[17] = 3;
X	board[24] = -2;
X	off[0] = off[1] = -15;
X	in[0] = in[1] = 5;
X	gvalue = 1;
X	dlast = 0;
X}
X
Xwrscore ()  {
X	writel ("Score:  ");
X	writel (color[1]);
X	writec (' ');
X	wrint (rscore);
X	writel (", ");
X	writel (color[0]);
X	writec (' ');
X	wrint (wscore);
X}
X
Xfixtty (mode)
Xint	mode;
X{
X	if (tflag)
X		newpos();
X	buflush();
X	tty.sg_flags = mode;
X	if (stty (0,&tty) < 0) {
X		perror("fixtty");
X		exit(1);
X	}
X}
X
Xgetout ()  {
X	/* go to bottom of screen */
X	if (tflag)  {
X		curmove (23,0);
X		cline();
X	} else
X		writec ('\n');
X
X	/* fix terminal status */
X	fixtty (old);
X	exit(1);
X}
Xroll ()  {
X	register char	c;
X	register int	row;
X	register int	col;
X
X	if (iroll)  {
X		if (tflag)  {
X			row = curr;
X			col = curc;
X			curmove (17,0);
X		} else
X			writec ('\n');
X		writel ("ROLL: ");
X		c = readc();
X		if (c != '\n')  {
X			while (c < '1' || c > '6')
X				c = readc();
X			D0 = c-'0';
X			writec (' ');
X			writec (c);
X			c = readc();
X			while (c < '1' || c > '6')
X				c = readc();
X			D1 = c-'0';
X			writec (' ');
X			writec (c);
X			if (tflag)  {
X				curmove (17,0);
X				cline();
X				curmove (row,col);
X			} else
X				writec ('\n');
X			return;
X		}
X		if (tflag)  {
X			curmove (17,0);
X			cline();
X			curmove (row,col);
X		} else
X			writec ('\n');
X	}
X	D0 = rnum(6)+1;
X	D1 = rnum(6)+1;
X	d0 = 0;
X}
END_OF_subs.c
if test 7449 -ne `wc -c <subs.c`; then
    echo shar: \"subs.c\" unpacked with wrong size!
fi
# end of overwriting check
fi
if test -f table.c -a "${1}" != "-c" ; then 
  echo shar: Will not over-write existing file \"table.c\"
else
echo shar: Extracting \"table.c\" \(5101 characters\)
sed "s/^X//" >table.c <<'END_OF_table.c'
X/*
X * Copyright (c) 1980 Regents of the University of California.
X * All rights reserved.
X *
X * Redistribution and use in source and binary forms are permitted
X * provided that this notice is preserved and that due credit is given
X * to the University of California at Berkeley. The name of the University
X * may not be used to endorse or promote products derived from this
X * software without specific prior written permission. This software
X * is provided ``as is'' without express or implied warranty.
X */
X
X#ifndef lint
Xstatic char sccsid[] = "@(#)table.c	5.2 (Berkeley) 2/16/88";
X#endif /* not lint */
X
X#include "back.h"
X
Xchar	*help2[] = {
X	"   Enter moves as <s>-<f> or <s>/<r> where <s> is the starting",
X	"position, <f> is the finishing position, and <r> is the roll.",
X	"Remember, each die roll must be moved separately.",
X	0
X};
X
Xstruct state	{
X	char	ch;
X	int	fcode;
X	int	newst;
X};
X
Xstruct state	atmata[] = {
X
X	'R', 1, 0,	'?', 7, 0,	'Q', 0, -3,	'B', 8, 25,
X	'9', 2, 25,	'8', 2, 25,	'7', 2, 25,	'6', 2, 25,
X	'5', 2, 25,	'4', 2, 25,	'3', 2, 25,	'2', 2, 19,
X	'1', 2, 15,	'0', 2, 25,	'.', 0, 0,	'9', 2, 25,
X	'8', 2, 25,	'7', 2, 25,	'6', 2, 25,	'5', 2, 25,
X
X	'4', 2, 25,	'3', 2, 25,	'2', 2, 25,	'1', 2, 25,
X	'0', 2, 25,	'/', 0, 32,	'-', 0, 39,	'.', 0, 0,
X	'/', 5, 32,	' ', 6, 3,	',', 6, 3,	'\n', 0, -1,
X	'6', 3, 28,	'5', 3, 28,	'4', 3, 28,	'3', 3, 28,
X	'2', 3, 28,	'1', 3, 28,	'.', 0, 0,	'H', 9, 61,
X
X	'9', 4, 61,	'8', 4, 61,	'7', 4, 61,	'6', 4, 61,
X	'5', 4, 61,	'4', 4, 61,	'3', 4, 61,	'2', 4, 53,
X	'1', 4, 51,	'0', 4, 61,	'.', 0, 0,	'9', 4, 61,
X	'8', 4, 61,	'7', 4, 61,	'6', 4, 61,	'5', 4, 61,
X	'4', 4, 61,	'3', 4, 61,	'2', 4, 61,	'1', 4, 61,
X
X	'0', 4, 61,	' ', 6, 3,	',', 6, 3,	'-', 5, 39,
X	'\n', 0, -1,	'.', 0, 0
X};
X
Xcheckmove (ist)
X
Xint	ist;
X
X{
X	register int	j, n;
X	register char	c;
X	char		a;
X
Xdomove:
X	if (ist == 0)  {
X		if (tflag)
X			curmove (curr,32);
X		else
X			writel ("\t\t");
X		writel ("Move:  ");
X	}
X	ist = mvl = ncin = 0;
X	for (j = 0; j < 5; j++)
X		p[j] = g[j] = -1;
X
Xdochar:
X	c = readc();
X
X	if (c == 'S')  {
X		raflag = 0;
X		save (1);
X		if (tflag)  {
X			curmove (cturn == -1? 18: 19,39);
X			ist = -1;
X			goto domove;
X		} else  {
X			proll ();
X			ist = 0;
X			goto domove;
X		}
X	}
X
X	if (c == tty.sg_erase && ncin > 0)  {
X		if (tflag)
X			curmove (curr,curc-1);
X		else  {
X			if (tty.sg_erase == '\010')
X				writel ("\010 \010");
X			else
X				writec (cin[ncin-1]);
X		}
X		ncin--;
X		n = rsetbrd();
X		if (n == 0)  {
X			n = -1;
X			if (tflag)
X				refresh();
X		}
X		if ((ist = n) > 0)
X			goto dochar;
X		goto domove;
X	}
X
X	if (c == tty.sg_kill && ncin > 0)  {
X		if (tflag)  {
X			refresh();
X			curmove (curr,39);
X			ist = -1;
X			goto domove;
X		} else  if (tty.sg_erase == '\010')  {
X			for (j = 0; j < ncin; j++)
X				writel ("\010 \010");
X			ist = -1;
X			goto domove;
X		} else  {
X			writec ('\\');
X			writec ('\n');
X			proll ();
X			ist = 0;
X			goto domove;
X		}
X	}
X
X	n = dotable(c,ist);
X	if (n >= 0)  {
X		cin[ncin++] = c;
X		if (n > 2)
X		if ((! tflag) || c != '\n')
X			writec (c);
X		ist = n;
X		if (n)
X			goto dochar;
X		else
X			goto domove;
X	}
X
X	if (n == -1 && mvl >= mvlim)
X		return(0);
X	if (n == -1 && mvl < mvlim-1)
X		return(-4);
X
X	if (n == -6)  {
X		if (! tflag)  {
X			if (movokay(mvl+1))  {
X				wrboard();
X				movback (mvl+1);
X			}
X			proll ();
X			writel ("\t\tMove:  ");
X			for (j = 0; j < ncin;)
X				writec (cin[j++]);
X		} else  {
X			if (movokay(mvl+1))  {
X				refresh();
X				movback (mvl+1);
X			} else
X				curmove (cturn == -1? 18:19,ncin+39);
X		}
X		ist = n = rsetbrd();
X		goto dochar;
X	}
X
X	if (n != -5)
X		return(n);
X	writec ('\007');
X	goto dochar;
X}
X
Xdotable (c,i)
Xchar		c;
Xregister int	i;
X
X{
X	register int	a, j;
X	int		test;
X
X	test = (c == 'R');
X
X	while ( (a = atmata[i].ch) != '.')  {
X		if (a == c || (test && a == '\n'))  {
X			switch  (atmata[i].fcode)  {
X
X			case 1:
X				wrboard();
X				if (tflag)  {
X					curmove (cturn == -1? 18: 19,0);
X					proll ();
X					writel ("\t\t");
X				} else
X					proll ();
X				break;
X
X			case 2:
X				if (p[mvl] == -1)
X					p[mvl] = c-'0';
X				else
X					p[mvl] = p[mvl]*10+c-'0';
X				break;
X
X			case 3:
X				if (g[mvl] != -1)  {
X					if (mvl < mvlim)
X						mvl++;
X					p[mvl] = p[mvl-1];
X				}
X				g[mvl] = p[mvl]+cturn*(c-'0');
X				if (g[mvl] < 0)
X					g[mvl] = 0;
X				if (g[mvl] > 25)
X					g[mvl] = 25;
X				break;
X
X			case 4:
X				if (g[mvl] == -1)
X					g[mvl] = c-'0';
X				else
X					g[mvl] = g[mvl]*10+c-'0';
X				break;
X
X			case 5:
X				if (mvl < mvlim)
X					mvl++;
X				p[mvl] = g[mvl-1];
X				break;
X
X			case 6:
X				if (mvl < mvlim)
X					mvl++;
X				break;
X
X			case 7:
X				if (tflag)
X					curmove (20,0);
X				else
X					writec ('\n');
X				text (help2);
X				if (tflag)  {
X					curmove (cturn == -1? 18: 19,39);
X				} else  {
X					writec ('\n');
X					proll();
X					writel ("\t\tMove:  ");
X				}
X				break;
X
X			case 8:
X				p[mvl] = bar;
X				break;
X
X			case 9:
X				g[mvl] = home;
X			}
X
X			if (! test || a != '\n')
X				return (atmata[i].newst);
X			else
X				return (-6);
X		}
X
X		i++;
X	}
X
X	return (-5);
X}
X
Xrsetbrd ()  {
X	register int	i, j, n;
X
X	n = 0;
X	mvl = 0;
X	for (i = 0; i < 4; i++)
X		p[i] = g[i] = -1;
X	for (j = 0; j < ncin; j++)
X		n = dotable (cin[j],n);
X	return (n);
X}
END_OF_table.c
if test 5101 -ne `wc -c <table.c`; then
    echo shar: \"table.c\" unpacked with wrong size!
fi
# end of overwriting check
fi
if test -f text.c -a "${1}" != "-c" ; then 
  echo shar: Will not over-write existing file \"text.c\"
else
echo shar: Extracting \"text.c\" \(5320 characters\)
sed "s/^X//" >text.c <<'END_OF_text.c'
X/*
X * Copyright (c) 1980 Regents of the University of California.
X * All rights reserved.
X *
X * Redistribution and use in source and binary forms are permitted
X * provided that this notice is preserved and that due credit is given
X * to the University of California at Berkeley. The name of the University
X * may not be used to endorse or promote products derived from this
X * software without specific prior written permission. This software
X * is provided ``as is'' without express or implied warranty.
X */
X
X#ifndef lint
Xstatic char sccsid[] = "@(#)text.c	5.2 (Berkeley) 2/16/88";
X#endif /* not lint */
X
X#include "back.h"
X
Xchar *instr[] = {
X    "    If you did not notice by now, this program reacts to things as",
X    "soon as you type them, without waiting for a newline.  This means that",
X    "the special characters RUBOUT, ESC, and CONTROL-D, will not perform",
X    "their special functions during most of this program.  The program",
X    "should usually stop when a RUBOUT is typed, but occasionally it will",
X    "ignore RUBOUTs until it is waiting for input.\n",
X    "    These instructions are presented in small chunks designed not to",
X    "roll off the top of your screen.  When the characters '-->' are print-",
X    "ed, no more data will be printed until a space or newline is typed.",
X    "In this way, you can finish one section before continuing to another.",
X    "Like this:",
X    "",
X    "    The two sides are colored 'red' and 'white.' The computer may play",
X    "one side, or if there are two players, the computer can merely act as",
X    "a gamekeeper, letting the players make the moves.  Once you tell the",
X    "computer what color(s) you want to play, the decision remains in ef-",
X    "fect until you quit the program, even if you play more than one game,",
X    "since the program keeps a running score.\n",
X    "    The program will prompt for a move in one of two ways.  If the",
X    "player has the opportunity to double, then merely his color will be",
X    "typed out.  The player can now do one of several things.  He can dou-",
X    "ble by typing a 'd', he can roll by typing a space (' ') or newline,",
X    "or if he is not sure, he can reprint the board by typing a 'r'.\n",
X    "    If the player cannot double, his roll will be thrust in front of",
X    "him, followed by the request 'Move:', asking for a move but not giving",
X    "him the chance to double.  He can still ask for the board by typing",
X    "'r'.  In either of these two states, the player can quit by typing 'q'",
X    "or save the game by typing 's'.  In either case, the player will be",
X    "asked to verify, in case there was some error.  The program then ends",
X    "immediately, after first saving the file if so requested.",
X    "",
X    "    A player can move one of his men using two forms of input.  The",
X    "first form is <s>-<f>, where <s> is the starting position, and <f> is",
X    "the finishing position of the player's man.  For example, if white",
X    "wanted to move a piece from position 13 to position 8, his move could",
X    "be entered as 13-8.  The second form is <s>/<r> where <s> is the",
X    "starting position, an <r> is the roll actually made.  Hence, white",
X    "could have entered as 13/5 instead of 13-8.\n",
X    "    A player must move each roll of the dice separately.  For example,",
X    "if a player rolled 4 3, and wanted to move from 13 to 6, he could",
X    "enter it as 13/4,9/3 or 13/3,10/4 or 13-10,10-6 or 13-9,9-6, but not",
X    "13-6.  The last two entries can be shortened to 13-10-6 and 13-9-6.",
X    "If you want to move more than one piece from the same position, such",
X    "as 13-10,13-9, you can abbreviate this using the <s>/<r> format as by",
X    "entering more than one <r>, or 13/34.  A player can use both forms for",
X    "the same roll, e.g. 13/3,13-9, and separates individual moves by ei-",
X    "ther a comma or a space.  The letter 'b' represents the bar, and the",
X    "letter 'h' represents a player's home.  You could also enter the",
X    "number that would be in the position of the bar, 25 or 0 as appropri-",
X    "ate.  Use a newline at the end of your moves for a turn.",
X    "",
X    "    As you type in your move, if a character does not make sense under",
X    "the above constrictions, a bell will sound instead of the character,",
X    "and it will be ignored.  You may kill lines and erase characters as",
X    "you would normally, but don't be surprised if they look different than",
X    "usual.  Also, if you have entered one or more of your rolls, and you",
X    "wish to see what the move looks like so far, type a 'r' to see what it",
X    "looks like.  This cannot be done in the middle of a move (e.g., after",
X    "a '-' or '/').  After the printing board, the program will go back to",
X    "inputting your move and you can backspace or kill just as if you had",
X    "just typed in your input.\n",
X    "    Now you should be ready to begin the game.  Good luck!",
X    "",
X    0};
X
X
Xtext (t)
Xchar	**t;
X
X{
X	register int	i;
X	register char	*s, *a;
X
X	fixtty (noech);
X	while (*t != 0)  {
X		s = a = *t;
X		for (i = 0; *a != '\0'; i--)
X			a++;
X		if (i)  {
X			writel (s);
X			writec ('\n');
X		} else  {
X			writel ("-->");
X			fixtty (raw);
X			while ((i = readc()) != ' ' && i != '\n');
X			fixtty (noech);
X			clear();
X		}
X		t++;
X	}
X	fixtty (raw);
X}
END_OF_text.c
if test 5320 -ne `wc -c <text.c`; then
    echo shar: \"text.c\" unpacked with wrong size!
fi
# end of overwriting check
fi
if test -f ttext1.c -a "${1}" != "-c" ; then 
  echo shar: Will not over-write existing file \"ttext1.c\"
else
echo shar: Extracting \"ttext1.c\" \(7717 characters\)
sed "s/^X//" >ttext1.c <<'END_OF_ttext1.c'
X/*
X * Copyright (c) 1980 Regents of the University of California.
X * All rights reserved.
X *
X * Redistribution and use in source and binary forms are permitted
X * provided that this notice is preserved and that due credit is given
X * to the University of California at Berkeley. The name of the University
X * may not be used to endorse or promote products derived from this
X * software without specific prior written permission. This software
X * is provided ``as is'' without express or implied warranty.
X */
X
X#ifndef lint
Xstatic char sccsid[] = "@(#)ttext1.c	5.2 (Berkeley) 2/16/88";
X#endif /* not lint */
X
X#include "back.h"
X
Xchar	*opts = " QIMRHEDSPT";
Xchar	*prompt = "-->";
X
Xchar *list[] = {
X    "\n\n\tI\tIntroduction to Backgammon",
X    "\tM\tMoves and Points",
X    "\tR\tRemoving Men from the Board",
X    "\tH\tHitting Blots",
X    "\tE\tEnding the Game and Scoring",
X    "\tD\tDoubling",
X    "\tS\tStrategy",
X    "\tP\tThe Program and How to Use It",
X    "\nalso, you can type:",
X    "\t?\tto get this list",
X    "\tQ\tto go start playing",
X    "\tT\tto go straight to the tutorial",
X    0
X};
X
Xchar	*hello[] = {
X    "\n\032   These rules consist of text describing how to play Backgammon",
X    "followed by a tutorial session where you play a practice game",
X    "against the computer.  When using this program, think carefuly",
X    "before typing, since it reacts as soon as you type something.  In",
X    "addition, the program presents text output, such as these rules,",
X    "in small blocks that will not roll off the top of the screen.",
X    "Frequently, you will see the characters '-->' indicating that the",
X    "program is waiting for you to finish reading, and will continue",
X    "printing when you type a space or newline.  Also, the rules are",
X    "divided into sections, and although you should read them in or-",
X    "der, you can go directly to any of them by typing one of the fol-",
X    "lowing letters:",
X    "(Remember to hit a space or a newline to continue.)",
X    "",
X    0
X};
X
Xchar	*intro1[] = {
X    "\nIntroduction:",
X    "\n   Backgammon is a game involving the skill of two players and",
X    "the luck of two dice.  There are two players, red and white, and",
X    "each player gets fifteen men.  The object of the game is to re-",
X    "move all your men from the board before the opponent does.  The",
X    "board consists of twenty-four positions, a 'bar' and a 'home' for",
X    "each player.  It looks like this:",
X    "",
X    0};
X
Xchar	*intro2[] = {
X    "",
X    "\n   Although not indicated on the board, the players' homes are",
X    "located just to the right of the board.  A player's men are placed",
X    "there when they are removed from the board.  The board you just",
X    "saw was in it's initial position.  All games start with the board",
X    "looking like this.  Notice that red's pieces are represented by the",
X    "letter 'r' and white's pieces are represented by the letter 'w'.",
X    "Also, a position may have zero or more pieces on it, e.g.  posi-",
X    "tion 12 has five red pieces on it, while position 11 does not",
X    "have any pieces of either color.",
X    "",
X    0};
X
Xchar	*moves[] = {
X    "\nMoves and Points:",
X    "\n   Moves are made along the positions on the board according to",
X    "their numbers.  Red moves in the positive direction (clockwise",
X    "from 1 to 24), and white moves in the negative direction (coun-",
X    "terclockwise from 24 to 1).",
X    "\n   A turn consists of rolling the dice, and moving the number of",
X    "positions indicated on each die.  The two numbers can be used to",
X    "move one man the sum of the two rolls, or two men the number on",
X    "each individual die.  For example, if red rolled 6 3 at the start",
X    "of the game, he might move a man from 1 to 7 to 10, using both",
X    "dice for one man, or he might move two men from position 12, one",
X    "to 15 and one to 18.  (Red did not have to choose two men start-",
X    "ing from the same position.)  In addition, doubles are treated",
X    "specially in backgammon.  When a player rolls doubles, he gets to",
X    "move as if he had four dice instead of two.  For instance, if you",
X    "rolled double 2's, you could move one man eight positions, four",
X    "men two positions each, or any permutation in between.",
X    "",
X    "\n   However, there are certain limitations, called 'points.'  A",
X    "player has a point when he has two or more men on the same posi-",
X    "tion.  This gives him custody of that position, and his opponent",
X    "cannot place his men there, even if passing through on the way to",
X    "another position.  When a player has six points in a row, it is",
X    "called a 'wall,' since any of his opponent's men behind the wall",
X    "cannot pass it and are trapped, at least for the moment.  Notice",
X    "that this could mean that a player could not use part or all of",
X    "his roll.  However, he must use as much of his roll as possible.",
X    "",
X    0};
X
Xchar	*remove[] = {
X    "\nRemoving Men from the Board:",
X    "\n   The most important part of the game is removing men, since",
X    "that is how you win the game.  Once a man is removed, he stays",
X    "off the board for the duration of the game.  However, a player",
X    "cannot remove men until all his men are on his 'inner table,' or",
X    "the last six positions of the board (19-24 for red, 6-1 for",
X    "white).",
X    "\n   To get off the board, a player must roll the exact number to",
X    "get his man one position past the last position on the board, or",
X    "his 'home.'  Hence, if red wanted to remove a man from position",
X    "23, he would have to roll a 2, anything else would be used for",
X    "another man, or for another purpose.  However, there is one ex-",
X    "ception.  If the player rolling has no men far enough to move the",
X    "roll made, he may move his farthest man off the board.  For exam-",
X    "ple, if red's farthest man back was on position 21, he could re-",
X    "move men from that position if he rolled a 5 or a 6, as well as a",
X    "4.  Since he does not have men on 20 (where he could use a 5) or",
X    "on 19 (where he could use a 6), he can use these rolls for posi-",
X    "tion 21.  A player never has to remove men, but he must make as",
X    "many moves as possible.",
X    "",
X    0};
X
Xchar	*hits[] = {
X    "\nHitting Blots:",
X    "\n   Although two men on a position form an impenetrable point, a",
X    "lone man is not so secure.  Such a man is called a 'blot' and has",
X    "the potential of getting hit by an opposing man.  When a player's",
X    "blot is hit, he is placed on the bar, and the first thing that",
X    "player must do is move the man off the bar.  Such moves are",
X    "counted as if the bar is one position behind the first position",
X    "on the board.  Thus if red has a man on the bar and rolls 2 3, he",
X    "must move the man on the bar to position 2 or 3 before moving any",
X    "other man.  If white had points on positions 2 and 3, then red",
X    "would forfeit his turn.  Being on the bar is a very bad position,",
X    "for often a player can lose many turns trying to move off the",
X    "bar, as well as being set back the full distance of the board.",
X    "",
X    0};
X
Xchar	*endgame[] = {
X    "\nEnding the Game and Scoring:",
X    "\n   Winning a game usually wins one point, the normal value of a",
X    "game.  However, if the losing player has not removed any men yet,",
X    "then the winning player wins double the game value, called a",
X    "'gammon.'  If the losing player has a player on the bar or on the",
X    "winner's inner table, then the winner gets triple the game value,",
X    "which is called a 'backgammon.'  (So that's where the name comes",
X    "from!)",
X    "",
X    0};
END_OF_ttext1.c
if test 7717 -ne `wc -c <ttext1.c`; then
    echo shar: \"ttext1.c\" unpacked with wrong size!
fi
# end of overwriting check
fi
if test -f ttext2.c -a "${1}" != "-c" ; then 
  echo shar: Will not over-write existing file \"ttext2.c\"
else
echo shar: Extracting \"ttext2.c\" \(6096 characters\)
sed "s/^X//" >ttext2.c <<'END_OF_ttext2.c'
X/*
X * Copyright (c) 1980 Regents of the University of California.
X * All rights reserved.
X *
X * Redistribution and use in source and binary forms are permitted
X * provided that this notice is preserved and that due credit is given
X * to the University of California at Berkeley. The name of the University
X * may not be used to endorse or promote products derived from this
X * software without specific prior written permission. This software
X * is provided ``as is'' without express or implied warranty.
X */
X
X#ifndef lint
Xstatic char sccsid[] = "@(#)ttext2.c	5.2 (Berkeley) 2/16/88";
X#endif /* not lint */
X
X#include "back.h"
X
Xchar *prompt, *list, *opts;
X
Xchar	*doubl[] = {
X    "\nDoubling:",
X    "\n   If a player thinks he is in a good position, he may double the",
X    "value of the game.  However, his opponent may not accept the pro-",
X    "posal and forfeit the game before the price gets too high.  A",
X    "player must double before he rolls, and once his double has been",
X    "accepted, he cannot double again, until his opponent has doubled.",
X    "Thus, unless the game swings back and forth in advantage between",
X    "the two players a great deal, the value of the game should be",
X    "low.  At any rate, the value of the game will never go above 64,",
X    "or six doubles.  However, if a player wins a backgammon at 64",
X    "points, he wins 192 points!",
X    "",
X    0};
X
Xchar	*stragy[] = {
X    "\nStrategy:",
X    "\n   Some general hints when playing:  Try not to leave men open",
X    "unless absolutely necessary.  Also, it is good to make as many",
X    "points as possible.  Often, two men from different positions can",
X    "be brought together to form a new point.  Although walls (six",
X    "points in a row) are difficult to form, many points nestled close-",
X    "ly together produce a formidable barrier.  Also, while it is good",
X    "to move back men forward, doing so lessens the opportunity for you",
X    "to hit men.  Finally, remember that once the two player's have",
X    "passed each other on the board, there is no chance of either team",
X    "being hit, so the game reduces to a race off the board.  Addi-",
X    "tional hints on strategy are presented in the practice game.",
X    "",
X    0};
X
Xchar	*prog[] = {
X   "\nThe Program and How It Works:",
X   "\n   A general rule of thumb is when you don't know what to do,",
X   "type a question mark, and you should get some help.  When it is",
X   "your turn, only your color will be printed out, with nothing",
X   "after it.  You may double by typing a 'd', but if you type a",
X   "space or newline, you will get your roll.  (Remember, you must",
X   "double before you roll.)  Also, typing a 'r' will reprint the",
X   "board, and a 'q' will quit the game.  The program will type",
X   "'Move:' when it wants your move, and you may indicate each die's",
X   "move with <s>-<f>, where <s> is the starting position and <f> is",
X   "the finishing position, or <s>/<r> where <r> is the roll made.",
X   "<s>-<f1>-<f2> is short for <s>-<f1>,<f1>-<f2> and <s>/<r1><r2> is",
X   "short for <s>/<r1>,<s>/<r2>.  Moves may be separated by a comma",
X   "or a space.",
X   "",
X   "\n   While typing, any input which does not make sense will not be",
X   "echoed, and a bell will sound instead.  Also, backspacing and",
X   "killing lines will echo differently than normal.  You may examine",
X   "the board by typing a 'r' if you have made a partial move, or be-",
X   "fore you type a newline, to see what the board looks like.  You",
X   "must end your move with a newline.  If you cannot double, your",
X   "roll will always be printed, and you will not be given the oppor-",
X   "tunity to double.  Home and bar are represented by the appropri-",
X   "ate number, 0 or 25 as the case may be, or by the letters 'h' or",
X   "'b' as appropriate.  You may also type 'r' or 'q' when the program",
X   "types 'Move:', which has the same effect as above.  Finally, you",
X   "will get to decide if you want to play red or white (or both if you",
X   "want to play a friend) at the beginning of the session, and you",
X   "will not get to change your mind later, since the computer keeps",
X   "score.",
X   "",
X   0};
X
Xchar	*lastch[] = {
X   "\nTutorial (Practice Game):",
X   "\n   This tutorial, for simplicity's sake, will let you play one",
X   "predetermined game.  All the rolls have been pre-arranged, and",
X   "only one response will let you advance to the next move.",
X   "Although a given roll will may have several legal moves, the tu-",
X   "torial will only accept one (not including the same moves in a",
X   "different order), claiming that that move is 'best.'  Obviously,",
X   "a subjective statement.  At any rate, be patient with it and have",
X   "fun learning about backgammon.  Also, to speed things up a lit-",
X   "tle, doubling will not take place in the tutorial, so you will",
X   "never get that opportunity, and quitting only leaves the tutori-",
X   "al, not the game.  You will still be able to play backgammon",
X   "after quitting.",
X   "\n   This is your last chance to look over the rules before the tu-",
X   "torial starts.",
X   "",
X   0};
X
Xtext (txt)
Xchar	**txt;
X
X{
X	char	**begin;
X	char	*a;
X	char	b;
X	char	*c;
X	int	i;
X
X	fixtty (noech);
X	begin = txt;
X	while (*txt)  {
X		a = *(txt++);
X		if (*a != '\0')  {
X			c = a;
X			for (i = 0; *(c++) != '\0'; i--);
X			writel (a);
X			writec ('\n');
X		} else  {
X			fixtty (raw);
X			writel (prompt);
X			for (;;)  {
X				if ((b = readc()) == '?')  {
X					if (tflag)  {
X						if (begscr)  {
X							curmove (18,0);
X							clend();
X						} else
X							clear();
X					} else
X						writec ('\n');
X					text (list);
X					writel (prompt);
X					continue;
X				}
X				i = 0;
X				if (b == '\n')
X					break;
X				while (i < 11)  {
X					if (b == opts[i])
X						break;
X					i++;
X				}
X				if (i == 11)
X					writec ('\007');
X				else
X					break;
X			}
X			if (tflag)  {
X				if (begscr)  {
X					curmove (18,0);
X					clend();
X				} else
X					clear();
X			} else
X				writec ('\n');
X			if (i)
X				return(i);
X			fixtty (noech);
X			if (tflag)
X				curmove (curr,0);
X			begin = txt;
X		}
X	}
X	fixtty (raw);
X	return (0);
X}
END_OF_ttext2.c
if test 6096 -ne `wc -c <ttext2.c`; then
    echo shar: \"ttext2.c\" unpacked with wrong size!
fi
# end of overwriting check
fi
if test -f version.c -a "${1}" != "-c" ; then 
  echo shar: Will not over-write existing file \"version.c\"
else
echo shar: Extracting \"version.c\" \(689 characters\)
sed "s/^X//" >version.c <<'END_OF_version.c'
X/*
X * Copyright (c) 1980, 1987 Regents of the University of California.
X * All rights reserved.
X *
X * Redistribution and use in source and binary forms are permitted
X * provided that this notice is preserved and that due credit is given
X * to the University of California at Berkeley. The name of the University
X * may not be used to endorse or promote products derived from this
X * software without specific prior written permission. This software
X * is provided ``as is'' without express or implied warranty.
X */
X
X#ifndef lint
Xstatic char sccsid[] = "@(#)version.c	1.2 (Berkeley) 2/16/88";
X#endif /* not lint */
X
Xchar	*message[] = {
X	"Last updated on Saturday, January 11, 1986.",
X	0
X};
END_OF_version.c
if test 689 -ne `wc -c <version.c`; then
    echo shar: \"version.c\" unpacked with wrong size!
fi
# end of overwriting check
fi
echo shar: End of archive 2 \(of 3\).
cp /dev/null ark2isdone
MISSING=""
for I in 1 2 3 ; do
    if test ! -f ark${I}isdone ; then
	MISSING="${MISSING} ${I}"
    fi
done
if test "${MISSING}" = "" ; then
    echo You have unpacked all 3 archives.
    rm -f ark[1-9]isdone
else
    echo You still need to unpack the following archives:
    echo "        " ${MISSING}
fi
##  End of shell archive.
exit 0