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