Initial import from FreeBSD RELENG_4:
[dragonfly.git] / games / backgammon / common_source / one.c
1 /*
2  * Copyright (c) 1980, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
20  *
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
31  * SUCH DAMAGE.
32  */
33
34 #ifndef lint
35 #if 0
36 static char sccsid[] = "@(#)one.c       8.1 (Berkeley) 5/31/93";
37 #endif
38 static const char rcsid[] =
39  "$FreeBSD: src/games/backgammon/common_source/one.c,v 1.5 1999/11/30 03:48:27 billf Exp $";
40 #endif /* not lint */
41
42 #include "back.h"
43
44 makmove (i)
45 int     i;
46
47 {
48         int     n, d;
49         int             max;
50
51         d = d0;
52         n = abs(g[i]-p[i]);
53         max = (*offptr < 0? 7: last());
54         if (board[p[i]]*cturn <= 0)
55                 return (checkd(d)+2);
56         if (g[i] != home && board[g[i]]*cturn < -1)
57                 return (checkd(d)+3);
58         if (i || D0 == D1)  {
59                 if (n == max? D1 < n: D1 != n)
60                         return (checkd(d)+1);
61         } else  {
62                 if (n == max? D0 < n && D1 < n: D0 != n && D1 != n)
63                         return (checkd(d)+1);
64                 if (n == max? D0 < n: D0 != n)  {
65                         if (d0)
66                                 return (checkd(d)+1);
67                         swap;
68                 }
69         }
70         if (g[i] == home && *offptr < 0)
71                 return (checkd(d)+4);
72         h[i] = 0;
73         board[p[i]] -= cturn;
74         if (g[i] != home)  {
75                 if (board[g[i]] == -cturn)  {
76                         board[home] -= cturn;
77                         board[g[i]] = 0;
78                         h[i] = 1;
79                         if (abs(bar-g[i]) < 7)  {
80                                 (*inopp)--;
81                                 if (*offopp >= 0)
82                                         *offopp -= 15;
83                         }
84                 }
85                 board[g[i]] += cturn;
86                 if (abs(home-g[i]) < 7 && abs(home-p[i]) > 6)  {
87                         (*inptr)++;
88                         if (*inptr+*offptr == 0)
89                                 *offptr += 15;
90                 }
91         } else {
92                 (*offptr)++;
93                 (*inptr)--;
94         }
95         return (0);
96 }
97 \f
98 moverr (i)
99 int     i;
100
101 {
102         int     j;
103
104         if (tflag)
105                 curmove (20,0);
106         else
107                 writec ('\n');
108         writel ("Error:  ");
109         for (j = 0; j <= i; j++)  {
110                 wrint (p[j]);
111                 writec ('-');
112                 wrint (g[j]);
113                 if (j < i)
114                         writec (',');
115         }
116         writel ("... ");
117         movback (i);
118 }
119
120
121 checkd (d)
122 int     d;
123
124 {
125         if (d0 != d)
126                 swap;
127         return (0);
128 }
129
130 last ()  {
131         int     i;
132
133         for (i = home-6*cturn; i != home; i += cturn)
134                 if (board[i]*cturn > 0)
135                         return (abs(home-i));
136 }
137 \f
138 movback (i)
139 int     i;
140
141 {
142         int     j;
143
144         for (j = i-1; j >= 0; j--)
145                 backone(j);
146 }
147
148 backone (i)
149 int     i;
150
151 {
152         board[p[i]] += cturn;
153         if (g[i] != home)  {
154                 board[g[i]] -= cturn;
155                 if (abs(g[i]-home) < 7 && abs(p[i]-home) > 6)  {
156                         (*inptr)--;
157                         if (*inptr+*offptr < 15 && *offptr >= 0)
158                                 *offptr -= 15;
159                 }
160         } else  {
161                 (*offptr)--;
162                 (*inptr)++;
163         }
164         if (h[i])  {
165                 board[home] += cturn;
166                 board[g[i]] = -cturn;
167                 if (abs(bar-g[i]) < 7)  {
168                         (*inopp)++;
169                         if (*inopp+*offopp == 0)
170                                 *offopp += 15;
171                 }
172         }
173 }