|
|
1.06a MS-DOS
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <stdarg.h>
#include <dos.h>
#include <io.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#define LOOP_NOPEN 50
#define MAX_NODES 25
#define HIGH 8
int players, player, roller, bet_amt, lose, win, hungup,ansi=0;
unsigned char node[MAX_NODES]={NULL},node_num,sys_nodes;
char curatr;
/***********************/
/* Function prototypes */
/***********************/
int nopen(char *path,int access);
int roll(void);
void bprintf(char *fmt,...);
void color(char atr);
main(int argc, char *argv[])
{
FILE *fp;
char cont='Y', ch;
delay(0);
/* read xtrn.dat */
bprintf("\n\n\1y\1hWelcome to Dice Wars!\1n");
do {
bprintf("\n\nI]nstructions\nJ]oin the current game\nL]ist current players");
bprintf("\nQ]uit back to the board");
bprintf("\n\nCommand: ");
ch=toupper(getch());
switch(ch) {
case 'Q':
break;
case 'I':
/* instructions(); */
break;
case 'L':
get_game_status();
bprintf("\n\n#players=%d roller=%d bet=%d",players,roller,bet_amt);
break;
case 'J':
join_game();
menu();
break;
}
} while ( ch!='Q' && ch!=3 && !hungup);
bprintf("\n\nThanks for playing! Try again soon!\r\n");
get_game_status();
node[node_num-1]=0;
put_game_status();
return;
}
/* This is the routine to roll the dice */
int roll()
{
char ch;
int die1, die2, dice, shake;
die1=die2=dice=0;
clrscr();
while (!lose&&!win) { ch='';
bprintf("\n\n[S]end message or [ENTER] to roll!"); ch=toupper(getche());
bprintf("\n\n");
for (shake=0; shake<50; shake++) {
die1=random(6)+1; die2=random(6)+1;
}
dice=die1+die2;
bprintf("You rolled a %d and a %d for a total of %d",die1,die2,dice);
return dice;
}
}
/***********************************
Gets the current status of the game
************************************/
get_game_status()
{
int file;
if ((file=nopen("GAMESTAT.DAB",O_RDONLY))==-1)
return -1;
read(file,&players,sizeof(players));
read(file,&roller,sizeof(roller));
read(file,&bet_amt,sizeof(bet_amt));
read(file,node,sys_nodes);
close(file); return 0;
}
put_game_status()
{
int file;
if ((file=nopen("GAMESTAT.DAB",O_RDWR|O_CREAT))==-1)
return -1;
write(file,&players,sizeof(players));
write(file,&roller,sizeof(roller));
write(file,&bet_amt,sizeof(bet_amt));
lseek(file,node_num-1,SEEK_CUR);
write(file,&node[node_num-1],1);
close(file);
}
increase_bet()
{
int add;
while (1) {
bprintf("\n\nEnter amount to increase by (in Kbytes): ");
scanf("%d",add);
if (add<=100)
break;
else bprintf("\n\nMust be less than 100K!!");
}
return;
}
/* Here we write the player information to the player file */
put_player_status()
{
FILE *fp;
char fname[81];
int x;
sprintf(fname,"PLAYER.%d",player);
if ((fp=fopen(fname,"a+"))==NULL) return;
fprintf(fname,"0\n1\nUser Name\n100000");
fclose(fp);
}
/* This is where the game takes place */
menu()
{
char ch,
*rollercmd="\n\nCommand (I,P,R,S,Q,?): ",
*cmd="\n\nCommand (S,Q,?): ";
if(roller==player)
bprintf(rollercmd);
else
bprintf(cmd);
while(1) {
if(kbhit()) {
ch=toupper(getch());
bprintf("%c\r\n",ch);
if(ch=='Q')
break;
switch(ch) {
case 'S': /* send message */
break;
case 'I': /* increase bet */
if(roller!=player) {
bprintf("\r\nYou're not the roller!");
break; }
break;
case 'P':
if(roller!=player) {
bprintf("\r\nYou're not the roller!\n");
break; }
break;
case 'R': /* Roll dice */
if(roller!=player) {
bprintf("\r\nYou're not the roller!\n");
break; }
break;
} /* switch */
if(roller==player)
bprintf(rollercmd);
else
bprintf(cmd);
} /* kbhit */
} /* while(!done) */
}
/* Here is where we wait for an appropriate moment to join the game */
join_game()
{
if (access("GAMESTAT.DAB",0)) {
players=roller=1; bet_amt=0;
put_game_status();
return;
}
get_game_status();
node[node_num-1]=players+1;
put_game_status();
bprintf("\n\nHang on! Waiting to enter game!");
while (bet_amt) {
if (kbhit()==3) {
hungup=1; return;
}
get_game_status();
}
player=++players;
put_player_status();
put_game_status();
return;
}
/****************************************************************************/
/* Network open function. Opens all files DENYALL and retries LOOP_NOPEN */
/* number of times if the attempted file is already open or denying access */
/* for some other reason. All files are opened in BINARY mode. */
/****************************************************************************/
int nopen(char *str, int access)
{
char count=0,logentry[256];
int file,share;
if(access==O_RDONLY) share=O_DENYWRITE;
else share=O_DENYALL;
while(((file=open(str,O_BINARY|share|access,S_IWRITE))==-1)
&& errno==EACCES && count++<LOOP_NOPEN)
if(count>10)
delay(50);
if(count)
bprintf("\r\n!$!$!$ NOPEN COLLISION - File: %s Count: %d\r\n"
,str,count);
if(file==-1 && errno==EACCES)
puts("\7\r\nNOPEN: ACCESS DENIED\r\n\7");
return(file);
}
/****************************************************************************/
/* Performs printf replacing ctrl-a codes with ansi seq if user has ansi */
/****************************************************************************/
void bprintf(char *fmt, ...)
{
char sbuf[1024];
int i,len;
vsprintf(sbuf,fmt,_va_ptr);
len=strlen(sbuf);
for(i=0;i<len;i++) {
if(sbuf[i]==1) { /* ctrl-a */
if(ansi) {
switch(toupper(sbuf[i+1])) {
case 'R':
color(RED);
break;
case 'G':
color(GREEN);
break;
case 'B':
color(BLUE);
break;
case 'C':
color(CYAN);
break;
case 'M':
color(MAGENTA);
break;
case 'Y':
color(YELLOW);
break;
case 'H':
color(curatr|HIGH);
break;
case 'I':
color(curatr|BLINK);
break;
case 'N':
color(LIGHTGRAY);
break; } }
i++; } /* skip code */
else putchar(sbuf[i]); }
}
/****************************************************************************/
/* Sends ansi codes to change remote ansi terminal's colors */
/* Only sends necessary codes - tracks remote terminal's current attributes */
/* through the 'curatr' variable */
/****************************************************************************/
void color(char atr)
{
if((!(atr&HIGH) && curatr&HIGH) || (!(atr&BLINK) && curatr&BLINK)
|| atr==LIGHTGRAY) {
bprintf("\x1b[0m");
curatr=LIGHTGRAY; }
if(atr==LIGHTGRAY) { /* no attributes */
curatr=atr;
return; }
if(atr&BLINK) { /* special attributes */
if(!(curatr&BLINK))
bprintf("\x1b[5m"); }
if(atr&HIGH) {
if(!(curatr&HIGH))
bprintf("\x1b[1m"); }
if((atr&0x7)==BLACK) { /* foreground colors */
if((curatr&0x7)!=BLACK)
bprintf("\x1b[30m"); }
else if((atr&0x7)==RED) {
if((curatr&0x7)!=RED)
bprintf("\x1b[31m"); }
else if((atr&0x7)==GREEN) {
if((curatr&0x7)!=GREEN)
bprintf("\x1b[32m"); }
else if((atr&0x7)==BROWN) {
if((curatr&0x7)!=BROWN)
bprintf("\x1b[33m"); }
else if((atr&0x7)==BLUE) {
if((curatr&0x7)!=BLUE)
bprintf("\x1b[34m"); }
else if((atr&0x7)==MAGENTA) {
if((curatr&0x7)!=MAGENTA)
bprintf("\x1b[35m"); }
else if((atr&0x7)==CYAN) {
if((curatr&0x7)!=CYAN)
bprintf("\x1b[36m"); }
else if((atr&0x7)==LIGHTGRAY) {
if((curatr&0x7)!=LIGHTGRAY)
bprintf("\x1b[37m"); }
if((atr&0x70)==(BLACK<<4)) { /* background colors */
if((curatr&0x70)!=(BLACK<<4))
bprintf("\x1b[40m"); }
else if((atr&0x70)==(RED<<4)) {
if((curatr&0x70)!=(RED<<4))
bprintf("\x1b[41m"); }
else if((atr&0x70)==(GREEN<<4)) {
if((curatr&0x70)!=(GREEN<<4))
bprintf("\x1b[42m"); }
else if((atr&0x70)==(BROWN<<4)) {
if((curatr&0x70)!=(BROWN<<4))
bprintf("\x1b[43m"); }
else if((atr&0x70)==(BLUE<<4)) {
if((curatr&0x70)!=(BLUE<<4))
bprintf("\x1b[44m"); }
else if((atr&0x70)==(MAGENTA<<4)) {
if((curatr&0x70)!=(MAGENTA<<4))
bprintf("\x1b[45m"); }
else if((atr&0x70)==(CYAN<<4)) {
if((curatr&0x70)!=(CYAN<<4))
bprintf("\x1b[46m"); }
else if((atr&0x70)==(LIGHTGRAY<<4)) {
if((curatr&0x70)!=(LIGHTGRAY<<4))
bprintf("\x1b[47m"); }
curatr=atr;
}
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.