Annotation of sbbs/dice/war.c, revision 1.1.1.1

1.1       root        1: #include <stdlib.h>
                      2: #include <stdio.h>
                      3: #include <conio.h>
                      4: #include <stdarg.h>
                      5: #include <dos.h>
                      6: #include <io.h>
                      7: #include <sys/stat.h>
                      8: #include <errno.h>
                      9: #include <fcntl.h>
                     10: 
                     11: #define LOOP_NOPEN 50
                     12: #define MAX_NODES 25
                     13: #define HIGH 8
                     14: 
                     15: int players, player, roller, bet_amt, lose, win, hungup,ansi=0;
                     16: unsigned char node[MAX_NODES]={NULL},node_num,sys_nodes;
                     17: char curatr;
                     18: 
                     19: /***********************/
                     20: /* Function prototypes */
                     21: /***********************/
                     22: int nopen(char *path,int access);
                     23: int roll(void);
                     24: void bprintf(char *fmt,...);
                     25: void color(char atr);
                     26: 
                     27: main(int argc, char *argv[])
                     28: {
                     29:        FILE *fp;
                     30:        char cont='Y', ch;
                     31: 
                     32:        delay(0);
                     33: 
                     34:        /* read xtrn.dat */
                     35:        
                     36:        bprintf("\n\n\1y\1hWelcome to Dice Wars!\1n");
                     37:        do {
                     38:                bprintf("\n\nI]nstructions\nJ]oin the current game\nL]ist current players");
                     39:                bprintf("\nQ]uit back to the board");
                     40:                bprintf("\n\nCommand: ");
                     41:                ch=toupper(getch());
                     42: 
                     43:                switch(ch) {
                     44:                        case 'Q':
                     45:                                break;
                     46:                        case 'I':
                     47:                                /* instructions(); */
                     48:                                break;
                     49:                        case 'L':
                     50:                                get_game_status();
                     51:                                bprintf("\n\n#players=%d roller=%d bet=%d",players,roller,bet_amt);
                     52:                                break;
                     53:                        case 'J':
                     54:                                join_game();
                     55:                                menu();
                     56:                                break;
                     57:                }
                     58:        } while ( ch!='Q' && ch!=3 && !hungup);
                     59: 
                     60:        bprintf("\n\nThanks for playing!  Try again soon!\r\n");
                     61:        get_game_status();
                     62:        node[node_num-1]=0;
                     63:        put_game_status();
                     64:        return;
                     65: }
                     66: 
                     67: /* This is the routine to roll the dice */
                     68: 
                     69: int roll()
                     70: {
                     71:        char ch;
                     72:        int die1, die2, dice, shake;
                     73:        die1=die2=dice=0;
                     74: 
                     75:        clrscr();
                     76:        while (!lose&&!win) { ch='';
                     77:                bprintf("\n\n[S]end message or [ENTER] to roll!"); ch=toupper(getche());
                     78:                bprintf("\n\n");
                     79:                for (shake=0; shake<50; shake++) {
                     80:                        die1=random(6)+1; die2=random(6)+1;
                     81:                }
                     82:                dice=die1+die2;
                     83:                bprintf("You rolled a %d and a %d for a total of %d",die1,die2,dice);
                     84:                return dice;
                     85:        }
                     86: }
                     87: 
                     88: /***********************************
                     89:  Gets the current status of the game
                     90: ************************************/
                     91: get_game_status()
                     92: {
                     93:        int file;
                     94: 
                     95:        if ((file=nopen("GAMESTAT.DAB",O_RDONLY))==-1)
                     96:                return -1;
                     97: 
                     98:        read(file,&players,sizeof(players));
                     99:        read(file,&roller,sizeof(roller));
                    100:        read(file,&bet_amt,sizeof(bet_amt));
                    101:        read(file,node,sys_nodes);
                    102:        close(file); return 0;
                    103: }
                    104: 
                    105: put_game_status()
                    106: {
                    107:        int file;
                    108: 
                    109:        if ((file=nopen("GAMESTAT.DAB",O_RDWR|O_CREAT))==-1)
                    110:                return -1;
                    111: 
                    112:        write(file,&players,sizeof(players));
                    113:        write(file,&roller,sizeof(roller));
                    114:        write(file,&bet_amt,sizeof(bet_amt));
                    115:        lseek(file,node_num-1,SEEK_CUR);
                    116:        write(file,&node[node_num-1],1);
                    117:        close(file);
                    118: }
                    119: 
                    120: increase_bet()
                    121: {
                    122:        int add;
                    123: 
                    124:        while (1) {
                    125:                bprintf("\n\nEnter amount to increase by (in Kbytes): ");
                    126:                scanf("%d",add);
                    127:                if (add<=100)
                    128:                        break;
                    129:                else bprintf("\n\nMust be less than 100K!!");
                    130:        }
                    131:        return;
                    132: }
                    133: 
                    134: /* Here we write the player information to the player file */
                    135: 
                    136: put_player_status()
                    137: {
                    138:        FILE *fp;
                    139:        char fname[81];
                    140:        int x;
                    141: 
                    142:        sprintf(fname,"PLAYER.%d",player);
                    143:        if ((fp=fopen(fname,"a+"))==NULL) return;
                    144:        fprintf(fname,"0\n1\nUser Name\n100000");
                    145:        fclose(fp);
                    146: 
                    147: }
                    148: 
                    149: /* This is where the game takes place */
                    150: 
                    151: menu()
                    152: {
                    153:        char ch,
                    154:                *rollercmd="\n\nCommand (I,P,R,S,Q,?): ",
                    155:                *cmd="\n\nCommand (S,Q,?): ";
                    156:        if(roller==player)
                    157:                bprintf(rollercmd);
                    158:        else
                    159:                bprintf(cmd);
                    160:        while(1) {
                    161:                if(kbhit()) {
                    162:                        ch=toupper(getch());
                    163:                        bprintf("%c\r\n",ch);
                    164:                        if(ch=='Q')
                    165:                                break;
                    166:                        switch(ch) {
                    167:                                case 'S':   /* send message */
                    168:                                        break;
                    169:                                case 'I':   /* increase bet */
                    170:                                        if(roller!=player) {
                    171:                                                bprintf("\r\nYou're not the roller!");
                    172:                                                break; }
                    173:                                        break;
                    174:                                case 'P':
                    175:                                        if(roller!=player) {
                    176:                                                bprintf("\r\nYou're not the roller!\n");
                    177:                                                break; }
                    178:                     break;
                    179:                                case 'R':   /* Roll dice */
                    180:                                        if(roller!=player) {
                    181:                                                bprintf("\r\nYou're not the roller!\n");
                    182:                                                break; }
                    183:                                        break;
                    184:                        } /* switch */
                    185:                        if(roller==player)
                    186:                                bprintf(rollercmd);
                    187:                        else
                    188:                                bprintf(cmd);
                    189:                } /* kbhit */
                    190:        } /* while(!done) */
                    191: 
                    192: 
                    193: }
                    194: 
                    195: /* Here is where we wait for an appropriate moment to join the game */
                    196: 
                    197: join_game()
                    198: {
                    199: 
                    200:        if (access("GAMESTAT.DAB",0)) {
                    201:                players=roller=1; bet_amt=0;
                    202:                put_game_status();
                    203:                return;
                    204:        }
                    205:        get_game_status();
                    206:        node[node_num-1]=players+1;
                    207:        put_game_status();
                    208: 
                    209:        bprintf("\n\nHang on!  Waiting to enter game!");
                    210:        while (bet_amt) {
                    211:                if (kbhit()==3) {
                    212:                        hungup=1; return;
                    213:                }
                    214:                get_game_status();
                    215:        }
                    216: 
                    217:        player=++players;
                    218:        put_player_status();
                    219:        put_game_status();
                    220: 
                    221:        return;
                    222: }
                    223: 
                    224: /****************************************************************************/
                    225: /* Network open function. Opens all files DENYALL and retries LOOP_NOPEN    */
                    226: /* number of times if the attempted file is already open or denying access  */
                    227: /* for some other reason.      All files are opened in BINARY mode.                    */
                    228: /****************************************************************************/
                    229: int nopen(char *str, int access)
                    230: {
                    231:        char count=0,logentry[256];
                    232:        int file,share;
                    233: 
                    234: if(access==O_RDONLY) share=O_DENYWRITE;
                    235:        else share=O_DENYALL;
                    236: while(((file=open(str,O_BINARY|share|access,S_IWRITE))==-1)
                    237:        && errno==EACCES && count++<LOOP_NOPEN)
                    238:        if(count>10)
                    239:                delay(50);
                    240: if(count)
                    241:        bprintf("\r\n!$!$!$ NOPEN COLLISION - File: %s Count: %d\r\n"
                    242:                ,str,count);
                    243: if(file==-1 && errno==EACCES)
                    244:        puts("\7\r\nNOPEN: ACCESS DENIED\r\n\7");
                    245: return(file);
                    246: }
                    247: 
                    248: /****************************************************************************/
                    249: /* Performs printf replacing ctrl-a codes with ansi seq if user has ansi       */
                    250: /****************************************************************************/
                    251: void bprintf(char *fmt, ...)
                    252: {
                    253:        char sbuf[1024];
                    254:        int i,len;
                    255: 
                    256: vsprintf(sbuf,fmt,_va_ptr);
                    257: len=strlen(sbuf);
                    258: for(i=0;i<len;i++) {
                    259:        if(sbuf[i]==1) { /* ctrl-a */
                    260:                if(ansi) {
                    261:                        switch(toupper(sbuf[i+1])) {
                    262:                                case 'R':
                    263:                                        color(RED);
                    264:                                        break;
                    265:                                case 'G':
                    266:                                        color(GREEN);
                    267:                                        break;
                    268:                                case 'B':
                    269:                                        color(BLUE);
                    270:                                        break;
                    271:                                case 'C':
                    272:                                        color(CYAN);
                    273:                                        break;
                    274:                                case 'M':
                    275:                                        color(MAGENTA);
                    276:                                        break;
                    277:                                case 'Y':
                    278:                                        color(YELLOW);
                    279:                                        break;
                    280:                                case 'H':
                    281:                                        color(curatr|HIGH);
                    282:                                        break;
                    283:                                case 'I':
                    284:                                        color(curatr|BLINK);
                    285:                                        break;
                    286:                                case 'N':
                    287:                                        color(LIGHTGRAY);
                    288:                                        break; } }
                    289:                                i++; } /* skip code */
                    290:         else putchar(sbuf[i]); }
                    291: }
                    292: 
                    293: /****************************************************************************/
                    294: /* Sends ansi codes to change remote ansi terminal's colors                 */
                    295: /* Only sends necessary codes - tracks remote terminal's current attributes */
                    296: /* through the 'curatr' variable                                                                                       */
                    297: /****************************************************************************/
                    298: void color(char atr)
                    299: {
                    300: 
                    301: if((!(atr&HIGH) && curatr&HIGH)        || (!(atr&BLINK) && curatr&BLINK)
                    302:        || atr==LIGHTGRAY) {
                    303:        bprintf("\x1b[0m");
                    304:        curatr=LIGHTGRAY; }
                    305: 
                    306: if(atr==LIGHTGRAY) {                                   /* no attributes */
                    307:        curatr=atr;
                    308:        return; }
                    309: 
                    310: if(atr&BLINK) {                                                /* special attributes */
                    311:        if(!(curatr&BLINK))
                    312:                bprintf("\x1b[5m"); }
                    313: if(atr&HIGH) {
                    314:        if(!(curatr&HIGH))
                    315:                bprintf("\x1b[1m"); }
                    316: 
                    317: if((atr&0x7)==BLACK) {                         /* foreground colors */
                    318:        if((curatr&0x7)!=BLACK)
                    319:                bprintf("\x1b[30m"); }
                    320: else if((atr&0x7)==RED) {
                    321:        if((curatr&0x7)!=RED)
                    322:                bprintf("\x1b[31m"); }
                    323: else if((atr&0x7)==GREEN) {
                    324:        if((curatr&0x7)!=GREEN)
                    325:                bprintf("\x1b[32m"); }
                    326: else if((atr&0x7)==BROWN) {
                    327:        if((curatr&0x7)!=BROWN)
                    328:                bprintf("\x1b[33m"); }
                    329: else if((atr&0x7)==BLUE) {
                    330:        if((curatr&0x7)!=BLUE)
                    331:                bprintf("\x1b[34m"); }
                    332: else if((atr&0x7)==MAGENTA) {
                    333:        if((curatr&0x7)!=MAGENTA)
                    334:                bprintf("\x1b[35m"); }
                    335: else if((atr&0x7)==CYAN) {
                    336:        if((curatr&0x7)!=CYAN)
                    337:                bprintf("\x1b[36m"); }
                    338: else if((atr&0x7)==LIGHTGRAY) {
                    339:        if((curatr&0x7)!=LIGHTGRAY)
                    340:                bprintf("\x1b[37m"); }
                    341: 
                    342: if((atr&0x70)==(BLACK<<4)) {           /* background colors */
                    343:        if((curatr&0x70)!=(BLACK<<4))
                    344:                bprintf("\x1b[40m"); }
                    345: else if((atr&0x70)==(RED<<4)) {
                    346:        if((curatr&0x70)!=(RED<<4))
                    347:                bprintf("\x1b[41m"); }
                    348: else if((atr&0x70)==(GREEN<<4)) {
                    349:        if((curatr&0x70)!=(GREEN<<4))
                    350:                bprintf("\x1b[42m"); }
                    351: else if((atr&0x70)==(BROWN<<4)) {
                    352:        if((curatr&0x70)!=(BROWN<<4))
                    353:                bprintf("\x1b[43m"); }
                    354: else if((atr&0x70)==(BLUE<<4)) {
                    355:        if((curatr&0x70)!=(BLUE<<4))
                    356:                bprintf("\x1b[44m"); }
                    357: else if((atr&0x70)==(MAGENTA<<4)) {
                    358:        if((curatr&0x70)!=(MAGENTA<<4))
                    359:                bprintf("\x1b[45m"); }
                    360: else if((atr&0x70)==(CYAN<<4)) {
                    361:        if((curatr&0x70)!=(CYAN<<4))
                    362:                bprintf("\x1b[46m"); }
                    363: else if((atr&0x70)==(LIGHTGRAY<<4)) {
                    364:        if((curatr&0x70)!=(LIGHTGRAY<<4))
                    365:                bprintf("\x1b[47m"); }
                    366: 
                    367: curatr=atr;
                    368: }
                    369: 

unix.superglobalmegacorp.com

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