119fa9963Sdist /*
2*0e395338Sbostic  * Copyright (c) 1980, 1993
3*0e395338Sbostic  *	The Regents of the University of California.  All rights reserved.
466562148Sbostic  *
57dc4431aSbostic  * %sccs.include.redist.c%
619fa9963Sdist  */
719fa9963Sdist 
819fa9963Sdist #ifndef lint
9*0e395338Sbostic static char sccsid[] = "@(#)extra.c	8.1 (Berkeley) 05/31/93";
1066562148Sbostic #endif /* not lint */
1165ac7decSrrh 
1265ac7decSrrh #include "back.h"
1365ac7decSrrh 
1465ac7decSrrh #ifdef DEBUG
1565ac7decSrrh #include <stdio.h>
1665ac7decSrrh FILE	*trace;
1765ac7decSrrh #endif
1865ac7decSrrh 
1965ac7decSrrh /*
2065ac7decSrrh  * dble()
2165ac7decSrrh  *	Have the current player double and ask opponent to accept.
2265ac7decSrrh  */
2365ac7decSrrh 
dble()2465ac7decSrrh dble ()  {
2565ac7decSrrh 	register int	resp;			/* response to y/n */
2665ac7decSrrh 
2765ac7decSrrh 	for (;;)  {
2865ac7decSrrh 		writel (" doubles.");		/* indicate double */
2965ac7decSrrh 
3065ac7decSrrh 		if (cturn == -pnum)  {		/* see if computer accepts */
3165ac7decSrrh 			if (dblgood())  {	    /* guess not */
3265ac7decSrrh 				writel ("  Declined.\n");
3365ac7decSrrh 				nexturn();
3465ac7decSrrh 				cturn *= -2;	    /* indicate loss */
3565ac7decSrrh 				return;
3665ac7decSrrh 			} else  {		    /* computer accepts */
3765ac7decSrrh 				writel ("  Accepted.\n");
3865ac7decSrrh 				gvalue *= 2;	    /* double game value */
3965ac7decSrrh 				dlast = cturn;
4065ac7decSrrh 				if (tflag)
4165ac7decSrrh 					gwrite();
4265ac7decSrrh 				return;
4365ac7decSrrh 			}
4465ac7decSrrh 		}
4565ac7decSrrh 
4665ac7decSrrh 						/* ask if player accepts */
4765ac7decSrrh 		writel ("  Does ");
4865ac7decSrrh 		writel (cturn == 1? color[2]: color[3]);
4965ac7decSrrh 		writel (" accept?");
5065ac7decSrrh 
5165ac7decSrrh 						/* get response from yorn,
5265ac7decSrrh 						 * a "2" means he said "p"
5365ac7decSrrh 						 * for print board. */
5465ac7decSrrh 		if ((resp = yorn ('R')) == 2)  {
5565ac7decSrrh 			writel ("  Reprint.\n");
5665ac7decSrrh 			buflush();
5765ac7decSrrh 			wrboard();
5865ac7decSrrh 			writel (*Colorptr);
5965ac7decSrrh 			continue;
6065ac7decSrrh 		}
6165ac7decSrrh 
6265ac7decSrrh 						/* check response */
6365ac7decSrrh 		if (resp)  {
6465ac7decSrrh 						    /* accepted */
6565ac7decSrrh 			gvalue *= 2;
6665ac7decSrrh 			dlast = cturn;
6765ac7decSrrh 			if (tflag)
6865ac7decSrrh 				gwrite();
6965ac7decSrrh 			return;
7065ac7decSrrh 		}
7165ac7decSrrh 
7265ac7decSrrh 		nexturn ();			/* declined */
7365ac7decSrrh 		cturn *= -2;
7465ac7decSrrh 		return;
7565ac7decSrrh 	}
7665ac7decSrrh }
7765ac7decSrrh 
7865ac7decSrrh /*
7965ac7decSrrh  * dblgood ()
8065ac7decSrrh  *	Returns 1 if the computer would double in this position.  This
8165ac7decSrrh  * is not an exact science.  The computer will decline a double that he
8265ac7decSrrh  * would have made.  Accumulated judgments are kept in the variable n,
8365ac7decSrrh  * which is in "pips", i.e., the position of each man summed over all
8465ac7decSrrh  * men, with opponent's totals negative.  Thus, n should have a positive
8565ac7decSrrh  * value of 7 for each move ahead, or a negative value of 7 for each one
8665ac7decSrrh  * behind.
8765ac7decSrrh  */
8865ac7decSrrh 
dblgood()8965ac7decSrrh dblgood ()  {
9065ac7decSrrh 	register int	n;			/* accumulated judgment */
9165ac7decSrrh 	register int	OFFC = *offptr;		/* no. of computer's men off */
9265ac7decSrrh 	register int	OFFO = *offopp;		/* no. of player's men off */
9365ac7decSrrh 
9465ac7decSrrh #ifdef DEBUG
9565ac7decSrrh 	register int	i;
9665ac7decSrrh 	if (trace == NULL)
9765ac7decSrrh 		trace = fopen ("bgtrace","w");
9865ac7decSrrh #endif
9965ac7decSrrh 
10065ac7decSrrh 						/* get real pip value */
10165ac7decSrrh 	n = eval()*cturn;
10265ac7decSrrh #ifdef DEBUG
10365ac7decSrrh 	fputs ("\nDoubles:\nBoard: ",trace);
10465ac7decSrrh 	for (i = 0; i < 26; i++)
10565ac7decSrrh 		fprintf (trace," %d",board[i]);
10665ac7decSrrh 	fprintf (trace,"\n\tpip = %d, ",n);
10765ac7decSrrh #endif
10865ac7decSrrh 
10965ac7decSrrh 						/* below adjusts pip value
11065ac7decSrrh 						 * according to position
11165ac7decSrrh 						 * judgments */
11265ac7decSrrh 
11365ac7decSrrh 						/* check men moving off
11465ac7decSrrh 						 * board */
11565ac7decSrrh 	if (OFFC > -15 || OFFO > -15)  {
11665ac7decSrrh 		if (OFFC < 0 && OFFO < 0)  {
11765ac7decSrrh 			OFFC += 15;
11865ac7decSrrh 			OFFO += 15;
11965ac7decSrrh 			n +=((OFFC-OFFO)*7)/2;
12065ac7decSrrh 		} else if (OFFC < 0)  {
12165ac7decSrrh 			OFFC += 15;
12265ac7decSrrh 			n -= OFFO*7/2;
12365ac7decSrrh 		} else if (OFFO < 0)  {
12465ac7decSrrh 			OFFO += 15;
12565ac7decSrrh 			n += OFFC*7/2;
12665ac7decSrrh 		}
12765ac7decSrrh 		if (OFFC < 8 && OFFO > 8)
12865ac7decSrrh 			n -= 7;
12965ac7decSrrh 		if (OFFC < 10 && OFFO > 10)
13065ac7decSrrh 			n -= 7;
13165ac7decSrrh 		if (OFFC < 12 && OFFO > 12)
13265ac7decSrrh 			n -= 7;
13365ac7decSrrh 		if (OFFO < 8 && OFFC > 8)
13465ac7decSrrh 			n += 7;
13565ac7decSrrh 		if (OFFO < 10 && OFFC > 10)
13665ac7decSrrh 			n += 7;
13765ac7decSrrh 		if (OFFO < 12 && OFFC > 12)
13865ac7decSrrh 			n += 7;
13965ac7decSrrh 		n += ((OFFC-OFFO)*7)/2;
14065ac7decSrrh 	}
14165ac7decSrrh 
14265ac7decSrrh #ifdef DEBUG
14365ac7decSrrh 	fprintf (trace,"off = %d, ",n);
14465ac7decSrrh #endif
14565ac7decSrrh 
14665ac7decSrrh 						/* see if men are trapped */
14765ac7decSrrh 	n -= freemen(bar);
14865ac7decSrrh 	n += freemen(home);
14965ac7decSrrh 	n += trapped(home,-cturn);
15065ac7decSrrh 	n -= trapped(bar,cturn);
15165ac7decSrrh 
15265ac7decSrrh #ifdef DEBUG
15365ac7decSrrh 	fprintf (trace,"free = %d\n",n);
15465ac7decSrrh 	fprintf (trace,"\tOFFC = %d, OFFO = %d\n",OFFC,OFFO);
15565ac7decSrrh 	fflush (trace);
15665ac7decSrrh #endif
15765ac7decSrrh 
15865ac7decSrrh 						/* double if 2-3 moves ahead */
15965ac7decSrrh 	if (n > 10+rnum(7))
16065ac7decSrrh 		return(1);
16165ac7decSrrh 	return (0);
16265ac7decSrrh }
16365ac7decSrrh 
freemen(b)16465ac7decSrrh freemen (b)
16565ac7decSrrh int	b;
16665ac7decSrrh 
16765ac7decSrrh {
16865ac7decSrrh 	register int	i, inc, lim;
16965ac7decSrrh 
17065ac7decSrrh 	odds(0,0,0);
17165ac7decSrrh 	if (board[b] == 0)
17265ac7decSrrh 		return (0);
17365ac7decSrrh 	inc = (b == 0? 1: -1);
17465ac7decSrrh 	lim = (b == 0? 7: 18);
17565ac7decSrrh 	for (i = b+inc; i != lim; i += inc)
17665ac7decSrrh 		if (board[i]*inc < -1)
17765ac7decSrrh 			odds(abs(b-i),0,abs(board[b]));
17865ac7decSrrh 	if (abs(board[b]) == 1)
17965ac7decSrrh 		return ((36-count())/5);
18065ac7decSrrh 	return (count()/5);
18165ac7decSrrh }
18265ac7decSrrh 
trapped(n,inc)18365ac7decSrrh trapped (n,inc)
18465ac7decSrrh int	n, inc;
18565ac7decSrrh 
18665ac7decSrrh {
18765ac7decSrrh 	register int	i, j, k;
18865ac7decSrrh 	int		c, l, ct;
18965ac7decSrrh 
19065ac7decSrrh 	ct = 0;
19165ac7decSrrh 	l = n+7*inc;
19265ac7decSrrh 	for (i = n+inc; i != l; i += inc)  {
19365ac7decSrrh 		odds (0,0,0);
19465ac7decSrrh 		c = abs(i-l);
19565ac7decSrrh 		if (board[i]*inc > 0)  {
19665ac7decSrrh 			for (j = c; j < 13; j++)
19765ac7decSrrh 				if (board[i+inc*j]*inc < -1)  {
19865ac7decSrrh 					if (j < 7)
19965ac7decSrrh 						odds (j,0,1);
20065ac7decSrrh 					for (k = 1; k < 7 && k < j; k++)
20165ac7decSrrh 						if (j-k < 7)
20265ac7decSrrh 							odds (k,j-k,1);
20365ac7decSrrh 				}
20465ac7decSrrh 			ct += abs(board[i])*(36-count());
20565ac7decSrrh 		}
20665ac7decSrrh 	}
20765ac7decSrrh 	return (ct/5);
20865ac7decSrrh }
20965ac7decSrrh 
eval()21065ac7decSrrh eval ()  {
21165ac7decSrrh 
21265ac7decSrrh 	register int	i, j;
21365ac7decSrrh 
21465ac7decSrrh 	for (j = i = 0; i < 26; i++)
21565ac7decSrrh 		j += (board[i] >= 0 ? i*board[i] : (25-i)*board[i]);
21665ac7decSrrh 
21765ac7decSrrh 	if (off[1] >= 0)
21865ac7decSrrh 		j += 25*off[1];
21965ac7decSrrh 	else
22065ac7decSrrh 		j += 25*(off[1]+15);
22165ac7decSrrh 
22265ac7decSrrh 	if (off[0] >= 0)
22365ac7decSrrh 		j -= 25*off[0];
22465ac7decSrrh 	else
22565ac7decSrrh 		j -= 25*(off[0]+15);
22665ac7decSrrh 	return (j);
22765ac7decSrrh }
228