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
36 static char sccsid[] = "@(#)move.c 8.1 (Berkeley) 5/31/93";
38 static const char rcsid[] =
39 "$FreeBSD: src/games/backgammon/backgammon/move.c,v 1.5 1999/11/30 03:48:23 billf Exp $";
48 static char tests[20];
51 struct BOARD { /* structure of game position */
52 int b_board[26]; /* board position */
53 int b_in[2]; /* men in */
54 int b_off[2]; /* men off */
55 int b_st[4], b_fn[4]; /* moves */
57 struct BOARD *b_next; /* forward queue pointer */
60 struct BOARD *freeq = 0;
61 struct BOARD *checkq = 0;
62 struct BOARD *bsave();
63 struct BOARD *nextfree();
65 /* these variables are values for the
67 static int ch; /* chance of being hit */
68 static int op; /* computer's open men */
69 static int pt; /* comp's protected points */
70 static int em; /* farthest man back */
71 static int frc; /* chance to free comp's men */
72 static int frp; /* chance to free pl's men */
74 /* these values are the values for the
75 * move chosen (so far) */
76 static int chance; /* chance of being hit */
77 static int openmen; /* computer's open men */
78 static int points; /* comp's protected points */
79 static int endman; /* farthest man back */
80 static int barmen; /* men on bar */
81 static int menin; /* men in inner table */
82 static int menoff; /* men off board */
83 static int oldfrc; /* chance to free comp's men */
84 static int oldfrp; /* chance to free pl's men */
86 static int cp[5]; /* candidate start position */
87 static int cg[5]; /* candidate finish position */
89 static int race; /* game reduced to a race */
92 int okay; /* zero if first move */
98 /* see if comp should double */
99 if (gvalue < 64 && dlast != cturn && dblgood()) {
102 /* return if declined */
103 if (cturn != 1 && cturn != -1)
110 for (i = 0; i < 26; i++) {
114 for (i = 0; i < l; i++) {
123 curmove (cturn == -1? 18: 19,0);
129 /* make tty interruptable
135 /* find out how many moves */
138 writel (" but cannot use it.\n");
145 for (i = 0; i < 4; i++)
153 writel (" and moves ");
154 for (i = 0; i < mvlim; i++) {
157 wrint (p[i] = cp[i]);
159 wrint (g[i] = cg[i]);
164 /* print blots hit */
169 for (i = 0; i < mvlim; i++)
172 /* get ready for next move */
178 fixtty (raw); /* no more tty interrupt */
181 trymove (mvnum,swapped)
182 int mvnum; /* number of move (rel zero) */
183 int swapped; /* see if swapped also tested */
186 int pos; /* position on board */
187 int rval; /* value of roll */
189 /* if recursed through all dice
190 * values, compare move */
191 if (mvnum == mvlim) {
196 /* make sure dice in always
200 /* choose value for this move */
201 rval = dice[mvnum != 0];
203 /* find all legitimate moves */
204 for (pos = bar; pos != home; pos += cturn) {
205 /* fix order of dice */
208 /* break if stuck on bar */
209 if (board[bar] != 0 && pos != bar)
211 /* on to next if not occupied */
212 if (board[pos]*cturn <= 0)
214 /* set up arrays for move */
216 g[mvnum] = pos+rval*cturn;
217 if (g[mvnum]*cturn >= home) {
227 /* undo move to try another */
231 /* swap dice and try again */
232 if ((!swapped) && D0 != D1)
239 struct BOARD *now; /* current position */
241 now = nextfree (); /* get free BOARD */
244 for (i = 0; i < 26; i++)
245 now->b_board[i] = board[i];
246 now->b_in[0] = in[0];
247 now->b_in[1] = in[1];
248 now->b_off[0] = off[0];
249 now->b_off[1] = off[1];
250 for (i = 0; i < mvlim; i++) {
258 struct BOARD *new; /* item to insert */
260 struct BOARD *p = checkq; /* queue pointer */
261 int result; /* comparison result */
263 if (p == 0) { /* check if queue empty */
269 result = bcomp (new,p); /* compare to first element */
270 if (result < 0) { /* insert in front */
275 if (result == 0) { /* duplicate entry */
281 while (p->b_next != 0) { /* traverse queue */
282 result = bcomp (new,p->b_next);
283 if (result < 0) { /* found place */
284 new->b_next = p->b_next;
288 if (result == 0) { /* duplicate entry */
289 mvcheck (p->b_next,new);
295 /* place at end of queue */
304 int *aloc = a->b_board; /* pointer to board a */
305 int *bloc = b->b_board; /* pointer to board b */
307 int result; /* comparison result */
309 for (i = 0; i < 26; i++) { /* compare boards */
310 result = cturn*(aloc[i]-bloc[i]);
312 return (result); /* found inequality */
314 return (0); /* same position */
317 mvcheck (incumbent,candidate)
318 struct BOARD *incumbent;
319 struct BOARD *candidate;
324 for (i = 0; i < mvlim; i++) {
325 result = cturn*(candidate->b_st[i]-incumbent->b_st[i]);
333 for (i = 0; i < mvlim; i++) {
334 incumbent->b_st[i] = candidate->b_st[i];
335 incumbent->b_fn[i] = candidate->b_fn[i];
340 struct BOARD *dead; /* dead position */
342 dead->b_next = freeq; /* add to freeq */
351 new = (struct BOARD *)calloc (1,sizeof (struct BOARD));
353 writel ("\nOut of memory\n");
359 freeq = freeq->b_next;
365 /* current game position */
366 struct BOARD *now = bsave();
367 struct BOARD *next; /* next move */
371 trace = fopen ("bgtrace","w");
372 fprintf (trace,"\nRoll: %d %d%s\n",D0,D1,race? " (race)": "");
375 do { /* compare moves */
377 next = checkq->b_next;
381 } while (checkq != 0);
387 struct BOARD *s; /* game situation */
391 for (i = 0; i < 26; i++)
392 board[i] = s->b_board[i];
393 for (i = 0; i < 2; i++) {
395 off[i] = s->b_off[i];
397 for (i = 0; i < mvlim; i++) {
409 trace = fopen ("bgtrace","w");
415 for (i = 1; i < 25; i++) {
416 if (board[i] == cturn)
420 for (i = bar+cturn; i != home; i += cturn)
421 if (board[i]*cturn > 1)
423 frc = freemen (bar)+trapped (bar,cturn);
424 frp = freemen (home)+trapped (home,-cturn);
426 for (em = bar; em != home; em += cturn)
427 if (board[em]*cturn > 0)
431 fputs ("Board: ",trace);
432 for (i = 0; i < 26; i++)
433 fprintf (trace, " %d",board[i]);
435 fprintf (trace,"\n\tem = %d\n",em);
438 "\n\tch = %d, pt = %d, em = %d, frc = %d, frp = %d\n",
440 fputs ("\tMove: ",trace);
441 for (i = 0; i < mvlim; i++)
442 fprintf (trace," %d-%d",p[i],g[i]);
447 if ((cp[0] == 0 && cg[0] == 0) || movegood()) {
449 fprintf (trace,"\t[%s] ... wins.\n",tests);
452 for (i = 0; i < mvlim; i++) {
461 barmen = abs(board[home]);
470 fprintf (trace,"\t[%s] ... loses.\n",tests);
488 return (*offptr > menoff);
493 return (endman > em);
505 return (*inptr > menin);
508 n = barmen-abs(board[home]);
512 if (abs(chance-ch)+25*n > rnum(150))
513 return (n? (n < 0): (ch < chance));
518 return (*offptr > menoff);
522 if (abs(openmen-op) > 7+rnum(12))
523 return (openmen > op);
532 if (abs(endman-em) > rnum(2))
533 return (endman > em);
537 if (abs(frc-oldfrc) > rnum(2))
538 return (frc < oldfrc);
542 if (abs(n = pt-points) > rnum(4))
548 return (*inptr > menin);
552 if (abs(frp-oldfrp) > rnum(2))
553 return (frp > oldfrp);