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