xref: /openbsd-src/games/backgammon/common_source/table.c (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 /*	$OpenBSD: table.c,v 1.4 2001/06/23 23:50:04 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[] = "@(#)table.c	8.1 (Berkeley) 5/31/93";
39 #else
40 static char rcsid[] = "$OpenBSD: table.c,v 1.4 2001/06/23 23:50:04 pjanzen Exp $";
41 #endif
42 #endif /* not lint */
43 
44 #include <ctype.h>
45 #include "back.h"
46 
47 const char   *const help2[] = {
48 	"   Enter moves as <s>-<f> or <s>/<r> where <s> is the starting",
49 	"position, <f> is the finishing position, and <r> is the roll.",
50 	"Remember, each die roll must be moved separately.",
51 	0
52 };
53 
54 struct state {
55 	char    ch;
56 	int     fcode;
57 	int     newst;
58 };
59 
60 static const struct state atmata[] = {
61 
62 	{'R', 1, 0},	{'?', 7, 0},	{'Q', 0, -3},	{'B', 8, 25},
63 	{'9', 2, 25},	{'8', 2, 25},	{'7', 2, 25},	{'6', 2, 25},
64 	{'5', 2, 25},	{'4', 2, 25},	{'3', 2, 25},	{'2', 2, 19},
65 	{'1', 2, 15},	{'0', 2, 25},	{'.', 0, 0},	{'9', 2, 25},
66 	{'8', 2, 25},	{'7', 2, 25},	{'6', 2, 25},	{'5', 2, 25},
67 
68 	{'4', 2, 25},	{'3', 2, 25},	{'2', 2, 25},	{'1', 2, 25},
69 	{'0', 2, 25},	{'/', 0, 32},	{'-', 0, 39},	{'.', 0, 0},
70 	{'/', 5, 32},	{' ', 6, 3},	{',', 6, 3},	{'\n', 0, -1},
71 	{'6', 3, 28},	{'5', 3, 28},	{'4', 3, 28},	{'3', 3, 28},
72 	{'2', 3, 28},	{'1', 3, 28},	{'.', 0, 0},	{'H', 9, 61},
73 
74 	{'9', 4, 61},	{'8', 4, 61},	{'7', 4, 61},	{'6', 4, 61},
75 	{'5', 4, 61},	{'4', 4, 61},	{'3', 4, 61},	{'2', 4, 53},
76 	{'1', 4, 51},	{'0', 4, 61},	{'.', 0, 0},	{'9', 4, 61},
77 	{'8', 4, 61},	{'7', 4, 61},	{'6', 4, 61},	{'5', 4, 61},
78 	{'4', 4, 61},	{'3', 4, 61},	{'2', 4, 61},	{'1', 4, 61},
79 
80 	{'0', 4, 61},	{' ', 6, 3},	{',', 6, 3},	{'-', 5, 39},
81 	{'\n', 0, -1},	{'.', 0, 0}
82 };
83 
84 int
85 checkmove(ist)
86 	int     ist;
87 {
88 	int     curr, curc;
89 	int     j, n;
90 	int    c;
91 
92 domove:
93 	getyx(stdscr, curr, curc);
94 	if (ist == 0)
95 		mvprintw(curr, 32, "Move:  ");
96 	ist = mvl = ncin = 0;
97 	for (j = 0; j < 5; j++)
98 		p[j] = g[j] = -1;
99 
100 dochar:
101 	c = readc();
102 
103 	if (c == 'S') {
104 		raflag = 0;
105 		save(1);
106 		move(cturn == -1 ? 18 : 19, 39);
107 		ist = -1;
108 		goto domove;
109 	}
110 	if ((c == KEY_BACKSPACE || c == 0177) && ncin > 0) {
111 		getyx(stdscr, curr, curc);
112 		move(curr, curc - 1);
113 		ncin--;
114 		n = rsetbrd();
115 		if (n == 0) {
116 			n = -1;
117 			refresh();
118 		}
119 		if ((ist = n) > 0)
120 			goto dochar;
121 		goto domove;
122 	}
123 	if (c == KEY_DL && ncin > 0) {
124 		refresh();
125 		getyx(stdscr, curr, curc);
126 		move(curr, 39);
127 		ist = -1;
128 		goto domove;
129 	}
130 	if (!isascii(c)) {
131 		beep();
132 		goto domove;
133 	}
134 	n = dotable(c, ist);
135 	if (n >= 0) {
136 		cin[ncin++] = c;
137 		if (n > 2)
138 		if (c != '\n')
139 			addch(c);
140 		ist = n;
141 		if (n)
142 			goto dochar;
143 		else
144 			goto domove;
145 	}
146 	if (n == -1 && mvl >= mvlim)
147 		return(0);
148 	if (n == -1 && mvl < mvlim-1)
149 		return(-4);
150 	if (n == -6) {
151 		if (movokay(mvl + 1)) {
152 			moveplayers();
153 			movback(mvl + 1);
154 		} else
155 			move(cturn == -1 ? 18 : 19, ncin + 39);
156 		ist = n = rsetbrd();
157 		goto dochar;
158 	}
159 	if (n != -5)
160 		return(n);
161 	beep();
162 	goto dochar;
163 }
164 
165 int
166 dotable(c, i)
167 	char    c;
168 	int     i;
169 {
170 	int     a;
171 	int     test;
172 
173 	test = (c == 'R');
174 
175 	while ((a = atmata[i].ch) != '.') {
176 		if (a == c || (test && a == '\n')) {
177 			switch (atmata[i].fcode) {
178 			case 1:
179 				wrboard();
180 				move(cturn == -1 ? 18 : 19, 0);
181 				proll();
182 				addstr("\t\t");
183 				break;
184 
185 			case 2:
186 				if (p[mvl] == -1)
187 					p[mvl] = c - '0';
188 				else
189 					p[mvl] = p[mvl] * 10 + c - '0';
190 				break;
191 
192 			case 3:
193 				if (g[mvl] != -1) {
194 					if (mvl < mvlim)
195 						mvl++;
196 					p[mvl] = p[mvl - 1];
197 				}
198 				g[mvl] = p[mvl] + cturn * (c - '0');
199 				if (g[mvl] < 0)
200 					g[mvl] = 0;
201 				if (g[mvl] > 25)
202 					g[mvl] = 25;
203 				break;
204 
205 			case 4:
206 				if (g[mvl] == -1)
207 					g[mvl] = c - '0';
208 				else
209 					g[mvl] = g[mvl] * 10 + c - '0';
210 				break;
211 
212 			case 5:
213 				if (mvl < mvlim)
214 					mvl++;
215 				p[mvl] = g[mvl - 1];
216 				break;
217 
218 			case 6:
219 				if (mvl < mvlim)
220 					mvl++;
221 				break;
222 
223 			case 7:
224 				move(20, 0);
225 				text(help2);
226 				move(cturn == -1 ? 18 : 19, 39);
227 				break;
228 
229 			case 8:
230 				p[mvl] = bar;
231 				break;
232 
233 			case 9:
234 				g[mvl] = home;
235 			}
236 
237 			if (!test || a != '\n')
238 				return(atmata[i].newst);
239 			else
240 				return(-6);
241 		}
242 		i++;
243 	}
244 	return (-5);
245 }
246 
247 int
248 rsetbrd()
249 {
250 	int     i, j, n;
251 
252 	n = 0;
253 	mvl = 0;
254 	for (i = 0; i < 4; i++)
255 		p[i] = g[i] = -1;
256 	for (j = 0; j < ncin; j++)
257 		n = dotable(cin[j], n);
258 	return(n);
259 }
260