Craps Program with Local Variables
I'm having extremely difficult problems with getting used to not using global variables. I'm trying to write this craps game so that it only uses local variables, but I am having difficult getting all my seperate functions to use variables they all depend on.
My sample code is below, but let me explain my problem a little more in detail. If you see, I have the functions getWager() and adjustBalance(). Both these functions need to be able to read both the variables balance and wager, and modify them accordingly. Not only that, but adjustBalance() is also dependant on gameStatus, a part of the playGame() function.
How can I use only local variables to get my program to be able to read and modify things like balance and wager while depending on things like gameStatus with just the expressions and statements I have here. I've tried looking elsewhere, but I've been unable to get an explanation that helps me out very much. My book for my class is also a total piece of garbage when it comes to something like this. Anyone help me out. I'd really appreciate it. Thanks.
Code:
#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 */
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;
printf("Balance = $%d.00\n",balance);
printf("Enter wager: ");
scanf("%d", &wager);
while (wager > balance || 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()
{
int balance; /* user's current balance */
balance = 1000;
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;
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();
}
}