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 * @(#)move.c 8.1 (Berkeley) 5/31/93
34 * $FreeBSD: src/games/backgammon/backgammon/move.c,v 1.5 1999/11/30 03:48:23 billf Exp $
35 * $DragonFly: src/games/backgammon/backgammon/move.c,v 1.2 2003/06/17 04:25:22 dillon Exp $
44 static char tests[20];
47 struct BOARD { /* structure of game position */
48 int b_board[26]; /* board position */
49 int b_in[2]; /* men in */
50 int b_off[2]; /* men off */
51 int b_st[4], b_fn[4]; /* moves */
53 struct BOARD *b_next; /* forward queue pointer */
56 struct BOARD *freeq = 0;
57 struct BOARD *checkq = 0;
58 struct BOARD *bsave();
59 struct BOARD *nextfree();
61 /* these variables are values for the
63 static int ch; /* chance of being hit */
64 static int op; /* computer's open men */
65 static int pt; /* comp's protected points */
66 static int em; /* farthest man back */
67 static int frc; /* chance to free comp's men */
68 static int frp; /* chance to free pl's men */
70 /* these values are the values for the
71 * move chosen (so far) */
72 static int chance; /* chance of being hit */
73 static int openmen; /* computer's open men */
74 static int points; /* comp's protected points */
75 static int endman; /* farthest man back */
76 static int barmen; /* men on bar */
77 static int menin; /* men in inner table */
78 static int menoff; /* men off board */
79 static int oldfrc; /* chance to free comp's men */
80 static int oldfrp; /* chance to free pl's men */
82 static int cp[5]; /* candidate start position */
83 static int cg[5]; /* candidate finish position */
85 static int race; /* game reduced to a race */
88 int okay; /* zero if first move */
94 /* see if comp should double */
95 if (gvalue < 64 && dlast != cturn && dblgood()) {
98 /* return if declined */
99 if (cturn != 1 && cturn != -1)
106 for (i = 0; i < 26; i++) {
110 for (i = 0; i < l; i++) {
119 curmove (cturn == -1? 18: 19,0);
125 /* make tty interruptable
131 /* find out how many moves */
134 writel (" but cannot use it.\n");
141 for (i = 0; i < 4; i++)
149 writel (" and moves ");
150 for (i = 0; i < mvlim; i++) {
153 wrint (p[i] = cp[i]);
155 wrint (g[i] = cg[i]);
160 /* print blots hit */
165 for (i = 0; i < mvlim; i++)
168 /* get ready for next move */
174 fixtty (raw); /* no more tty interrupt */
177 trymove (mvnum,swapped)
178 int mvnum; /* number of move (rel zero) */
179 int swapped; /* see if swapped also tested */
182 int pos; /* position on board */
183 int rval; /* value of roll */
185 /* if recursed through all dice
186 * values, compare move */
187 if (mvnum == mvlim) {
192 /* make sure dice in always
196 /* choose value for this move */
197 rval = dice[mvnum != 0];
199 /* find all legitimate moves */
200 for (pos = bar; pos != home; pos += cturn) {
201 /* fix order of dice */
204 /* break if stuck on bar */
205 if (board[bar] != 0 && pos != bar)
207 /* on to next if not occupied */
208 if (board[pos]*cturn <= 0)
210 /* set up arrays for move */
212 g[mvnum] = pos+rval*cturn;
213 if (g[mvnum]*cturn >= home) {
223 /* undo move to try another */
227 /* swap dice and try again */
228 if ((!swapped) && D0 != D1)
235 struct BOARD *now; /* current position */
237 now = nextfree (); /* get free BOARD */
240 for (i = 0; i < 26; i++)
241 now->b_board[i] = board[i];
242 now->b_in[0] = in[0];
243 now->b_in[1] = in[1];
244 now->b_off[0] = off[0];
245 now->b_off[1] = off[1];
246 for (i = 0; i < mvlim; i++) {
254 struct BOARD *new; /* item to insert */
256 struct BOARD *p = checkq; /* queue pointer */
257 int result; /* comparison result */
259 if (p == 0) { /* check if queue empty */
265 result = bcomp (new,p); /* compare to first element */
266 if (result < 0) { /* insert in front */
271 if (result == 0) { /* duplicate entry */
277 while (p->b_next != 0) { /* traverse queue */
278 result = bcomp (new,p->b_next);
279 if (result < 0) { /* found place */
280 new->b_next = p->b_next;
284 if (result == 0) { /* duplicate entry */
285 mvcheck (p->b_next,new);
291 /* place at end of queue */
300 int *aloc = a->b_board; /* pointer to board a */
301 int *bloc = b->b_board; /* pointer to board b */
303 int result; /* comparison result */
305 for (i = 0; i < 26; i++) { /* compare boards */
306 result = cturn*(aloc[i]-bloc[i]);
308 return (result); /* found inequality */
310 return (0); /* same position */
313 mvcheck (incumbent,candidate)
314 struct BOARD *incumbent;
315 struct BOARD *candidate;
320 for (i = 0; i < mvlim; i++) {
321 result = cturn*(candidate->b_st[i]-incumbent->b_st[i]);
329 for (i = 0; i < mvlim; i++) {
330 incumbent->b_st[i] = candidate->b_st[i];
331 incumbent->b_fn[i] = candidate->b_fn[i];
336 struct BOARD *dead; /* dead position */
338 dead->b_next = freeq; /* add to freeq */
347 new = (struct BOARD *)calloc (1,sizeof (struct BOARD));
349 writel ("\nOut of memory\n");
355 freeq = freeq->b_next;
361 /* current game position */
362 struct BOARD *now = bsave();
363 struct BOARD *next; /* next move */
367 trace = fopen ("bgtrace","w");
368 fprintf (trace,"\nRoll: %d %d%s\n",D0,D1,race? " (race)": "");
371 do { /* compare moves */
373 next = checkq->b_next;
377 } while (checkq != 0);
383 struct BOARD *s; /* game situation */
387 for (i = 0; i < 26; i++)
388 board[i] = s->b_board[i];
389 for (i = 0; i < 2; i++) {
391 off[i] = s->b_off[i];
393 for (i = 0; i < mvlim; i++) {
405 trace = fopen ("bgtrace","w");
411 for (i = 1; i < 25; i++) {
412 if (board[i] == cturn)
416 for (i = bar+cturn; i != home; i += cturn)
417 if (board[i]*cturn > 1)
419 frc = freemen (bar)+trapped (bar,cturn);
420 frp = freemen (home)+trapped (home,-cturn);
422 for (em = bar; em != home; em += cturn)
423 if (board[em]*cturn > 0)
427 fputs ("Board: ",trace);
428 for (i = 0; i < 26; i++)
429 fprintf (trace, " %d",board[i]);
431 fprintf (trace,"\n\tem = %d\n",em);
434 "\n\tch = %d, pt = %d, em = %d, frc = %d, frp = %d\n",
436 fputs ("\tMove: ",trace);
437 for (i = 0; i < mvlim; i++)
438 fprintf (trace," %d-%d",p[i],g[i]);
443 if ((cp[0] == 0 && cg[0] == 0) || movegood()) {
445 fprintf (trace,"\t[%s] ... wins.\n",tests);
448 for (i = 0; i < mvlim; i++) {
457 barmen = abs(board[home]);
466 fprintf (trace,"\t[%s] ... loses.\n",tests);
484 return (*offptr > menoff);
489 return (endman > em);
501 return (*inptr > menin);
504 n = barmen-abs(board[home]);
508 if (abs(chance-ch)+25*n > rnum(150))
509 return (n? (n < 0): (ch < chance));
514 return (*offptr > menoff);
518 if (abs(openmen-op) > 7+rnum(12))
519 return (openmen > op);
528 if (abs(endman-em) > rnum(2))
529 return (endman > em);
533 if (abs(frc-oldfrc) > rnum(2))
534 return (frc < oldfrc);
538 if (abs(n = pt-points) > rnum(4))
544 return (*inptr > menin);
548 if (abs(frp-oldfrp) > rnum(2))
549 return (frp > oldfrp);