1 /*
2  * Copyright (c) 1980 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 
18 #ifndef lint
19 static char sccsid[] = "@(#)one.c	5.3 (Berkeley) 06/18/88";
20 #endif /* not lint */
21 
22 #include "back.h"
23 
24 makmove (i)
25 register int	i;
26 
27 {
28 	register int	n, d;
29 	int		max;
30 
31 	d = d0;
32 	n = abs(g[i]-p[i]);
33 	max = (*offptr < 0? 7: last());
34 	if (board[p[i]]*cturn <= 0)
35 		return (checkd(d)+2);
36 	if (g[i] != home && board[g[i]]*cturn < -1)
37 		return (checkd(d)+3);
38 	if (i || D0 == D1)  {
39 		if (n == max? D1 < n: D1 != n)
40 			return (checkd(d)+1);
41 	} else  {
42 		if (n == max? D0 < n && D1 < n: D0 != n && D1 != n)
43 			return (checkd(d)+1);
44 		if (n == max? D0 < n: D0 != n)  {
45 			if (d0)
46 				return (checkd(d)+1);
47 			swap;
48 		}
49 	}
50 	if (g[i] == home && *offptr < 0)
51 		return (checkd(d)+4);
52 	h[i] = 0;
53 	board[p[i]] -= cturn;
54 	if (g[i] != home)  {
55 		if (board[g[i]] == -cturn)  {
56 			board[home] -= cturn;
57 			board[g[i]] = 0;
58 			h[i] = 1;
59 			if (abs(bar-g[i]) < 7)  {
60 				(*inopp)--;
61 				if (*offopp >= 0)
62 					*offopp -= 15;
63 			}
64 		}
65 		board[g[i]] += cturn;
66 		if (abs(home-g[i]) < 7 && abs(home-p[i]) > 6)  {
67 			(*inptr)++;
68 			if (*inptr+*offptr == 0)
69 				*offptr += 15;
70 		}
71 	} else {
72 		(*offptr)++;
73 		(*inptr)--;
74 	}
75 	return (0);
76 }
77 
78 moverr (i)
79 register int	i;
80 
81 {
82 	register int	j;
83 
84 	if (tflag)
85 		curmove (20,0);
86 	else
87 		writec ('\n');
88 	writel ("Error:  ");
89 	for (j = 0; j <= i; j++)  {
90 		wrint (p[j]);
91 		writec ('-');
92 		wrint (g[j]);
93 		if (j < i)
94 			writec (',');
95 	}
96 	writel ("... ");
97 	movback (i);
98 }
99 
100 
101 checkd (d)
102 register int	d;
103 
104 {
105 	if (d0 != d)
106 		swap;
107 	return (0);
108 }
109 
110 last ()  {
111 	register int	i;
112 
113 	for (i = home-6*cturn; i != home; i += cturn)
114 		if (board[i]*cturn > 0)
115 			return (abs(home-i));
116 }
117 
118 movback (i)
119 register int	i;
120 
121 {
122 	register int	j;
123 
124 	for (j = i-1; j >= 0; j--)
125 		backone(j);
126 }
127 
128 backone (i)
129 register int	i;
130 
131 {
132 	board[p[i]] += cturn;
133 	if (g[i] != home)  {
134 		board[g[i]] -= cturn;
135 		if (abs(g[i]-home) < 7 && abs(p[i]-home) > 6)  {
136 			(*inptr)--;
137 			if (*inptr+*offptr < 15 && *offptr >= 0)
138 				*offptr -= 15;
139 		}
140 	} else  {
141 		(*offptr)--;
142 		(*inptr)++;
143 	}
144 	if (h[i])  {
145 		board[home] += cturn;
146 		board[g[i]] = -cturn;
147 		if (abs(bar-g[i]) < 7)  {
148 			(*inopp)++;
149 			if (*inopp+*offopp == 0)
150 				*offopp += 15;
151 		}
152 	}
153 }
154