Hello,
I have 3 major problems:
1) I am not sure if I am doing my static test method correctly.
2) I am not sure if the wagering class is actually checking the bankBalance and the wager
3) I have the following error, which I am not sure how to correct because I was
wondering what I should do if my test failed? Line 177 is the last line of code, its
complaining about my last function. The error reads:
(177) : warning C4715: 'Wagering::wageringTest' : not all control paths return a value
My code is the following:
// Craps game simulation with wagering.
#include "Dice.h"
Dice::Dice()
{
srand ( (unsigned)time ( 0 ) ); // initialize the random number generator
}
int rollDice( void ); // function prototype
Status craps( void ); // function prototype
void chatter( void ); // function prototype
// roll dice, calculate sum and display results
int Dice::rollDice( void )
{
die1 = 1 + rand() % 6; // pick random die1 value
die2 = 1 + rand() % 6; // pick random die2 value
return die1 + die2; // sum die1 and die2
}
void Dice::displayValue()
{
// display results of this roll
cout << "Player rolled " << die1 << " + " << die2
<< " = " << die1 + die2 << endl;
} // end function displayValue
// craps plays one game of craps, returns result of game
Status DiceGame::playCraps( void )
{
sum = die.rollDice(); // first roll of dice
die.displayValue();
// determine game status and point based on sum of dice
switch ( sum )
{
case 7: // win on first roll
case 11:
gameStatus = WON;
break;
case 2: // lose on first roll
case 3:
case 12:
gameStatus = LOST;
break;
default: // remember point
gameStatus = CONTINUE;
myPoint = sum;
cout << "Point is " << myPoint << '\n';
break;
} // end switch
// while game not complete ...
while ( gameStatus == CONTINUE )
{
chatter(); // create "chatter"
sum = die.rollDice(); // roll dice again
die.displayValue();
// determine game status
if ( sum == myPoint )
gameStatus = WON; // win by making point
else
{
if ( sum == 7 ) // lose by rolling 7
gameStatus = LOST;
} // end else
} // end while
// display won or lost message and return status
if ( gameStatus == WON )
{
cout << "Player wins" << endl;
return WON;
} // end if
else
{
cout << "Player loses" << endl;
return LOST;
} // end else
} // end function craps
// chatter displays messages at random to create "chatter"
void DiceGame::chatter()
{
// choose message at random
switch ( rand() % 9 )
{
case 0:
cout << "\nOh, you're going for broke, huh?";
break;
case 1:
cout << "\nAw cmon, take a chance!";
break;
case 2:
cout << "\nHey, I think this guy is going to break the bank!!";
break;
case 3:
cout << "\nYou're up big. Now's the time to cash in your chips!";
break;
case 4:
cout << "\nWay too lucky! Those dice have to be loaded!";
break;
case 5:
cout << "\nBet it all! Bet it all!";
break;
case 6:
cout << "\nCan I borrow a chip?";
break;
case 7:
cout << "\nLet's try our luck at another table.";
break;
case 8:
cout << "\nYou're a cheat! It is just a matter of time before I catch you!!!";
break;
} // end switch
cout << endl;
} // end function chatter
bool DiceGame::test()
{
Status result; // result of current game
double wager = 100; // wager for current game
double bankBalance = 1000; // current bank balance
int games = 20;
DiceGame d1;
// loop until user enters sentinel value
do
{
// display current balance and prompt for wager
cout << "You have $" << bankBalance << " in the bank.\n";
result = d1.playCraps(); // play game of craps
if ( result == LOST ) // if player lost current game
{
bankBalance -= wager; // decrease balance by wager
// display new balance
cout << "Your new bank balance is $" << bankBalance << "\n";
if ( bankBalance == 0 ) // balance is 0
{
// display message
cout << "Sorry. You Busted! Thank You For Playing.\n";
break; // break out of while loop
} // end if
} // end if
else // else, player won current game
{
bankBalance += wager; // increase balance by wager
// display new balance
cout << "Your new bank balance is $" << bankBalance << "\n";
} // end else
Wagering::wageringTest(wager, bankBalance);
games--;
} while ( games > 0 );
return true;
}
bool Wagering::wageringTest(double currentWager, double currentBankBalance)
{
if (currentBankBalance > 0)
{
while(currentWager > currentBankBalance)
{
return true;
}
}
}