1 /*	@(#)allow.c	8.1 (Berkeley) 5/31/93				*/
2 /*	$NetBSD: allow.c,v 1.8 2012/10/13 19:19:38 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 int
36 movallow(struct move *mm)
37 {
38 	int     i, m, iold;
39 	int     r;
40 
41 	if (mm->d0)
42 		mswap(mm);
43 	m = (mm->D0 == mm->D1 ? 4 : 2);
44 	for (i = 0; i < 4; i++)
45 		mm->p[i] = bar;
46 	i = iold = 0;
47 	while (i < m) {
48 		if (*offptr == 15)
49 			break;
50 		mm->h[i] = 0;
51 		if (board[bar]) {
52 			if (i == 1 || m == 4)
53 				mm->g[i] = bar + cturn * mm->D1;
54 			else
55 				mm->g[i] = bar + cturn * mm->D0;
56 			if ((r = makmove(mm, i)) != 0) {
57 				if (mm->d0 || m == 4)
58 					break;
59 				mswap(mm);
60 				movback(mm, i);
61 				if (i > iold)
62 					iold = i;
63 				for (i = 0; i < 4; i++)
64 					mm->p[i] = bar;
65 				i = 0;
66 			} else
67 				i++;
68 			continue;
69 		}
70 		if ((mm->p[i] += cturn) == home) {
71 			if (i > iold)
72 				iold = i;
73 			if (m == 2 && i) {
74 				movback(mm, i);
75 				mm->p[i--] = bar;
76 				if (mm->p[i] != bar)
77 					continue;
78 				else
79 					break;
80 			}
81 			if (mm->d0 || m == 4)
82 				break;
83 			mswap(mm);
84 			movback(mm, i);
85 			for (i = 0; i < 4; i++)
86 				mm->p[i] = bar;
87 			i = 0;
88 			continue;
89 		}
90 		if (i == 1 || m == 4)
91 			mm->g[i] = mm->p[i] + cturn * mm->D1;
92 		else
93 			mm->g[i] = mm->p[i] + cturn * mm->D0;
94 		if (mm->g[i] * cturn > home) {
95 			if (*offptr >= 0)
96 				mm->g[i] = home;
97 			else
98 				continue;
99 		}
100 		if (board[mm->p[i]] * cturn > 0 && (r = makmove(mm, i)) == 0)
101 			i++;
102 	}
103 	movback(mm, i);
104 	return (iold > i ? iold : i);
105 }
106