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