121217Sdist /*
221217Sdist  * Copyright (c) 1980 Regents of the University of California.
321217Sdist  * All rights reserved.  The Berkeley software License Agreement
421217Sdist  * specifies the terms and conditions for redistribution.
521217Sdist  */
66766Srrh 
721217Sdist #ifndef lint
8*30876Sbostic static char sccsid[] = "@(#)subs.c	5.2 (Berkeley) 04/09/87";
921217Sdist #endif not lint
1021217Sdist 
116766Srrh #include <stdio.h>
126766Srrh #include "back.h"
136766Srrh 
146766Srrh int	buffnum;
156766Srrh char	outbuff[BUFSIZ];
166766Srrh 
176766Srrh static char	plred[] = "Player is red, computer is white.";
186766Srrh static char	plwhite[] = "Player is white, computer is red.";
196766Srrh static char	nocomp[] = "(No computer play.)";
206766Srrh 
216766Srrh char  *descr[] = {
226766Srrh 	"Usage:  backgammon [-] [n r w b pr pw pb t3a]\n",
236766Srrh 	"\t-\tgets this list\n\tn\tdon't ask for rules or instructions",
246766Srrh 	"\tr\tplayer is red (implies n)\n\tw\tplayer is white (implies n)",
256766Srrh 	"\tb\ttwo players, red and white (implies n)",
266766Srrh 	"\tpr\tprint the board before red's turn",
276766Srrh 	"\tpw\tprint the board before white's turn",
286766Srrh 	"\tpb\tprint the board before both player's turn",
296766Srrh 	"\tterm\tterminal is a term",
306766Srrh 	"\tsfile\trecover saved game from file",
316766Srrh 	0
326766Srrh };
336766Srrh 
346766Srrh errexit (s)
356766Srrh register char	*s;
366766Srrh {
376766Srrh 	write (2,"\n",1);
386766Srrh 	perror (s);
396766Srrh 	getout();
406766Srrh }
416766Srrh 
426766Srrh strset (s1,s2)
436766Srrh register char	*s1, *s2;
446766Srrh {
456766Srrh 	while ( (*s1++ = *s2++) != '\0');
466766Srrh }
476766Srrh 
486766Srrh addbuf (c)
496766Srrh register char	c;
506766Srrh 
516766Srrh {
526766Srrh 	buffnum++;
536766Srrh 	if (buffnum == BUFSIZ)  {
546766Srrh 		if (write(1,outbuff,BUFSIZ) != BUFSIZ)
556766Srrh 			errexit ("addbuf (write):");
566766Srrh 		buffnum = 0;
576766Srrh 	}
586766Srrh 	outbuff[buffnum] = c;
596766Srrh }
606766Srrh 
616766Srrh buflush ()  {
626766Srrh 	if (buffnum < 0)
636766Srrh 		return;
646766Srrh 	buffnum++;
656766Srrh 	if (write (1,outbuff,buffnum) != buffnum)
666766Srrh 		errexit ("buflush (write):");
676766Srrh 	buffnum = -1;
686766Srrh }
696766Srrh 
706766Srrh readc () {
716766Srrh 	char	c;
726766Srrh 
736766Srrh 	if (tflag)  {
746766Srrh 		cline();
756766Srrh 		newpos();
766766Srrh 	}
776766Srrh 	buflush();
786766Srrh 	if (read(0,&c,1) != 1)
796766Srrh 		errexit ("readc");
80*30876Sbostic #ifdef WHY_IS_THIS_HARDWIRED_IN_HERE
816766Srrh 	if (c == '\177')
826766Srrh 		getout();
83*30876Sbostic #endif
846766Srrh 	if (c == '\033' || c == '\015')
856766Srrh 		return ('\n');
866766Srrh 	if (cflag)
876766Srrh 		return (c);
886766Srrh 	if (c == '\014')
896766Srrh 		return ('R');
906766Srrh 	if (c >= 'a' && c <= 'z')
916766Srrh 		return (c & 0137);
926766Srrh 	return (c);
936766Srrh }
946766Srrh 
956766Srrh writec (c)
966766Srrh char	c;
976766Srrh {
986766Srrh 	if (tflag)
996766Srrh 		fancyc (c);
1006766Srrh 	else
1016766Srrh 		addbuf (c);
1026766Srrh }
1036766Srrh 
1046766Srrh writel (l)
1056766Srrh register char	*l;
1066766Srrh {
1076766Srrh #ifdef DEBUG
1086766Srrh 	register char	*s;
1096766Srrh 
1106766Srrh 	if (trace == NULL)
1116766Srrh 		trace = fopen ("bgtrace","w");
1126766Srrh 
1136766Srrh 	fprintf (trace,"writel: \"");
1146766Srrh 	for (s = l; *s; s++) {
1156766Srrh 		if (*s < ' ' || *s == '\177')
1166766Srrh 			fprintf (trace,"^%c",(*s)^0100);
1176766Srrh 		else
1186766Srrh 			putc (*s,trace);
1196766Srrh 	}
1206766Srrh 	fprintf (trace,"\"\n");
1216766Srrh 	fflush (trace);
1226766Srrh #endif
1236766Srrh 
1246766Srrh 	while (*l)
1256766Srrh 		writec (*l++);
1266766Srrh }
1276766Srrh 
1286766Srrh proll ()   {
1296766Srrh 	if (d0)
1306766Srrh 		swap;
1316766Srrh 	if (cturn == 1)
1326766Srrh 		writel ("Red's roll:  ");
1336766Srrh 	else
1346766Srrh 		writel ("White's roll:  ");
1356766Srrh 	writec (D0+'0');
1366766Srrh 	writec ('\040');
1376766Srrh 	writec (D1+'0');
1386766Srrh 	if (tflag)
1396766Srrh 		cline();
1406766Srrh }
1416766Srrh 
1426766Srrh wrint (n)
1436766Srrh int	n;
1446766Srrh {
1456766Srrh 	register int	i, j, t;
1466766Srrh 
1476766Srrh 	for (i = 4; i > 0; i--)  {
1486766Srrh 		t = 1;
1496766Srrh 		for (j = 0; j<i; j++)
1506766Srrh 			t *= 10;
1516766Srrh 		if (n > t-1)
1526766Srrh 			writec ((n/t)%10+'0');
1536766Srrh 	}
1546766Srrh 	writec (n%10+'0');
1556766Srrh }
1566766Srrh 
1576766Srrh gwrite()  {
1586766Srrh 	register int	r, c;
1596766Srrh 
1606766Srrh 	if (tflag)  {
1616766Srrh 		r = curr;
1626766Srrh 		c = curc;
1636766Srrh 		curmove (16,0);
1646766Srrh 	}
1656766Srrh 
1666766Srrh 	if (gvalue > 1)  {
1676766Srrh 		writel ("Game value:  ");
1686766Srrh 		wrint (gvalue);
1696766Srrh 		writel (".  ");
1706766Srrh 		if (dlast == -1)
1716766Srrh 			writel (color[0]);
1726766Srrh 		else
1736766Srrh 			writel (color[1]);
1746766Srrh 		writel (" doubled last.");
1756766Srrh 	} else  {
1766766Srrh 		switch (pnum)  {
1776766Srrh 		case -1:			    /* player is red */
1786766Srrh 			writel (plred);
1796766Srrh 			break;
1806766Srrh 		case 0:				    /* player is both colors */
1816766Srrh 			writel (nocomp);
1826766Srrh 			break;
1836766Srrh 		case 1:				    /* player is white */
1846766Srrh 			writel (plwhite);
1856766Srrh 		}
1866766Srrh 	}
1876766Srrh 
1886766Srrh 	if (rscore || wscore)  {
1896766Srrh 		writel ("  ");
1906766Srrh 		wrscore();
1916766Srrh 	}
1926766Srrh 
1936766Srrh 	if (tflag)  {
1946766Srrh 		cline();
1956766Srrh 		curmove (r,c);
1966766Srrh 	}
1976766Srrh }
1986766Srrh 
1996766Srrh quit ()  {
2006766Srrh 	register int	i;
2016766Srrh 
2026766Srrh 	if (tflag)  {
2036766Srrh 		curmove (20,0);
2046766Srrh 		clend();
2056766Srrh 	} else
2066766Srrh 		writec ('\n');
2076766Srrh 	writel ("Are you sure you want to quit?");
2086766Srrh 	if (yorn (0))  {
2096766Srrh 		if (rfl)  {
2106766Srrh 			writel ("Would you like to save this game?");
2116766Srrh 			if (yorn(0))
2126766Srrh 				save(0);
2136766Srrh 		}
2146766Srrh 		cturn = 0;
2156766Srrh 		return (1);
2166766Srrh 	}
2176766Srrh 	return (0);
2186766Srrh }
2196766Srrh 
2206766Srrh yorn (special)
2216766Srrh register char	special;			/* special response */
2226766Srrh {
2236766Srrh 	register char	c;
2246766Srrh 	register int	i;
2256766Srrh 
2266766Srrh 	i = 1;
2276766Srrh 	while ( (c = readc()) != 'Y' && c != 'N')  {
2286766Srrh 		if (special && c == special)
2296766Srrh 			return (2);
2306766Srrh 		if (i)  {
2316766Srrh 			if (special)  {
2326766Srrh 				writel ("  (Y, N, or ");
2336766Srrh 				writec (special);
2346766Srrh 				writec (')');
2356766Srrh 			} else
2366766Srrh 				writel ("  (Y or N)");
2376766Srrh 			i = 0;
2386766Srrh 		} else
2396766Srrh 			writec ('\007');
2406766Srrh 	}
2416766Srrh 	if (c == 'Y')
2426766Srrh 		writel ("  Yes.\n");
2436766Srrh 	else
2446766Srrh 		writel ("  No.\n");
2456766Srrh 	if (tflag)
2466766Srrh 		buflush();
2476766Srrh 	return (c == 'Y');
2486766Srrh }
2496766Srrh 
2506766Srrh wrhit (i)
2516766Srrh register int	i;
2526766Srrh {
2536766Srrh 	writel ("Blot hit on ");
2546766Srrh 	wrint (i);
2556766Srrh 	writec ('.');
2566766Srrh 	writec ('\n');
2576766Srrh }
2586766Srrh 
2596766Srrh nexturn ()  {
2606766Srrh 	register int	c;
2616766Srrh 
2626766Srrh 	cturn = -cturn;
2636766Srrh 	c = cturn/abs(cturn);
2646766Srrh 	home = bar;
2656766Srrh 	bar = 25-bar;
2666766Srrh 	offptr += c;
2676766Srrh 	offopp -= c;
2686766Srrh 	inptr += c;
2696766Srrh 	inopp -= c;
2706766Srrh 	Colorptr += c;
2716766Srrh 	colorptr += c;
2726766Srrh }
2736766Srrh 
2746766Srrh getarg (arg)
2756766Srrh register char	***arg;
2766766Srrh 
2776766Srrh {
2789360Smckusick 	register char	**s;
2796766Srrh 
2806766Srrh 	/* process arguments here.  dashes are ignored, nbrw are ignored
2816766Srrh 	   if the game is being recovered */
2826766Srrh 
2839360Smckusick 	s = *arg;
2849360Smckusick 	while (s[0][0] == '-') {
2859360Smckusick 		switch (s[0][1])  {
2866766Srrh 
2876766Srrh 		/* don't ask if rules or instructions needed */
2886766Srrh 		case 'n':
2896766Srrh 			if (rflag)
2906766Srrh 				break;
2916766Srrh 			aflag = 0;
2929360Smckusick 			args[acnt++] = 'n';
2936766Srrh 			break;
2946766Srrh 
2956766Srrh 		/* player is both read and white */
2966766Srrh 		case 'b':
2976766Srrh 			if (rflag)
2986766Srrh 				break;
2996766Srrh 			pnum = 0;
3006766Srrh 			aflag = 0;
3019360Smckusick 			args[acnt++] = 'b';
3026766Srrh 			break;
3036766Srrh 
3046766Srrh 		/* player is red */
3056766Srrh 		case 'r':
3066766Srrh 			if (rflag)
3076766Srrh 				break;
3086766Srrh 			pnum = -1;
3096766Srrh 			aflag = 0;
3109360Smckusick 			args[acnt++] = 'r';
3116766Srrh 			break;
3126766Srrh 
3136766Srrh 		/* player is white */
3146766Srrh 		case 'w':
3156766Srrh 			if (rflag)
3166766Srrh 				break;
3176766Srrh 			pnum = 1;
3186766Srrh 			aflag = 0;
3199360Smckusick 			args[acnt++] = 'w';
3206766Srrh 			break;
3216766Srrh 
3226766Srrh 		/* print board after move according to following character */
3236766Srrh 		case 'p':
3249360Smckusick 			if (s[0][2] != 'r' && s[0][2] != 'w' && s[0][2] != 'b')
3256766Srrh 				break;
3266766Srrh 			args[acnt++] = 'p';
3279360Smckusick 			args[acnt++] = s[0][2];
3289360Smckusick 			if (s[0][2] == 'r')
3296766Srrh 				bflag = 1;
3309360Smckusick 			if (s[0][2] == 'w')
3316766Srrh 				bflag = -1;
3329360Smckusick 			if (s[0][2] == 'b')
3336766Srrh 				bflag = 0;
3346766Srrh 			break;
3356766Srrh 
3366766Srrh 		case 't':
3379360Smckusick 			if (s[0][2] == '\0') {	/* get terminal caps */
3389360Smckusick 				s++;
3399360Smckusick 				tflag = getcaps (*s);
3409360Smckusick 			} else
3419360Smckusick 				tflag = getcaps (&s[0][2]);
3429360Smckusick 			break;
3436766Srrh 
3446766Srrh 		case 's':
3459360Smckusick 			s++;
3469360Smckusick 			/* recover file */
3479360Smckusick 			recover (s[0]);
3489360Smckusick 			break;
3496766Srrh 		}
3509360Smckusick 		s++;
3516766Srrh 	}
3529360Smckusick 	if (s[0] != 0)
3539360Smckusick 		recover(s[0]);
3546766Srrh }
3556766Srrh 
3566766Srrh init ()  {
3576766Srrh 	register int	i;
3586766Srrh 	for (i = 0; i < 26;)
3596766Srrh 		board[i++] = 0;
3606766Srrh 	board[1] = 2;
3616766Srrh 	board[6] = board[13] = -5;
3626766Srrh 	board[8] = -3;
3636766Srrh 	board[12] = board[19] = 5;
3646766Srrh 	board[17] = 3;
3656766Srrh 	board[24] = -2;
3666766Srrh 	off[0] = off[1] = -15;
3676766Srrh 	in[0] = in[1] = 5;
3686766Srrh 	gvalue = 1;
3696766Srrh 	dlast = 0;
3706766Srrh }
3716766Srrh 
3726766Srrh wrscore ()  {
3736766Srrh 	writel ("Score:  ");
3746766Srrh 	writel (color[1]);
3756766Srrh 	writec (' ');
3766766Srrh 	wrint (rscore);
3776766Srrh 	writel (", ");
3786766Srrh 	writel (color[0]);
3796766Srrh 	writec (' ');
3806766Srrh 	wrint (wscore);
3816766Srrh }
3826766Srrh 
3836766Srrh fixtty (mode)
3846766Srrh int	mode;
3856766Srrh {
3866766Srrh 	if (tflag)
3876766Srrh 		newpos();
3886766Srrh 	buflush();
3896766Srrh 	tty.sg_flags = mode;
3906766Srrh 	if (stty (0,&tty) < 0)
3916766Srrh 		errexit("fixtty");
3926766Srrh }
3936766Srrh 
3946766Srrh getout ()  {
3956766Srrh 	/* go to bottom of screen */
3966766Srrh 	if (tflag)  {
3976766Srrh 		curmove (23,0);
3986766Srrh 		cline();
3996766Srrh 	} else
4006766Srrh 		writec ('\n');
4016766Srrh 
4026766Srrh 	/* fix terminal status */
4036766Srrh 	fixtty (old);
4046766Srrh 	exit();
4056766Srrh }
4066766Srrh roll ()  {
4076766Srrh 	register char	c;
4086766Srrh 	register int	row;
4096766Srrh 	register int	col;
4106766Srrh 
4116766Srrh 	if (iroll)  {
4126766Srrh 		if (tflag)  {
4136766Srrh 			row = curr;
4146766Srrh 			col = curc;
4156766Srrh 			curmove (17,0);
4166766Srrh 		} else
4176766Srrh 			writec ('\n');
4186766Srrh 		writel ("ROLL: ");
4196766Srrh 		c = readc();
4206766Srrh 		if (c != '\n')  {
4216766Srrh 			while (c < '1' || c > '6')
4226766Srrh 				c = readc();
4236766Srrh 			D0 = c-'0';
4246766Srrh 			writec (' ');
4256766Srrh 			writec (c);
4266766Srrh 			c = readc();
4276766Srrh 			while (c < '1' || c > '6')
4286766Srrh 				c = readc();
4296766Srrh 			D1 = c-'0';
4306766Srrh 			writec (' ');
4316766Srrh 			writec (c);
4326766Srrh 			if (tflag)  {
4336766Srrh 				curmove (17,0);
4346766Srrh 				cline();
4356766Srrh 				curmove (row,col);
4366766Srrh 			} else
4376766Srrh 				writec ('\n');
4386766Srrh 			return;
4396766Srrh 		}
4406766Srrh 		if (tflag)  {
4416766Srrh 			curmove (17,0);
4426766Srrh 			cline();
4436766Srrh 			curmove (row,col);
4446766Srrh 		} else
4456766Srrh 			writec ('\n');
4466766Srrh 	}
4476766Srrh 	D0 = rnum(6)+1;
4486766Srrh 	D1 = rnum(6)+1;
4496766Srrh 	d0 = 0;
4506766Srrh }
451