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