2 * Copyright (c) 1980, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)extra.c 8.1 (Berkeley) 5/31/93
34 * $FreeBSD: src/games/backgammon/backgammon/extra.c,v 1.4 1999/11/30 03:48:22 billf Exp $
35 * $DragonFly: src/games/backgammon/backgammon/extra.c,v 1.3 2006/08/08 16:36:11 pavalos Exp $
45 static int eval(void);
49 * Have the current player double and ask opponent to accept.
55 int resp; /* response to y/n */
58 writel (" doubles."); /* indicate double */
60 if (cturn == -pnum) { /* see if computer accepts */
61 if (dblgood()) { /* guess not */
62 writel (" Declined.\n");
64 cturn *= -2; /* indicate loss */
66 } else { /* computer accepts */
67 writel (" Accepted.\n");
68 gvalue *= 2; /* double game value */
76 /* ask if player accepts */
78 writel (cturn == 1? color[2]: color[3]);
81 /* get response from yorn,
82 * a "2" means he said "p"
84 if ((resp = yorn ('R')) == 2) {
85 writel (" Reprint.\n");
102 nexturn (); /* declined */
110 * Returns 1 if the computer would double in this position. This
111 * is not an exact science. The computer will decline a double that he
112 * would have made. Accumulated judgments are kept in the variable n,
113 * which is in "pips", i.e., the position of each man summed over all
114 * men, with opponent's totals negative. Thus, n should have a positive
115 * value of 7 for each move ahead, or a negative value of 7 for each one
122 int n; /* accumulated judgment */
123 int OFFC = *offptr; /* no. of computer's men off */
124 int OFFO = *offopp; /* no. of player's men off */
129 trace = fopen ("bgtrace","w");
132 /* get real pip value */
135 fputs ("\nDoubles:\nBoard: ",trace);
136 for (i = 0; i < 26; i++)
137 fprintf (trace," %d",board[i]);
138 fprintf (trace,"\n\tpip = %d, ",n);
141 /* below adjusts pip value
142 * according to position
145 /* check men moving off
147 if (OFFC > -15 || OFFO > -15) {
148 if (OFFC < 0 && OFFO < 0) {
151 n +=((OFFC-OFFO)*7)/2;
152 } else if (OFFC < 0) {
155 } else if (OFFO < 0) {
159 if (OFFC < 8 && OFFO > 8)
161 if (OFFC < 10 && OFFO > 10)
163 if (OFFC < 12 && OFFO > 12)
165 if (OFFO < 8 && OFFC > 8)
167 if (OFFO < 10 && OFFC > 10)
169 if (OFFO < 12 && OFFC > 12)
171 n += ((OFFC-OFFO)*7)/2;
175 fprintf (trace,"off = %d, ",n);
178 /* see if men are trapped */
181 n += trapped(home,-cturn);
182 n -= trapped(bar,cturn);
185 fprintf (trace,"free = %d\n",n);
186 fprintf (trace,"\tOFFC = %d, OFFO = %d\n",OFFC,OFFO);
190 /* double if 2-3 moves ahead */
204 inc = (b == 0? 1: -1);
205 lim = (b == 0? 7: 18);
206 for (i = b+inc; i != lim; i += inc)
207 if (board[i]*inc < -1)
208 odds(abs(b-i),0,abs(board[b]));
209 if (abs(board[b]) == 1)
210 return ((36-count())/5);
215 trapped(int n, int inc)
222 for (i = n+inc; i != l; i += inc) {
225 if (board[i]*inc > 0) {
226 for (j = c; j < 13; j++)
227 if (board[i+inc*j]*inc < -1) {
230 for (k = 1; k < 7 && k < j; k++)
234 ct += abs(board[i])*(36-count());
245 for (j = i = 0; i < 26; i++)
246 j += (board[i] >= 0 ? i*board[i] : (25-i)*board[i]);