]> git.cameronkatri.com Git - bsdgames-darwin.git/blob - backgammon/common_source/table.c
Crack down on global variables with single-letter names.
[bsdgames-darwin.git] / backgammon / common_source / table.c
1 /* $NetBSD: table.c,v 1.12 2012/10/13 18:44:15 dholland 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 <sys/cdefs.h>
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)table.c 8.1 (Berkeley) 5/31/93";
36 #else
37 __RCSID("$NetBSD: table.c,v 1.12 2012/10/13 18:44:15 dholland Exp $");
38 #endif
39 #endif /* not lint */
40
41 #include "back.h"
42
43 static const char *const help2[] = {
44 " Enter moves as <s>-<f> or <s>/<r> where <s> is the starting",
45 "position, <f> is the finishing position, and <r> is the roll.",
46 "Remember, each die roll must be moved separately.",
47 0
48 };
49
50 struct state {
51 char ch;
52 int fcode;
53 int newst;
54 };
55
56 static int mvl; /* working copy of move->mvlim */
57
58 static const struct state atmata[] = {
59
60 {'R', 1, 0}, {'?', 7, 0}, {'Q', 0, -3}, {'B', 8, 25},
61 {'9', 2, 25}, {'8', 2, 25}, {'7', 2, 25}, {'6', 2, 25},
62 {'5', 2, 25}, {'4', 2, 25}, {'3', 2, 25}, {'2', 2, 19},
63 {'1', 2, 15}, {'0', 2, 25}, {'.', 0, 0}, {'9', 2, 25},
64 {'8', 2, 25}, {'7', 2, 25}, {'6', 2, 25}, {'5', 2, 25},
65
66 {'4', 2, 25}, {'3', 2, 25}, {'2', 2, 25}, {'1', 2, 25},
67 {'0', 2, 25}, {'/', 0, 32}, {'-', 0, 39}, {'.', 0, 0},
68 {'/', 5, 32}, {' ', 6, 3}, {',', 6, 3}, {'\n', 0, -1},
69 {'6', 3, 28}, {'5', 3, 28}, {'4', 3, 28}, {'3', 3, 28},
70 {'2', 3, 28}, {'1', 3, 28}, {'.', 0, 0}, {'H', 9, 61},
71
72 {'9', 4, 61}, {'8', 4, 61}, {'7', 4, 61}, {'6', 4, 61},
73 {'5', 4, 61}, {'4', 4, 61}, {'3', 4, 61}, {'2', 4, 53},
74 {'1', 4, 51}, {'0', 4, 61}, {'.', 0, 0}, {'9', 4, 61},
75 {'8', 4, 61}, {'7', 4, 61}, {'6', 4, 61}, {'5', 4, 61},
76 {'4', 4, 61}, {'3', 4, 61}, {'2', 4, 61}, {'1', 4, 61},
77
78 {'0', 4, 61}, {' ', 6, 3}, {',', 6, 3}, {'-', 5, 39},
79 {'\n', 0, -1}, {'.', 0, 0}
80 };
81
82 static int dotable(int, int);
83 static int rsetbrd(void);
84
85 int
86 checkmove(int ist)
87 {
88 int j, n;
89 char c;
90 struct move *mm = &gm;
91
92 domove:
93 if (ist == 0) {
94 if (tflag)
95 curmove(curr, 32);
96 else
97 writel("\t\t");
98 writel("Move: ");
99 }
100 ist = mvl = ncin = 0;
101 for (j = 0; j < 5; j++)
102 mm->p[j] = mm->g[j] = -1;
103
104 dochar:
105 c = readc();
106
107 if (c == 'S') {
108 raflag = 0;
109 save(1);
110 if (tflag) {
111 curmove(cturn == -1 ? 18 : 19, 39);
112 ist = -1;
113 goto domove;
114 } else {
115 proll();
116 ist = 0;
117 goto domove;
118 }
119 }
120 if (c == old.c_cc[VERASE] && ncin > 0) {
121 if (tflag)
122 curmove(curr, curc - 1);
123 else {
124 if (old.c_cc[VERASE] == '\010')
125 writel("\010 \010");
126 else
127 writec(cin[ncin - 1]);
128 }
129 ncin--;
130 n = rsetbrd();
131 if (n == 0) {
132 n = -1;
133 if (tflag)
134 refresh();
135 }
136 if ((ist = n) > 0)
137 goto dochar;
138 goto domove;
139 }
140 if (c == old.c_cc[VKILL] && ncin > 0) {
141 if (tflag) {
142 refresh();
143 curmove(curr, 39);
144 ist = -1;
145 goto domove;
146 } else
147 if (old.c_cc[VERASE] == '\010') {
148 for (j = 0; j < ncin; j++)
149 writel("\010 \010");
150 ist = -1;
151 goto domove;
152 } else {
153 writec('\\');
154 writec('\n');
155 proll();
156 ist = 0;
157 goto domove;
158 }
159 }
160 n = dotable(c, ist);
161 if (n >= 0) {
162 cin[ncin++] = c;
163 if (n > 2)
164 if ((!tflag) || c != '\n')
165 writec(c);
166 ist = n;
167 if (n)
168 goto dochar;
169 else
170 goto domove;
171 }
172 if (n == -1 && mvl >= mm->mvlim)
173 return (0);
174 if (n == -1 && mvl < mm->mvlim - 1)
175 return (-4);
176
177 if (n == -6) {
178 if (!tflag) {
179 if (movokay(mvl + 1)) {
180 wrboard();
181 movback(mvl + 1);
182 }
183 proll();
184 writel("\t\tMove: ");
185 for (j = 0; j < ncin;)
186 writec(cin[j++]);
187 } else {
188 if (movokay(mvl + 1)) {
189 refresh();
190 movback(mvl + 1);
191 } else
192 curmove(cturn == -1 ? 18 : 19, ncin + 39);
193 }
194 ist = n = rsetbrd();
195 goto dochar;
196 }
197 if (n != -5)
198 return (n);
199 writec('\007');
200 goto dochar;
201 }
202
203 static int
204 dotable(int c, int i)
205 {
206 int a;
207 int test;
208 struct move *mm = &gm;
209
210 test = (c == 'R');
211
212 while ((a = atmata[i].ch) != '.') {
213 if (a == c || (test && a == '\n')) {
214 switch (atmata[i].fcode) {
215
216 case 1:
217 wrboard();
218 if (tflag) {
219 curmove(cturn == -1 ? 18 : 19, 0);
220 proll();
221 writel("\t\t");
222 } else
223 proll();
224 break;
225
226 case 2:
227 if (mm->p[mvl] == -1)
228 mm->p[mvl] = c - '0';
229 else
230 mm->p[mvl] = mm->p[mvl] * 10 + c - '0';
231 break;
232
233 case 3:
234 if (mm->g[mvl] != -1) {
235 if (mvl < mm->mvlim)
236 mvl++;
237 mm->p[mvl] = mm->p[mvl - 1];
238 }
239 mm->g[mvl] = mm->p[mvl] + cturn * (c - '0');
240 if (mm->g[mvl] < 0)
241 mm->g[mvl] = 0;
242 if (mm->g[mvl] > 25)
243 mm->g[mvl] = 25;
244 break;
245
246 case 4:
247 if (mm->g[mvl] == -1)
248 mm->g[mvl] = c - '0';
249 else
250 mm->g[mvl] = mm->g[mvl] * 10 + c - '0';
251 break;
252
253 case 5:
254 if (mvl < mm->mvlim)
255 mvl++;
256 mm->p[mvl] = mm->g[mvl - 1];
257 break;
258
259 case 6:
260 if (mvl < mm->mvlim)
261 mvl++;
262 break;
263
264 case 7:
265 if (tflag)
266 curmove(20, 0);
267 else
268 writec('\n');
269 (void) wrtext(help2);
270 if (tflag) {
271 curmove(cturn == -1 ? 18 : 19, 39);
272 } else {
273 writec('\n');
274 proll();
275 writel("\t\tMove: ");
276 }
277 break;
278
279 case 8:
280 mm->p[mvl] = bar;
281 break;
282
283 case 9:
284 mm->g[mvl] = home;
285 }
286
287 if (!test || a != '\n')
288 return (atmata[i].newst);
289 else
290 return (-6);
291 }
292 i++;
293 }
294
295 return (-5);
296 }
297
298 static int
299 rsetbrd(void)
300 {
301 int i, j, n;
302 struct move *mm = &gm;
303
304 n = 0;
305 mvl = 0;
306 for (i = 0; i < 4; i++)
307 mm->p[i] = mm->g[i] = -1;
308 for (j = 0; j < ncin; j++)
309 if ((n = dotable(cin[j], n)) < 0)
310 return n;
311 return (n);
312 }