T
tigrfire
I've been working on a program to try and play a game of Craps, based
on a version I found elsewhere - I didn't code the original, but I
added a few things such as a balance and wager system. I'm having
trouble doing it all without using global variables though, so I have
another post in this group about local variable usage and how to pass
it, but just when I thought I'd got it, my program has a segmentation
fault. I'm not exactly sure what a seg fault is, but I didn't get any
warnings about it when I compiled. I'm posting my program here to see
if anyone can tell me what I'm doing wrong. I'm almost positive it has
something to do with the way I pass variables between multiple
functions, most likely, between playGame() and getYesOrNo().
#include <stdio.h>
#include <stdlib.h>
#include <time.h> /* contains prototype for function time */
/* enumeration constants represent game status */
enum Status { CONTINUE, WON, LOST };
int rollDice( void ); /* function prototype */
int getWager(); /* getWager function prototype */
int playGame(); /* playGame function prototype */
void getYesOrNo(); /* getYesOrNo function prototype */
int main()
{
playGame();
return 0;
}
/* PRE: none
POST: returns to the caller one of the enumatered constants WON or
LOST */
int playGame ()
{
int sum; /* sum of rolled dice */
int myPoint; /* point earned */
int balance; /* user's current balance */
int theWager;
theWager = getWager();
balance = 1000;
printf("Balance = $%d.00\n",balance);
getWager();
enum Status gameStatus; /* can contain CONTINUE, WON, or LOST */
/* randomize random number generator using current time */
srand( time( NULL ) );
sum = rollDice(); /* first roll of the dice */
/* determine game status based on sum of dice */
switch( sum ) {
/* win on first roll */
case 7:
case 11:
gameStatus = WON;
break;
/* lose on first roll */
case 2:
case 3:
case 12:
gameStatus = LOST;
break;
/* remember point */
default:
gameStatus = CONTINUE;
myPoint = sum;
printf( "Point is %d\n", myPoint );
break; /* optional */
} /* end switch */
/* while game not complete */
while ( gameStatus == CONTINUE ) {
sum = rollDice(); /* roll dice again */
/* determine game status */
if ( sum == myPoint ) { /* win by making point */
gameStatus = WON; /* game over, player won */
} /* end if */
else {
if ( sum == 7 ) { /* lose by rolling 7 */
gameStatus = LOST; /* game over, player lost */
} /* end if */
} /* end else */
} /* end while */
/* display won or lost message */
if ( gameStatus == WON ) { /* did player win? */
printf( "Player wins\n" );
} /* end if */
else { /* player lost */
printf( "Player loses\n" );
} /* end else */
if (balance > 0)
{
getYesOrNo();
}
else
{
printf("Your final balance is $%d.00", balance);
}
return balance;
}
/* roll dice, calculate sum and display results */
int rollDice( void )
{
int die1; /* first die */
int die2; /* second die */
int workSum; /* sum of dice */
die1 = 1 + ( rand() % 6 ); /* pick random die1 value */
die2 = 1 + ( rand() % 6 ); /* pick random die2 value */
workSum = die1 + die2; /* sum die1 and die2 */
/* display results of this roll */
printf( "Player rolled %d + %d = %d\n", die1, die2, workSum );
return workSum; /* return sum of dice */
} /* end function rollRice */
/* PRE: player inputs a dollar amount to wager on next game
POST: function checks wager, if wager is invalid, prompt user to
enter new wager. when valid
wager is entered, returns to the calling function */
int getWager()
{
int wager;
int theBalance;
theBalance = playGame();
printf("Enter wager: ");
scanf("%d", &wager);
while (wager > theBalance || wager <= 0)
{
printf("Your wager must not exceed your current balance.\n");
printf("Enter a new wager: ");
scanf("%d", &wager);
}
return wager;
}
/* PRE: checks whether or not the last game was WON or LOST
POST: either adds or subtracts the wager from the player's current
balance */
/*
void adjustBalance()
{
if (gameStatus = WON)
{
balance = balance + wager;
}
else
{
balance = balance - wager;
}
}
*/
/* PRE: asks if the user desires to play another game of craps
POST: function checks the response to make sure it is either 'y' or
'n'.
the function will repeatedly ask until one of these conditions is
satisfied
and upon a valid answer, return to the calling function */
void getYesOrNo()
{
printf("Do you want to play another game? (y or n): ");
char ch;
int theBalance;
theBalance = playGame();
ch = getchar();
while (getchar() != '\n');
if (ch == 'y')
{
playGame();
}
else if
(ch == 'n')
{
printf("Your final balance is $%d.00", theBalance);
}
else
{
printf("You must answer y or n.\n\n");
getYesOrNo();
}
}
Note: I know the program's not perfect yet, it doesn't run correctly
and has a lot of things that still need fixing, so if you point them
out, I'm probably aware of them, just don't know how to fix them yet.
on a version I found elsewhere - I didn't code the original, but I
added a few things such as a balance and wager system. I'm having
trouble doing it all without using global variables though, so I have
another post in this group about local variable usage and how to pass
it, but just when I thought I'd got it, my program has a segmentation
fault. I'm not exactly sure what a seg fault is, but I didn't get any
warnings about it when I compiled. I'm posting my program here to see
if anyone can tell me what I'm doing wrong. I'm almost positive it has
something to do with the way I pass variables between multiple
functions, most likely, between playGame() and getYesOrNo().
#include <stdio.h>
#include <stdlib.h>
#include <time.h> /* contains prototype for function time */
/* enumeration constants represent game status */
enum Status { CONTINUE, WON, LOST };
int rollDice( void ); /* function prototype */
int getWager(); /* getWager function prototype */
int playGame(); /* playGame function prototype */
void getYesOrNo(); /* getYesOrNo function prototype */
int main()
{
playGame();
return 0;
}
/* PRE: none
POST: returns to the caller one of the enumatered constants WON or
LOST */
int playGame ()
{
int sum; /* sum of rolled dice */
int myPoint; /* point earned */
int balance; /* user's current balance */
int theWager;
theWager = getWager();
balance = 1000;
printf("Balance = $%d.00\n",balance);
getWager();
enum Status gameStatus; /* can contain CONTINUE, WON, or LOST */
/* randomize random number generator using current time */
srand( time( NULL ) );
sum = rollDice(); /* first roll of the dice */
/* determine game status based on sum of dice */
switch( sum ) {
/* win on first roll */
case 7:
case 11:
gameStatus = WON;
break;
/* lose on first roll */
case 2:
case 3:
case 12:
gameStatus = LOST;
break;
/* remember point */
default:
gameStatus = CONTINUE;
myPoint = sum;
printf( "Point is %d\n", myPoint );
break; /* optional */
} /* end switch */
/* while game not complete */
while ( gameStatus == CONTINUE ) {
sum = rollDice(); /* roll dice again */
/* determine game status */
if ( sum == myPoint ) { /* win by making point */
gameStatus = WON; /* game over, player won */
} /* end if */
else {
if ( sum == 7 ) { /* lose by rolling 7 */
gameStatus = LOST; /* game over, player lost */
} /* end if */
} /* end else */
} /* end while */
/* display won or lost message */
if ( gameStatus == WON ) { /* did player win? */
printf( "Player wins\n" );
} /* end if */
else { /* player lost */
printf( "Player loses\n" );
} /* end else */
if (balance > 0)
{
getYesOrNo();
}
else
{
printf("Your final balance is $%d.00", balance);
}
return balance;
}
/* roll dice, calculate sum and display results */
int rollDice( void )
{
int die1; /* first die */
int die2; /* second die */
int workSum; /* sum of dice */
die1 = 1 + ( rand() % 6 ); /* pick random die1 value */
die2 = 1 + ( rand() % 6 ); /* pick random die2 value */
workSum = die1 + die2; /* sum die1 and die2 */
/* display results of this roll */
printf( "Player rolled %d + %d = %d\n", die1, die2, workSum );
return workSum; /* return sum of dice */
} /* end function rollRice */
/* PRE: player inputs a dollar amount to wager on next game
POST: function checks wager, if wager is invalid, prompt user to
enter new wager. when valid
wager is entered, returns to the calling function */
int getWager()
{
int wager;
int theBalance;
theBalance = playGame();
printf("Enter wager: ");
scanf("%d", &wager);
while (wager > theBalance || wager <= 0)
{
printf("Your wager must not exceed your current balance.\n");
printf("Enter a new wager: ");
scanf("%d", &wager);
}
return wager;
}
/* PRE: checks whether or not the last game was WON or LOST
POST: either adds or subtracts the wager from the player's current
balance */
/*
void adjustBalance()
{
if (gameStatus = WON)
{
balance = balance + wager;
}
else
{
balance = balance - wager;
}
}
*/
/* PRE: asks if the user desires to play another game of craps
POST: function checks the response to make sure it is either 'y' or
'n'.
the function will repeatedly ask until one of these conditions is
satisfied
and upon a valid answer, return to the calling function */
void getYesOrNo()
{
printf("Do you want to play another game? (y or n): ");
char ch;
int theBalance;
theBalance = playGame();
ch = getchar();
while (getchar() != '\n');
if (ch == 'y')
{
playGame();
}
else if
(ch == 'n')
{
printf("Your final balance is $%d.00", theBalance);
}
else
{
printf("You must answer y or n.\n\n");
getYesOrNo();
}
}
Note: I know the program's not perfect yet, it doesn't run correctly
and has a lot of things that still need fixing, so if you point them
out, I'm probably aware of them, just don't know how to fix them yet.