Annotation of 43BSDReno/games/backgammon/common_source/subs.c, revision 1.1.1.1

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

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.