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