xref: /dragonfly/games/backgammon/common_source/one.c (revision 4318c66eac379e15105fe145d406dfef81b795f6)
1 /*        @(#)one.c 8.1 (Berkeley) 5/31/93                            */
2 /*        $NetBSD: one.c,v 1.9 2012/10/13 19:19:39 dholland Exp $     */
3 
4 /*
5  * Copyright (c) 1980, 1993
6  *        The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #include "back.h"
34 
35 static int checkd(struct move *, int);
36 static int last(void);
37 
38 int
makmove(struct move * mm,int i)39 makmove(struct move *mm, int i)
40 {
41           int     n, d;
42           int     max;
43 
44           d = mm->d0;
45           n = abs(mm->g[i] - mm->p[i]);
46           max = (*offptr < 0 ? 7 : last());
47           if (board[mm->p[i]] * cturn <= 0)
48                     return (checkd(mm, d) + 2);
49           if (mm->g[i] != home && board[mm->g[i]] * cturn < -1)
50                     return (checkd(mm, d) + 3);
51           if (i || mm->D0 == mm->D1) {
52                     if (n == max ? mm->D1 < n : mm->D1 != n)
53                               return (checkd(mm, d) + 1);
54           } else {
55                     if (n == max ? mm->D0 < n && mm->D1 < n : mm->D0 != n && mm->D1 != n)
56                               return (checkd(mm, d) + 1);
57                     if (n == max ? mm->D0 < n : mm->D0 != n) {
58                               if (mm->d0)
59                                         return (checkd(mm, d) + 1);
60                               mswap(mm);
61                     }
62           }
63           if (mm->g[i] == home && *offptr < 0)
64                     return (checkd(mm, d) + 4);
65           mm->h[i] = 0;
66           board[mm->p[i]] -= cturn;
67           if (mm->g[i] != home) {
68                     if (board[mm->g[i]] == -cturn) {
69                               board[home] -= cturn;
70                               board[mm->g[i]] = 0;
71                               mm->h[i] = 1;
72                               if (abs(bar - mm->g[i]) < 7) {
73                                         (*inopp)--;
74                                         if (*offopp >= 0)
75                                                   *offopp -= 15;
76                               }
77                     }
78                     board[mm->g[i]] += cturn;
79                     if (abs(home - mm->g[i]) < 7 && abs(home - mm->p[i]) > 6) {
80                               (*inptr)++;
81                               if (*inptr + *offptr == 0)
82                                         *offptr += 15;
83                     }
84           } else {
85                     (*offptr)++;
86                     (*inptr)--;
87           }
88           return (0);
89 }
90 
91 void
moverr(struct move * mm,int i)92 moverr(struct move *mm, int i)
93 {
94           int     j;
95 
96           if (tflag)
97                     curmove(20, 0);
98           else
99                     writec('\n');
100           writel("Error:  ");
101           for (j = 0; j <= i; j++) {
102                     wrint(mm->p[j]);
103                     writec('-');
104                     wrint(mm->g[j]);
105                     if (j < i)
106                               writec(',');
107           }
108           writel("... ");
109           movback(mm, i);
110 }
111 
112 
113 static int
checkd(struct move * mm,int d)114 checkd(struct move *mm, int d)
115 {
116           if (mm->d0 != d)
117                     mswap(mm);
118           return (0);
119 }
120 
121 static int
last(void)122 last(void)
123 {
124           int     i;
125 
126           for (i = home - 6 * cturn; i != home; i += cturn)
127                     if (board[i] * cturn > 0)
128                               return (abs(home - i));
129           return (-1);
130 }
131 
132 void
movback(struct move * mm,int i)133 movback(struct move *mm, int i)
134 {
135           int     j;
136 
137           for (j = i - 1; j >= 0; j--)
138                     backone(mm, j);
139 }
140 
141 void
backone(struct move * mm,int i)142 backone(struct move *mm, int i)
143 {
144           board[mm->p[i]] += cturn;
145           if (mm->g[i] != home) {
146                     board[mm->g[i]] -= cturn;
147                     if (abs(mm->g[i] - home) < 7 && abs(mm->p[i] - home) > 6) {
148                               (*inptr)--;
149                               if (*inptr + *offptr < 15 && *offptr >= 0)
150                                         *offptr -= 15;
151                     }
152           } else {
153                     (*offptr)--;
154                     (*inptr)++;
155           }
156           if (mm->h[i]) {
157                     board[home] += cturn;
158                     board[mm->g[i]] = -cturn;
159                     if (abs(bar - mm->g[i]) < 7) {
160                               (*inopp)++;
161                               if (*inopp + *offopp == 0)
162                                         *offopp += 15;
163                     }
164           }
165 }
166