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 this notice is preserved and that due credit is given
7  * to the University of California at Berkeley. The name of the University
8  * may not be used to endorse or promote products derived from this
9  * software without specific prior written permission. This software
10  * is provided ``as is'' without express or implied warranty.
11  */
12 
13 #ifndef lint
14 static char sccsid[] = "@(#)one.c	5.2 (Berkeley) 02/16/88";
15 #endif /* not lint */
16 
17 #include "back.h"
18 
19 makmove (i)
20 register int	i;
21 
22 {
23 	register int	n, d;
24 	int		max;
25 
26 	d = d0;
27 	n = abs(g[i]-p[i]);
28 	max = (*offptr < 0? 7: last());
29 	if (board[p[i]]*cturn <= 0)
30 		return (checkd(d)+2);
31 	if (g[i] != home && board[g[i]]*cturn < -1)
32 		return (checkd(d)+3);
33 	if (i || D0 == D1)  {
34 		if (n == max? D1 < n: D1 != n)
35 			return (checkd(d)+1);
36 	} else  {
37 		if (n == max? D0 < n && D1 < n: D0 != n && D1 != n)
38 			return (checkd(d)+1);
39 		if (n == max? D0 < n: D0 != n)  {
40 			if (d0)
41 				return (checkd(d)+1);
42 			swap;
43 		}
44 	}
45 	if (g[i] == home && *offptr < 0)
46 		return (checkd(d)+4);
47 	h[i] = 0;
48 	board[p[i]] -= cturn;
49 	if (g[i] != home)  {
50 		if (board[g[i]] == -cturn)  {
51 			board[home] -= cturn;
52 			board[g[i]] = 0;
53 			h[i] = 1;
54 			if (abs(bar-g[i]) < 7)  {
55 				(*inopp)--;
56 				if (*offopp >= 0)
57 					*offopp -= 15;
58 			}
59 		}
60 		board[g[i]] += cturn;
61 		if (abs(home-g[i]) < 7 && abs(home-p[i]) > 6)  {
62 			(*inptr)++;
63 			if (*inptr+*offptr == 0)
64 				*offptr += 15;
65 		}
66 	} else {
67 		(*offptr)++;
68 		(*inptr)--;
69 	}
70 	return (0);
71 }
72 
73 moverr (i)
74 register int	i;
75 
76 {
77 	register int	j;
78 
79 	if (tflag)
80 		curmove (20,0);
81 	else
82 		writec ('\n');
83 	writel ("Error:  ");
84 	for (j = 0; j <= i; j++)  {
85 		wrint (p[j]);
86 		writec ('-');
87 		wrint (g[j]);
88 		if (j < i)
89 			writec (',');
90 	}
91 	writel ("... ");
92 	movback (i);
93 }
94 
95 
96 checkd (d)
97 register int	d;
98 
99 {
100 	if (d0 != d)
101 		swap;
102 	return (0);
103 }
104 
105 last ()  {
106 	register int	i;
107 
108 	for (i = home-6*cturn; i != home; i += cturn)
109 		if (board[i]*cturn > 0)
110 			return (abs(home-i));
111 }
112 
113 movback (i)
114 register int	i;
115 
116 {
117 	register int	j;
118 
119 	for (j = i-1; j >= 0; j--)
120 		backone(j);
121 }
122 
123 backone (i)
124 register int	i;
125 
126 {
127 	board[p[i]] += cturn;
128 	if (g[i] != home)  {
129 		board[g[i]] -= cturn;
130 		if (abs(g[i]-home) < 7 && abs(p[i]-home) > 6)  {
131 			(*inptr)--;
132 			if (*inptr+*offptr < 15 && *offptr >= 0)
133 				*offptr -= 15;
134 		}
135 	} else  {
136 		(*offptr)--;
137 		(*inptr)++;
138 	}
139 	if (h[i])  {
140 		board[home] += cturn;
141 		board[g[i]] = -cturn;
142 		if (abs(bar-g[i]) < 7)  {
143 			(*inopp)++;
144 			if (*inopp+*offopp == 0)
145 				*offopp += 15;
146 		}
147 	}
148 }
149