1 /*	$OpenBSD: check.c,v 1.3 1999/07/31 21:57:40 pjanzen Exp $	*/
2 
3 /*
4  * Copyright (c) 1980, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #ifndef lint
37 #if 0
38 static char sccsid[] = "@(#)check.c	8.1 (Berkeley) 5/31/93";
39 #else
40 static char rcsid[] = "$OpenBSD: check.c,v 1.3 1999/07/31 21:57:40 pjanzen Exp $";
41 #endif
42 #endif /* not lint */
43 
44 #include "back.h"
45 
46 void
47 getmove()
48 {
49 	int     i, c;
50 
51 	c = 0;
52 	for (;;) {
53 		i = checkmove(c);
54 
55 		switch (i) {
56 		case -1:
57 			if (movokay(mvlim)) {
58 				if (tflag)
59 					curmove(20, 0);
60 				else
61 					writec('\n');
62 				for (i = 0; i < mvlim; i++)
63 					if (h[i])
64 						wrhit(g[i]);
65 				nexturn();
66 				if (*offopp == 15)
67 					cturn *= -2;
68 				if (tflag && pnum)
69 					bflag = pnum;
70 				return;
71 			}
72 		case -4:
73 		case 0:
74 			if (tflag)
75 				refresh();
76 			if (i != 0 && i != -4)
77 				break;
78 			if (tflag)
79 				curmove(20, 0);
80 			else
81 				writec('\n');
82 			writel(*Colorptr);
83 			if (i == -4)
84 				writel(" must make ");
85 			else
86 				writel(" can only make ");
87 			writec(mvlim + '0');
88 			writel(" move");
89 			if (mvlim > 1)
90 				writec('s');
91 			writec('.');
92 			writec('\n');
93 			break;
94 
95 		case -3:
96 			if (quit())
97 				return;
98 		}
99 
100 		if (!tflag)
101 			proll();
102 		else {
103 			curmove(cturn == -1 ? 18 : 19, 39);
104 			cline();
105 			c = -1;
106 		}
107 	}
108 }
109 
110 int
111 movokay(mv)
112 	int     mv;
113 {
114 	int     i, m;
115 
116 	if (d0)
117 		swap;
118 
119 	for (i = 0; i < mv; i++) {
120 		if (p[i] == g[i]) {
121 			moverr(i);
122 			curmove(20, 0);
123 			writel("Attempt to move to same location.\n");
124 			return(0);
125 		}
126 		if (cturn * (g[i] - p[i]) < 0) {
127 			moverr(i);
128 			curmove(20, 0);
129 			writel("Backwards move.\n");
130 			return(0);
131 		}
132 		if (abs(board[bar]) && p[i] != bar) {
133 			moverr(i);
134 			curmove(20, 0);
135 			writel("Men still on bar.\n");
136 			return(0);
137 		}
138 		if ((m = makmove(i))) {
139 			moverr(i);
140 			switch (m) {
141 			case 1:
142 				writel("Move not rolled.\n");
143 				break;
144 
145 			case 2:
146 				writel("Bad starting position.\n");
147 				break;
148 
149 			case 3:
150 				writel("Destination occupied.\n");
151 				break;
152 
153 			case 4:
154 				writel("Can't remove men yet.\n");
155 			}
156 			return(0);
157 		}
158 	}
159 	return(1);
160 }
161