I got a little bit of help from a guy on another forum and have re-written my program, but am still having some difficulty. I've
re-written most of the program as the following code shows (read after for problems) :
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h> /* contains prototype for function time */
#define beginbalance 1000
/* enumeration constants represent game status */
enum Status { CONTINUE, WON, LOST };
int rollDice(void); /* rollDice function prototype */
int playGame(void); /* playGame function prototype */
int getWager(void);
int adjustBalance(int balance, int wager);
void getYesOrNo(int balance);
int main()
{
playGame();
return 0;
}
/* PRE: none
POST: returns to the caller one of the enumatered constants WON or
LOST */
int playGame (void)
{
int sum; /* sum of rolled dice */
int myPoint; /* point earned */
int result = 0; /* integer defining whether or not last game was WON
or LOST */
int balance = beginbalance;
printf("Balance = $%d.00\n",balance);
int wager = 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" );
result = 1;
} /* end if */
else { /* player lost */
printf( "Player loses\n" );
result = -1;
} /* end else */
adjustBalance(balance, wager);
getYesOrNo(balance);
return result;
}
/* 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(void)
{
int amount = 0;
printf("Enter wager: ");
scanf("%d", &amount);
return amount;
}
/* 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 */
int adjustBalance(int balance, int wager)
{
int newbalance = balance;
if (wager)
{
if (wager <= balance)
{
newbalance = balance + wager * playGame();
}
else
{
printf("Your wager must not exceed your current balance.\n");
getWager();
}
}
printf("Balance = $%d.00\n",newbalance);
return newbalance;
}
/* 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(int balance)
{
char ch;
printf("Do you want to play another game? (y or n): ");
ch = getchar();
while (getchar() != '\n');
if (getchar() == 'y')
{
playGame();
}
else if (getchar() == 'n')
{
printf("\n\nYour final balance is $%d.00",balance);
}
else
{
printf("You must answer y or n.");
getYesOrNo(balance);
}
}
This code, if you compile it, introduces some problems.
1. getYesOrNo doesn't function properly either reading the user input,
or showing up after each game at the appropriate time. A little
difficult understanding my wording, more obvious if compiled.
2. If balance < wager, program does not output current balance on next
game.
3. Balance, despite my best efforts with your code, doesn't seem to
ever change, staying at $1000. I'm sure this is due to some poor
writing on my part trying to do things your way.