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