121217Sdist /*
2*60746Sbostic * Copyright (c) 1980, 1993
3*60746Sbostic * The Regents of the University of California. All rights reserved.
433487Sbostic *
542565Sbostic * %sccs.include.redist.c%
621217Sdist */
76766Srrh
821217Sdist #ifndef lint
9*60746Sbostic static char sccsid[] = "@(#)subs.c 8.1 (Berkeley) 05/31/93";
1033487Sbostic #endif /* not lint */
1121217Sdist
126766Srrh #include <stdio.h>
136766Srrh #include "back.h"
146766Srrh
156766Srrh int buffnum;
166766Srrh char outbuff[BUFSIZ];
176766Srrh
186766Srrh static char plred[] = "Player is red, computer is white.";
196766Srrh static char plwhite[] = "Player is white, computer is red.";
206766Srrh static char nocomp[] = "(No computer play.)";
216766Srrh
226766Srrh char *descr[] = {
236766Srrh "Usage: backgammon [-] [n r w b pr pw pb t3a]\n",
246766Srrh "\t-\tgets this list\n\tn\tdon't ask for rules or instructions",
256766Srrh "\tr\tplayer is red (implies n)\n\tw\tplayer is white (implies n)",
266766Srrh "\tb\ttwo players, red and white (implies n)",
276766Srrh "\tpr\tprint the board before red's turn",
286766Srrh "\tpw\tprint the board before white's turn",
296766Srrh "\tpb\tprint the board before both player's turn",
306766Srrh "\tterm\tterminal is a term",
316766Srrh "\tsfile\trecover saved game from file",
326766Srrh 0
336766Srrh };
346766Srrh
errexit(s)356766Srrh errexit (s)
366766Srrh register char *s;
376766Srrh {
386766Srrh write (2,"\n",1);
396766Srrh perror (s);
406766Srrh getout();
416766Srrh }
426766Srrh
strset(s1,s2)436766Srrh strset (s1,s2)
446766Srrh register char *s1, *s2;
456766Srrh {
466766Srrh while ( (*s1++ = *s2++) != '\0');
476766Srrh }
486766Srrh
addbuf(c)496766Srrh addbuf (c)
506766Srrh register char c;
516766Srrh
526766Srrh {
536766Srrh buffnum++;
546766Srrh if (buffnum == BUFSIZ) {
556766Srrh if (write(1,outbuff,BUFSIZ) != BUFSIZ)
566766Srrh errexit ("addbuf (write):");
576766Srrh buffnum = 0;
586766Srrh }
596766Srrh outbuff[buffnum] = c;
606766Srrh }
616766Srrh
buflush()626766Srrh buflush () {
636766Srrh if (buffnum < 0)
646766Srrh return;
656766Srrh buffnum++;
666766Srrh if (write (1,outbuff,buffnum) != buffnum)
676766Srrh errexit ("buflush (write):");
686766Srrh buffnum = -1;
696766Srrh }
706766Srrh
readc()716766Srrh readc () {
726766Srrh char c;
736766Srrh
746766Srrh if (tflag) {
756766Srrh cline();
766766Srrh newpos();
776766Srrh }
786766Srrh buflush();
796766Srrh if (read(0,&c,1) != 1)
806766Srrh errexit ("readc");
8130876Sbostic #ifdef WHY_IS_THIS_HARDWIRED_IN_HERE
826766Srrh if (c == '\177')
836766Srrh getout();
8430876Sbostic #endif
856766Srrh if (c == '\033' || c == '\015')
866766Srrh return ('\n');
876766Srrh if (cflag)
886766Srrh return (c);
896766Srrh if (c == '\014')
906766Srrh return ('R');
916766Srrh if (c >= 'a' && c <= 'z')
926766Srrh return (c & 0137);
936766Srrh return (c);
946766Srrh }
956766Srrh
writec(c)966766Srrh writec (c)
976766Srrh char c;
986766Srrh {
996766Srrh if (tflag)
1006766Srrh fancyc (c);
1016766Srrh else
1026766Srrh addbuf (c);
1036766Srrh }
1046766Srrh
writel(l)1056766Srrh writel (l)
1066766Srrh register char *l;
1076766Srrh {
1086766Srrh #ifdef DEBUG
1096766Srrh register char *s;
1106766Srrh
1116766Srrh if (trace == NULL)
1126766Srrh trace = fopen ("bgtrace","w");
1136766Srrh
1146766Srrh fprintf (trace,"writel: \"");
1156766Srrh for (s = l; *s; s++) {
1166766Srrh if (*s < ' ' || *s == '\177')
1176766Srrh fprintf (trace,"^%c",(*s)^0100);
1186766Srrh else
1196766Srrh putc (*s,trace);
1206766Srrh }
1216766Srrh fprintf (trace,"\"\n");
1226766Srrh fflush (trace);
1236766Srrh #endif
1246766Srrh
1256766Srrh while (*l)
1266766Srrh writec (*l++);
1276766Srrh }
1286766Srrh
proll()1296766Srrh proll () {
1306766Srrh if (d0)
1316766Srrh swap;
1326766Srrh if (cturn == 1)
1336766Srrh writel ("Red's roll: ");
1346766Srrh else
1356766Srrh writel ("White's roll: ");
1366766Srrh writec (D0+'0');
1376766Srrh writec ('\040');
1386766Srrh writec (D1+'0');
1396766Srrh if (tflag)
1406766Srrh cline();
1416766Srrh }
1426766Srrh
wrint(n)1436766Srrh wrint (n)
1446766Srrh int n;
1456766Srrh {
1466766Srrh register int i, j, t;
1476766Srrh
1486766Srrh for (i = 4; i > 0; i--) {
1496766Srrh t = 1;
1506766Srrh for (j = 0; j<i; j++)
1516766Srrh t *= 10;
1526766Srrh if (n > t-1)
1536766Srrh writec ((n/t)%10+'0');
1546766Srrh }
1556766Srrh writec (n%10+'0');
1566766Srrh }
1576766Srrh
gwrite()1586766Srrh gwrite() {
1596766Srrh register int r, c;
1606766Srrh
1616766Srrh if (tflag) {
1626766Srrh r = curr;
1636766Srrh c = curc;
1646766Srrh curmove (16,0);
1656766Srrh }
1666766Srrh
1676766Srrh if (gvalue > 1) {
1686766Srrh writel ("Game value: ");
1696766Srrh wrint (gvalue);
1706766Srrh writel (". ");
1716766Srrh if (dlast == -1)
1726766Srrh writel (color[0]);
1736766Srrh else
1746766Srrh writel (color[1]);
1756766Srrh writel (" doubled last.");
1766766Srrh } else {
1776766Srrh switch (pnum) {
1786766Srrh case -1: /* player is red */
1796766Srrh writel (plred);
1806766Srrh break;
1816766Srrh case 0: /* player is both colors */
1826766Srrh writel (nocomp);
1836766Srrh break;
1846766Srrh case 1: /* player is white */
1856766Srrh writel (plwhite);
1866766Srrh }
1876766Srrh }
1886766Srrh
1896766Srrh if (rscore || wscore) {
1906766Srrh writel (" ");
1916766Srrh wrscore();
1926766Srrh }
1936766Srrh
1946766Srrh if (tflag) {
1956766Srrh cline();
1966766Srrh curmove (r,c);
1976766Srrh }
1986766Srrh }
1996766Srrh
quit()2006766Srrh quit () {
2016766Srrh register int i;
2026766Srrh
2036766Srrh if (tflag) {
2046766Srrh curmove (20,0);
2056766Srrh clend();
2066766Srrh } else
2076766Srrh writec ('\n');
2086766Srrh writel ("Are you sure you want to quit?");
2096766Srrh if (yorn (0)) {
2106766Srrh if (rfl) {
2116766Srrh writel ("Would you like to save this game?");
2126766Srrh if (yorn(0))
2136766Srrh save(0);
2146766Srrh }
2156766Srrh cturn = 0;
2166766Srrh return (1);
2176766Srrh }
2186766Srrh return (0);
2196766Srrh }
2206766Srrh
yorn(special)2216766Srrh yorn (special)
2226766Srrh register char special; /* special response */
2236766Srrh {
2246766Srrh register char c;
2256766Srrh register int i;
2266766Srrh
2276766Srrh i = 1;
2286766Srrh while ( (c = readc()) != 'Y' && c != 'N') {
2296766Srrh if (special && c == special)
2306766Srrh return (2);
2316766Srrh if (i) {
2326766Srrh if (special) {
2336766Srrh writel (" (Y, N, or ");
2346766Srrh writec (special);
2356766Srrh writec (')');
2366766Srrh } else
2376766Srrh writel (" (Y or N)");
2386766Srrh i = 0;
2396766Srrh } else
2406766Srrh writec ('\007');
2416766Srrh }
2426766Srrh if (c == 'Y')
2436766Srrh writel (" Yes.\n");
2446766Srrh else
2456766Srrh writel (" No.\n");
2466766Srrh if (tflag)
2476766Srrh buflush();
2486766Srrh return (c == 'Y');
2496766Srrh }
2506766Srrh
wrhit(i)2516766Srrh wrhit (i)
2526766Srrh register int i;
2536766Srrh {
2546766Srrh writel ("Blot hit on ");
2556766Srrh wrint (i);
2566766Srrh writec ('.');
2576766Srrh writec ('\n');
2586766Srrh }
2596766Srrh
nexturn()2606766Srrh nexturn () {
2616766Srrh register int c;
2626766Srrh
2636766Srrh cturn = -cturn;
2646766Srrh c = cturn/abs(cturn);
2656766Srrh home = bar;
2666766Srrh bar = 25-bar;
2676766Srrh offptr += c;
2686766Srrh offopp -= c;
2696766Srrh inptr += c;
2706766Srrh inopp -= c;
2716766Srrh Colorptr += c;
2726766Srrh colorptr += c;
2736766Srrh }
2746766Srrh
getarg(arg)2756766Srrh getarg (arg)
2766766Srrh register char ***arg;
2776766Srrh
2786766Srrh {
2799360Smckusick register char **s;
2806766Srrh
2816766Srrh /* process arguments here. dashes are ignored, nbrw are ignored
2826766Srrh if the game is being recovered */
2836766Srrh
2849360Smckusick s = *arg;
2859360Smckusick while (s[0][0] == '-') {
2869360Smckusick switch (s[0][1]) {
2876766Srrh
2886766Srrh /* don't ask if rules or instructions needed */
2896766Srrh case 'n':
2906766Srrh if (rflag)
2916766Srrh break;
2926766Srrh aflag = 0;
2939360Smckusick args[acnt++] = 'n';
2946766Srrh break;
2956766Srrh
2966766Srrh /* player is both read and white */
2976766Srrh case 'b':
2986766Srrh if (rflag)
2996766Srrh break;
3006766Srrh pnum = 0;
3016766Srrh aflag = 0;
3029360Smckusick args[acnt++] = 'b';
3036766Srrh break;
3046766Srrh
3056766Srrh /* player is red */
3066766Srrh case 'r':
3076766Srrh if (rflag)
3086766Srrh break;
3096766Srrh pnum = -1;
3106766Srrh aflag = 0;
3119360Smckusick args[acnt++] = 'r';
3126766Srrh break;
3136766Srrh
3146766Srrh /* player is white */
3156766Srrh case 'w':
3166766Srrh if (rflag)
3176766Srrh break;
3186766Srrh pnum = 1;
3196766Srrh aflag = 0;
3209360Smckusick args[acnt++] = 'w';
3216766Srrh break;
3226766Srrh
3236766Srrh /* print board after move according to following character */
3246766Srrh case 'p':
3259360Smckusick if (s[0][2] != 'r' && s[0][2] != 'w' && s[0][2] != 'b')
3266766Srrh break;
3276766Srrh args[acnt++] = 'p';
3289360Smckusick args[acnt++] = s[0][2];
3299360Smckusick if (s[0][2] == 'r')
3306766Srrh bflag = 1;
3319360Smckusick if (s[0][2] == 'w')
3326766Srrh bflag = -1;
3339360Smckusick if (s[0][2] == 'b')
3346766Srrh bflag = 0;
3356766Srrh break;
3366766Srrh
3376766Srrh case 't':
3389360Smckusick if (s[0][2] == '\0') { /* get terminal caps */
3399360Smckusick s++;
3409360Smckusick tflag = getcaps (*s);
3419360Smckusick } else
3429360Smckusick tflag = getcaps (&s[0][2]);
3439360Smckusick break;
3446766Srrh
3456766Srrh case 's':
3469360Smckusick s++;
3479360Smckusick /* recover file */
3489360Smckusick recover (s[0]);
3499360Smckusick break;
3506766Srrh }
3519360Smckusick s++;
3526766Srrh }
3539360Smckusick if (s[0] != 0)
3549360Smckusick recover(s[0]);
3556766Srrh }
3566766Srrh
init()3576766Srrh init () {
3586766Srrh register int i;
3596766Srrh for (i = 0; i < 26;)
3606766Srrh board[i++] = 0;
3616766Srrh board[1] = 2;
3626766Srrh board[6] = board[13] = -5;
3636766Srrh board[8] = -3;
3646766Srrh board[12] = board[19] = 5;
3656766Srrh board[17] = 3;
3666766Srrh board[24] = -2;
3676766Srrh off[0] = off[1] = -15;
3686766Srrh in[0] = in[1] = 5;
3696766Srrh gvalue = 1;
3706766Srrh dlast = 0;
3716766Srrh }
3726766Srrh
wrscore()3736766Srrh wrscore () {
3746766Srrh writel ("Score: ");
3756766Srrh writel (color[1]);
3766766Srrh writec (' ');
3776766Srrh wrint (rscore);
3786766Srrh writel (", ");
3796766Srrh writel (color[0]);
3806766Srrh writec (' ');
3816766Srrh wrint (wscore);
3826766Srrh }
3836766Srrh
fixtty(mode)3846766Srrh fixtty (mode)
3856766Srrh int mode;
3866766Srrh {
3876766Srrh if (tflag)
3886766Srrh newpos();
3896766Srrh buflush();
3906766Srrh tty.sg_flags = mode;
3916766Srrh if (stty (0,&tty) < 0)
3926766Srrh errexit("fixtty");
3936766Srrh }
3946766Srrh
getout()3956766Srrh getout () {
3966766Srrh /* go to bottom of screen */
3976766Srrh if (tflag) {
3986766Srrh curmove (23,0);
3996766Srrh cline();
4006766Srrh } else
4016766Srrh writec ('\n');
4026766Srrh
4036766Srrh /* fix terminal status */
4046766Srrh fixtty (old);
4056766Srrh exit();
4066766Srrh }
roll()4076766Srrh roll () {
4086766Srrh register char c;
4096766Srrh register int row;
4106766Srrh register int col;
4116766Srrh
4126766Srrh if (iroll) {
4136766Srrh if (tflag) {
4146766Srrh row = curr;
4156766Srrh col = curc;
4166766Srrh curmove (17,0);
4176766Srrh } else
4186766Srrh writec ('\n');
4196766Srrh writel ("ROLL: ");
4206766Srrh c = readc();
4216766Srrh if (c != '\n') {
4226766Srrh while (c < '1' || c > '6')
4236766Srrh c = readc();
4246766Srrh D0 = c-'0';
4256766Srrh writec (' ');
4266766Srrh writec (c);
4276766Srrh c = readc();
4286766Srrh while (c < '1' || c > '6')
4296766Srrh c = readc();
4306766Srrh D1 = c-'0';
4316766Srrh writec (' ');
4326766Srrh writec (c);
4336766Srrh if (tflag) {
4346766Srrh curmove (17,0);
4356766Srrh cline();
4366766Srrh curmove (row,col);
4376766Srrh } else
4386766Srrh writec ('\n');
4396766Srrh return;
4406766Srrh }
4416766Srrh if (tflag) {
4426766Srrh curmove (17,0);
4436766Srrh cline();
4446766Srrh curmove (row,col);
4456766Srrh } else
4466766Srrh writec ('\n');
4476766Srrh }
4486766Srrh D0 = rnum(6)+1;
4496766Srrh D1 = rnum(6)+1;
4506766Srrh d0 = 0;
4516766Srrh }
452