File:  [Synchronet] / sbbs / dice / craps.c
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs
Tue Apr 24 16:37:52 2018 UTC (8 years, 2 months ago) by root
Branches: digitaldynamics, MAIN
CVS tags: v1_06a, HEAD
1.06a MS-DOS

#include <stdlib.h>
#include <stdio.h>
#include <dos.h>
#include <io.h>
#include <fcntl.h>

int players, player, roller, bet_amt, lose, win, ok;

main(int argc, char *argv[])
{
	FILE *fp;
	char cont='Y', ch;


	get_game_status(ok);
	if (ok) { players++; player=players; }
	if (players>1) request_access();
	while (!kbhit()) { }
	randomize();
	roll();


	while(cont=='Y') {
		printf("\n\nWould you like to play again?");cont=toupper(getche());
		if (cont=='Y')
			roll();
		else
			printf("\n\nThank you for playing, try again soon!");
	}
	return;
}

/* This is the routine to roll the dice */

roll()
{
	FILE *fp;
	char ch;
	int die1, die2, dice, roll, match, shake, x;
	die1=die2=dice=roll=match=lose=win=0;

	clrscr();
	while (!lose&&!win) { ch='';
		printf("\n\n[S]end message or [ENTER] to roll!"); ch=toupper(getche());
		printf("\n\n");
		for (shake=0; shake<50; shake++) {
			die1=random(6)+1; die2=random(6)+1;
		}
		dice=die1+die2; roll++;
		printf("You rolled a %d and a %d for a total of %d",die1,die2,dice);

		if (match==dice) {
			printf("\n\nBitchin!  You matched your first roll!  You WIN!"); getche();
			win=1; return;
		}
		if ((dice==7||dice==11) && roll==1) {
			printf("\n\nHurray!  You WIN!"); win=1; getche(); return;
		}
		if (((dice==2||dice==12) && roll==1) || ((dice==7||dice==11) && roll>1)) {
			printf("\n\nBummer dude, You LOST!"); lose=1; getche(); return;
		} else if (roll==1) {
			match=dice;
			printf("\n\nYou've gotta try to match a %d",match);
		}
			printf("\n\nWould you like to increase the bet? ");
			while (ch!='Y' && ch!='N') {
				ch=toupper(getche()); if (ch=='Y') increase_bet();
			}  ch='';
	}
}

/***********************************
 Gets the current status of the game
************************************/
int get_game_status()
{
	FILE *fp;

	if(!access("GAMESTAT.TXT",0)) {
		printf("file exists\7");
		if ((fp=fopen("GAMESTAT.TXT","r"))==NULL)
			return -1;
		fscanf(fp,"%d%d%d", &players, &roller, &bet_amt);
		fclose(fp); return 0;
	} else {
		if ((fp=fopen("GAMESTAT.TXT","a+"))==NULL)
			return -1;
		players=player=roller=1;bet_amt=0;
		fprintf(fp,"%d\n%d\n%d", players, roller, bet_amt);
		fclose(fp); return 1;
	}
}

increase_bet()
{
	int add;

	ok=0;

	while (!ok) {
		printf("\n\nEnter amount to increase by (in Kbytes): ");
		scanf("%d",add);
		if (add<=100) ok=1;
		else printf("\n\nMust be less than 100K!!");
	}
	return;
}


/* This function waits till the current bet (if any) is over before
   giving the new player access to the game */

request_access()
{
	FILE *fp;
	char fname[81];
	int x;

	sprintf(fname,"REQUEST.%d",player);
	if ((fp=fopen(fname,"a+"))==NULL) return;
	fclose(fp);

	printf("\n\nHang on!  Waiting to enter game!");
	while (!access(fname,0)) { }

	sprintf(fname,"PLAYER.%d",player);
	if ((fp=fopen(fname,"a+"))==NULL) return;
	fclose(fp);

}



menu()
{
	char ch;

	if (roller==player)
		printf("\n\nCommand (I,P,R,S,Q,?): ");
	else
		printf("\n\nCommand (S,Q,?): ");
}

unix.superglobalmegacorp.com

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