xref: /openbsd-src/games/backgammon/common_source/table.c (revision d13be5d47e4149db2549a9828e244d59dbc43f15)
1 /*	$OpenBSD: table.c,v 1.8 2009/10/27 23:59:23 deraadt 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. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <ctype.h>
33 #include "back.h"
34 
35 const char   *const help2[] = {
36 	"   Enter moves as <s>-<f> or <s>/<r> where <s> is the starting",
37 	"position, <f> is the finishing position, and <r> is the roll.",
38 	"Remember, each die roll must be moved separately.",
39 	0
40 };
41 
42 struct state {
43 	char    ch;
44 	int     fcode;
45 	int     newst;
46 };
47 
48 static const struct state atmata[] = {
49 
50 	{'R', 1, 0},	{'?', 7, 0},	{'Q', 0, -3},	{'B', 8, 25},
51 	{'9', 2, 25},	{'8', 2, 25},	{'7', 2, 25},	{'6', 2, 25},
52 	{'5', 2, 25},	{'4', 2, 25},	{'3', 2, 25},	{'2', 2, 19},
53 	{'1', 2, 15},	{'0', 2, 25},	{'.', 0, 0},	{'9', 2, 25},
54 	{'8', 2, 25},	{'7', 2, 25},	{'6', 2, 25},	{'5', 2, 25},
55 
56 	{'4', 2, 25},	{'3', 2, 25},	{'2', 2, 25},	{'1', 2, 25},
57 	{'0', 2, 25},	{'/', 0, 32},	{'-', 0, 39},	{'.', 0, 0},
58 	{'/', 5, 32},	{' ', 6, 3},	{',', 6, 3},	{'\n', 0, -1},
59 	{'6', 3, 28},	{'5', 3, 28},	{'4', 3, 28},	{'3', 3, 28},
60 	{'2', 3, 28},	{'1', 3, 28},	{'.', 0, 0},	{'H', 9, 61},
61 
62 	{'9', 4, 61},	{'8', 4, 61},	{'7', 4, 61},	{'6', 4, 61},
63 	{'5', 4, 61},	{'4', 4, 61},	{'3', 4, 61},	{'2', 4, 53},
64 	{'1', 4, 51},	{'0', 4, 61},	{'.', 0, 0},	{'9', 4, 61},
65 	{'8', 4, 61},	{'7', 4, 61},	{'6', 4, 61},	{'5', 4, 61},
66 	{'4', 4, 61},	{'3', 4, 61},	{'2', 4, 61},	{'1', 4, 61},
67 
68 	{'0', 4, 61},	{' ', 6, 3},	{',', 6, 3},	{'-', 5, 39},
69 	{'\n', 0, -1},	{'.', 0, 0}
70 };
71 
72 int
73 checkmove(ist)
74 	int     ist;
75 {
76 	int     curr, curc;
77 	int     j, n;
78 	int    c;
79 
80 domove:
81 	getyx(stdscr, curr, curc);
82 	if (ist == 0)
83 		mvprintw(curr, 32, "Move:  ");
84 	ist = mvl = ncin = 0;
85 	for (j = 0; j < 5; j++)
86 		p[j] = g[j] = -1;
87 
88 dochar:
89 	c = readc();
90 
91 	if (c == 'S') {
92 		raflag = 0;
93 		save(1);
94 		move(cturn == -1 ? 18 : 19, 39);
95 		ist = -1;
96 		goto domove;
97 	}
98 	if ((c == KEY_BACKSPACE || c == 0177) && ncin > 0) {
99 		getyx(stdscr, curr, curc);
100 		move(curr, curc - 1);
101 		ncin--;
102 		n = rsetbrd();
103 		if (n == 0) {
104 			n = -1;
105 			refresh();
106 		}
107 		if ((ist = n) > 0)
108 			goto dochar;
109 		getyx(stdscr, curr, curc);
110 		move(curr, 39);
111 		clrtoeol();
112 		goto domove;
113 	} else if (c == KEY_DL && ncin > 0) {
114 		getyx(stdscr, curr, curc);
115 		move(curr, 39);
116 		clrtoeol();
117 		ist = -1;
118 		refresh();
119 		goto domove;
120 	}
121 	if (!isascii(c) || (ncin >= CIN_SIZE - 1)) {
122 		beep();
123 		goto domove;
124 	}
125 	n = dotable(c, ist);
126 	if (n >= 0) {
127 		cin[ncin++] = c;
128 		if (n > 2)
129 			if (c != '\n')
130 				addch(c);
131 		ist = n;
132 		if (n)
133 			goto dochar;
134 		else
135 			goto domove;
136 	}
137 	if (n == -1 && mvl >= mvlim)
138 		return(0);
139 	if (n == -1 && mvl < mvlim-1)
140 		return(-4);
141 	if (n == -6) {
142 		if (movokay(mvl + 1)) {
143 			moveplayers();
144 			movback(mvl + 1);
145 		} else
146 			move(cturn == -1 ? 18 : 19, ncin + 39);
147 		ist = n = rsetbrd();
148 		goto dochar;
149 	}
150 	if (n != -5)
151 		return(n);
152 	beep();
153 	goto dochar;
154 }
155 
156 int
157 dotable(c, i)
158 	char    c;
159 	int     i;
160 {
161 	int     a;
162 	int     test;
163 
164 	test = (c == 'R');
165 
166 	while ((a = atmata[i].ch) != '.') {
167 		if (a == c || (test && a == '\n')) {
168 			switch (atmata[i].fcode) {
169 			case 1:
170 				wrboard();
171 				move(cturn == -1 ? 18 : 19, 0);
172 				proll();
173 				addstr("\t\t");
174 				break;
175 
176 			case 2:
177 				if (p[mvl] == -1)
178 					p[mvl] = c - '0';
179 				else
180 					p[mvl] = p[mvl] * 10 + c - '0';
181 				break;
182 
183 			case 3:
184 				if (g[mvl] != -1) {
185 					if (mvl < mvlim)
186 						mvl++;
187 					p[mvl] = p[mvl - 1];
188 				}
189 				g[mvl] = p[mvl] + cturn * (c - '0');
190 				if (g[mvl] < 0)
191 					g[mvl] = 0;
192 				if (g[mvl] > 25)
193 					g[mvl] = 25;
194 				break;
195 
196 			case 4:
197 				if (g[mvl] == -1)
198 					g[mvl] = c - '0';
199 				else
200 					g[mvl] = g[mvl] * 10 + c - '0';
201 				break;
202 
203 			case 5:
204 				if (mvl < mvlim)
205 					mvl++;
206 				p[mvl] = g[mvl - 1];
207 				break;
208 
209 			case 6:
210 				if (mvl < mvlim)
211 					mvl++;
212 				break;
213 
214 			case 7:
215 				move(20, 0);
216 				text(help2);
217 				move(cturn == -1 ? 18 : 19, 39);
218 				break;
219 
220 			case 8:
221 				p[mvl] = bar;
222 				break;
223 
224 			case 9:
225 				g[mvl] = home;
226 			}
227 
228 			if (!test || a != '\n')
229 				return(atmata[i].newst);
230 			else
231 				return(-6);
232 		}
233 		i++;
234 	}
235 	return (-5);
236 }
237 
238 int
239 rsetbrd()
240 {
241 	int     i, j, n;
242 
243 	n = 0;
244 	mvl = 0;
245 	for (i = 0; i < 4; i++)
246 		p[i] = g[i] = -1;
247 	for (j = 0; j < ncin; j++)
248 		if ((n = dotable(cin[j], n)) < 0)
249 			return (n);
250 	return(n);
251 }
252