1 /*
2  * Copyright (c) 1980, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)extra.c	8.1 (Berkeley) 05/31/93";
10 #endif /* not lint */
11 
12 #include "back.h"
13 
14 #ifdef DEBUG
15 #include <stdio.h>
16 FILE	*trace;
17 #endif
18 
19 /*
20  * dble()
21  *	Have the current player double and ask opponent to accept.
22  */
23 
dble()24 dble ()  {
25 	register int	resp;			/* response to y/n */
26 
27 	for (;;)  {
28 		writel (" doubles.");		/* indicate double */
29 
30 		if (cturn == -pnum)  {		/* see if computer accepts */
31 			if (dblgood())  {	    /* guess not */
32 				writel ("  Declined.\n");
33 				nexturn();
34 				cturn *= -2;	    /* indicate loss */
35 				return;
36 			} else  {		    /* computer accepts */
37 				writel ("  Accepted.\n");
38 				gvalue *= 2;	    /* double game value */
39 				dlast = cturn;
40 				if (tflag)
41 					gwrite();
42 				return;
43 			}
44 		}
45 
46 						/* ask if player accepts */
47 		writel ("  Does ");
48 		writel (cturn == 1? color[2]: color[3]);
49 		writel (" accept?");
50 
51 						/* get response from yorn,
52 						 * a "2" means he said "p"
53 						 * for print board. */
54 		if ((resp = yorn ('R')) == 2)  {
55 			writel ("  Reprint.\n");
56 			buflush();
57 			wrboard();
58 			writel (*Colorptr);
59 			continue;
60 		}
61 
62 						/* check response */
63 		if (resp)  {
64 						    /* accepted */
65 			gvalue *= 2;
66 			dlast = cturn;
67 			if (tflag)
68 				gwrite();
69 			return;
70 		}
71 
72 		nexturn ();			/* declined */
73 		cturn *= -2;
74 		return;
75 	}
76 }
77 
78 /*
79  * dblgood ()
80  *	Returns 1 if the computer would double in this position.  This
81  * is not an exact science.  The computer will decline a double that he
82  * would have made.  Accumulated judgments are kept in the variable n,
83  * which is in "pips", i.e., the position of each man summed over all
84  * men, with opponent's totals negative.  Thus, n should have a positive
85  * value of 7 for each move ahead, or a negative value of 7 for each one
86  * behind.
87  */
88 
dblgood()89 dblgood ()  {
90 	register int	n;			/* accumulated judgment */
91 	register int	OFFC = *offptr;		/* no. of computer's men off */
92 	register int	OFFO = *offopp;		/* no. of player's men off */
93 
94 #ifdef DEBUG
95 	register int	i;
96 	if (trace == NULL)
97 		trace = fopen ("bgtrace","w");
98 #endif
99 
100 						/* get real pip value */
101 	n = eval()*cturn;
102 #ifdef DEBUG
103 	fputs ("\nDoubles:\nBoard: ",trace);
104 	for (i = 0; i < 26; i++)
105 		fprintf (trace," %d",board[i]);
106 	fprintf (trace,"\n\tpip = %d, ",n);
107 #endif
108 
109 						/* below adjusts pip value
110 						 * according to position
111 						 * judgments */
112 
113 						/* check men moving off
114 						 * board */
115 	if (OFFC > -15 || OFFO > -15)  {
116 		if (OFFC < 0 && OFFO < 0)  {
117 			OFFC += 15;
118 			OFFO += 15;
119 			n +=((OFFC-OFFO)*7)/2;
120 		} else if (OFFC < 0)  {
121 			OFFC += 15;
122 			n -= OFFO*7/2;
123 		} else if (OFFO < 0)  {
124 			OFFO += 15;
125 			n += OFFC*7/2;
126 		}
127 		if (OFFC < 8 && OFFO > 8)
128 			n -= 7;
129 		if (OFFC < 10 && OFFO > 10)
130 			n -= 7;
131 		if (OFFC < 12 && OFFO > 12)
132 			n -= 7;
133 		if (OFFO < 8 && OFFC > 8)
134 			n += 7;
135 		if (OFFO < 10 && OFFC > 10)
136 			n += 7;
137 		if (OFFO < 12 && OFFC > 12)
138 			n += 7;
139 		n += ((OFFC-OFFO)*7)/2;
140 	}
141 
142 #ifdef DEBUG
143 	fprintf (trace,"off = %d, ",n);
144 #endif
145 
146 						/* see if men are trapped */
147 	n -= freemen(bar);
148 	n += freemen(home);
149 	n += trapped(home,-cturn);
150 	n -= trapped(bar,cturn);
151 
152 #ifdef DEBUG
153 	fprintf (trace,"free = %d\n",n);
154 	fprintf (trace,"\tOFFC = %d, OFFO = %d\n",OFFC,OFFO);
155 	fflush (trace);
156 #endif
157 
158 						/* double if 2-3 moves ahead */
159 	if (n > 10+rnum(7))
160 		return(1);
161 	return (0);
162 }
163 
freemen(b)164 freemen (b)
165 int	b;
166 
167 {
168 	register int	i, inc, lim;
169 
170 	odds(0,0,0);
171 	if (board[b] == 0)
172 		return (0);
173 	inc = (b == 0? 1: -1);
174 	lim = (b == 0? 7: 18);
175 	for (i = b+inc; i != lim; i += inc)
176 		if (board[i]*inc < -1)
177 			odds(abs(b-i),0,abs(board[b]));
178 	if (abs(board[b]) == 1)
179 		return ((36-count())/5);
180 	return (count()/5);
181 }
182 
trapped(n,inc)183 trapped (n,inc)
184 int	n, inc;
185 
186 {
187 	register int	i, j, k;
188 	int		c, l, ct;
189 
190 	ct = 0;
191 	l = n+7*inc;
192 	for (i = n+inc; i != l; i += inc)  {
193 		odds (0,0,0);
194 		c = abs(i-l);
195 		if (board[i]*inc > 0)  {
196 			for (j = c; j < 13; j++)
197 				if (board[i+inc*j]*inc < -1)  {
198 					if (j < 7)
199 						odds (j,0,1);
200 					for (k = 1; k < 7 && k < j; k++)
201 						if (j-k < 7)
202 							odds (k,j-k,1);
203 				}
204 			ct += abs(board[i])*(36-count());
205 		}
206 	}
207 	return (ct/5);
208 }
209 
eval()210 eval ()  {
211 
212 	register int	i, j;
213 
214 	for (j = i = 0; i < 26; i++)
215 		j += (board[i] >= 0 ? i*board[i] : (25-i)*board[i]);
216 
217 	if (off[1] >= 0)
218 		j += 25*off[1];
219 	else
220 		j += 25*(off[1]+15);
221 
222 	if (off[0] >= 0)
223 		j -= 25*off[0];
224 	else
225 		j -= 25*(off[0]+15);
226 	return (j);
227 }
228