subs.c revision 1.3 1 /* $NetBSD: subs.c,v 1.3 1995/03/21 15:05:54 cgd 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[] = "@(#)subs.c 8.1 (Berkeley) 5/31/93";
39 #else
40 static char rcsid[] = "$NetBSD: subs.c,v 1.3 1995/03/21 15:05:54 cgd Exp $";
41 #endif
42 #endif /* not lint */
43
44 #include <stdio.h>
45 #include "back.h"
46
47 int buffnum;
48 char outbuff[BUFSIZ];
49
50 static char plred[] = "Player is red, computer is white.";
51 static char plwhite[] = "Player is white, computer is red.";
52 static char nocomp[] = "(No computer play.)";
53
54 char *descr[] = {
55 "Usage: backgammon [-] [n r w b pr pw pb t3a]\n",
56 "\t-\tgets this list\n\tn\tdon't ask for rules or instructions",
57 "\tr\tplayer is red (implies n)\n\tw\tplayer is white (implies n)",
58 "\tb\ttwo players, red and white (implies n)",
59 "\tpr\tprint the board before red's turn",
60 "\tpw\tprint the board before white's turn",
61 "\tpb\tprint the board before both player's turn",
62 "\tterm\tterminal is a term",
63 "\tsfile\trecover saved game from file",
64 0
65 };
66
67 errexit (s)
68 register char *s;
69 {
70 write (2,"\n",1);
71 perror (s);
72 getout();
73 }
74
75 strset (s1,s2)
76 register char *s1, *s2;
77 {
78 while ( (*s1++ = *s2++) != '\0');
79 }
80
81 addbuf (c)
82 register char c;
83
84 {
85 buffnum++;
86 if (buffnum == BUFSIZ) {
87 if (write(1,outbuff,BUFSIZ) != BUFSIZ)
88 errexit ("addbuf (write):");
89 buffnum = 0;
90 }
91 outbuff[buffnum] = c;
92 }
93
94 buflush () {
95 if (buffnum < 0)
96 return;
97 buffnum++;
98 if (write (1,outbuff,buffnum) != buffnum)
99 errexit ("buflush (write):");
100 buffnum = -1;
101 }
102
103 readc () {
104 char c;
105
106 if (tflag) {
107 cline();
108 newpos();
109 }
110 buflush();
111 if (read(0,&c,1) != 1)
112 errexit ("readc");
113 #ifdef WHY_IS_THIS_HARDWIRED_IN_HERE
114 if (c == '\177')
115 getout();
116 #endif
117 if (c == '\033' || c == '\015')
118 return ('\n');
119 if (cflag)
120 return (c);
121 if (c == '\014')
122 return ('R');
123 if (c >= 'a' && c <= 'z')
124 return (c & 0137);
125 return (c);
126 }
127
128 writec (c)
129 char c;
130 {
131 if (tflag)
132 fancyc (c);
133 else
134 addbuf (c);
135 }
136
137 writel (l)
138 register char *l;
139 {
140 #ifdef DEBUG
141 register char *s;
142
143 if (trace == NULL)
144 trace = fopen ("bgtrace","w");
145
146 fprintf (trace,"writel: \"");
147 for (s = l; *s; s++) {
148 if (*s < ' ' || *s == '\177')
149 fprintf (trace,"^%c",(*s)^0100);
150 else
151 putc (*s,trace);
152 }
153 fprintf (trace,"\"\n");
154 fflush (trace);
155 #endif
156
157 while (*l)
158 writec (*l++);
159 }
160
161 proll () {
162 if (d0)
163 swap;
164 if (cturn == 1)
165 writel ("Red's roll: ");
166 else
167 writel ("White's roll: ");
168 writec (D0+'0');
169 writec ('\040');
170 writec (D1+'0');
171 if (tflag)
172 cline();
173 }
174
175 wrint (n)
176 int n;
177 {
178 register int i, j, t;
179
180 for (i = 4; i > 0; i--) {
181 t = 1;
182 for (j = 0; j<i; j++)
183 t *= 10;
184 if (n > t-1)
185 writec ((n/t)%10+'0');
186 }
187 writec (n%10+'0');
188 }
189
190 gwrite() {
191 register int r, c;
192
193 if (tflag) {
194 r = curr;
195 c = curc;
196 curmove (16,0);
197 }
198
199 if (gvalue > 1) {
200 writel ("Game value: ");
201 wrint (gvalue);
202 writel (". ");
203 if (dlast == -1)
204 writel (color[0]);
205 else
206 writel (color[1]);
207 writel (" doubled last.");
208 } else {
209 switch (pnum) {
210 case -1: /* player is red */
211 writel (plred);
212 break;
213 case 0: /* player is both colors */
214 writel (nocomp);
215 break;
216 case 1: /* player is white */
217 writel (plwhite);
218 }
219 }
220
221 if (rscore || wscore) {
222 writel (" ");
223 wrscore();
224 }
225
226 if (tflag) {
227 cline();
228 curmove (r,c);
229 }
230 }
231
232 quit () {
233 register int i;
234
235 if (tflag) {
236 curmove (20,0);
237 clend();
238 } else
239 writec ('\n');
240 writel ("Are you sure you want to quit?");
241 if (yorn (0)) {
242 if (rfl) {
243 writel ("Would you like to save this game?");
244 if (yorn(0))
245 save(0);
246 }
247 cturn = 0;
248 return (1);
249 }
250 return (0);
251 }
252
253 yorn (special)
254 register char special; /* special response */
255 {
256 register char c;
257 register int i;
258
259 i = 1;
260 while ( (c = readc()) != 'Y' && c != 'N') {
261 if (special && c == special)
262 return (2);
263 if (i) {
264 if (special) {
265 writel (" (Y, N, or ");
266 writec (special);
267 writec (')');
268 } else
269 writel (" (Y or N)");
270 i = 0;
271 } else
272 writec ('\007');
273 }
274 if (c == 'Y')
275 writel (" Yes.\n");
276 else
277 writel (" No.\n");
278 if (tflag)
279 buflush();
280 return (c == 'Y');
281 }
282
283 wrhit (i)
284 register int i;
285 {
286 writel ("Blot hit on ");
287 wrint (i);
288 writec ('.');
289 writec ('\n');
290 }
291
292 nexturn () {
293 register int c;
294
295 cturn = -cturn;
296 c = cturn/abs(cturn);
297 home = bar;
298 bar = 25-bar;
299 offptr += c;
300 offopp -= c;
301 inptr += c;
302 inopp -= c;
303 Colorptr += c;
304 colorptr += c;
305 }
306
307 getarg (arg)
308 register char ***arg;
309
310 {
311 register char **s;
312
313 /* process arguments here. dashes are ignored, nbrw are ignored
314 if the game is being recovered */
315
316 s = *arg;
317 while (s[0][0] == '-') {
318 switch (s[0][1]) {
319
320 /* don't ask if rules or instructions needed */
321 case 'n':
322 if (rflag)
323 break;
324 aflag = 0;
325 args[acnt++] = 'n';
326 break;
327
328 /* player is both read and white */
329 case 'b':
330 if (rflag)
331 break;
332 pnum = 0;
333 aflag = 0;
334 args[acnt++] = 'b';
335 break;
336
337 /* player is red */
338 case 'r':
339 if (rflag)
340 break;
341 pnum = -1;
342 aflag = 0;
343 args[acnt++] = 'r';
344 break;
345
346 /* player is white */
347 case 'w':
348 if (rflag)
349 break;
350 pnum = 1;
351 aflag = 0;
352 args[acnt++] = 'w';
353 break;
354
355 /* print board after move according to following character */
356 case 'p':
357 if (s[0][2] != 'r' && s[0][2] != 'w' && s[0][2] != 'b')
358 break;
359 args[acnt++] = 'p';
360 args[acnt++] = s[0][2];
361 if (s[0][2] == 'r')
362 bflag = 1;
363 if (s[0][2] == 'w')
364 bflag = -1;
365 if (s[0][2] == 'b')
366 bflag = 0;
367 break;
368
369 case 't':
370 if (s[0][2] == '\0') { /* get terminal caps */
371 s++;
372 tflag = getcaps (*s);
373 } else
374 tflag = getcaps (&s[0][2]);
375 break;
376
377 case 's':
378 s++;
379 /* recover file */
380 recover (s[0]);
381 break;
382 }
383 s++;
384 }
385 if (s[0] != 0)
386 recover(s[0]);
387 }
388
389 init () {
390 register int i;
391 for (i = 0; i < 26;)
392 board[i++] = 0;
393 board[1] = 2;
394 board[6] = board[13] = -5;
395 board[8] = -3;
396 board[12] = board[19] = 5;
397 board[17] = 3;
398 board[24] = -2;
399 off[0] = off[1] = -15;
400 in[0] = in[1] = 5;
401 gvalue = 1;
402 dlast = 0;
403 }
404
405 wrscore () {
406 writel ("Score: ");
407 writel (color[1]);
408 writec (' ');
409 wrint (rscore);
410 writel (", ");
411 writel (color[0]);
412 writec (' ');
413 wrint (wscore);
414 }
415
416 fixtty (mode)
417 int mode;
418 {
419 if (tflag)
420 newpos();
421 buflush();
422 tty.sg_flags = mode;
423 if (stty (0,&tty) < 0)
424 errexit("fixtty");
425 }
426
427 getout () {
428 /* go to bottom of screen */
429 if (tflag) {
430 curmove (23,0);
431 cline();
432 } else
433 writec ('\n');
434
435 /* fix terminal status */
436 fixtty (old);
437 exit();
438 }
439 roll () {
440 register char c;
441 register int row;
442 register int col;
443
444 if (iroll) {
445 if (tflag) {
446 row = curr;
447 col = curc;
448 curmove (17,0);
449 } else
450 writec ('\n');
451 writel ("ROLL: ");
452 c = readc();
453 if (c != '\n') {
454 while (c < '1' || c > '6')
455 c = readc();
456 D0 = c-'0';
457 writec (' ');
458 writec (c);
459 c = readc();
460 while (c < '1' || c > '6')
461 c = readc();
462 D1 = c-'0';
463 writec (' ');
464 writec (c);
465 if (tflag) {
466 curmove (17,0);
467 cline();
468 curmove (row,col);
469 } else
470 writec ('\n');
471 return;
472 }
473 if (tflag) {
474 curmove (17,0);
475 cline();
476 curmove (row,col);
477 } else
478 writec ('\n');
479 }
480 D0 = rnum(6)+1;
481 D1 = rnum(6)+1;
482 d0 = 0;
483 }
484