TextBasedRPG not working correctly
I copied this code from a book im reading, and it doesnt work correctly.
Basically, the problem is that instead of the combat simulation going like this:
-diplay user and monster hitpoints
-user attack or run
-monster attack if run failed
-repeat
it goes like this
-display user and monster hitpoints
-user attack or run
-display main menu(move, rest, viewstats, quit)
Here is the code where im almost positive the problem is occurring in:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
bool Player::attack(Monster& monster)
{
int selection = 1;
cout << "1) Attack, 2) Run: ";
cin >> selection;
cout << endl;
switch( selection )
{
case 1:
cout << "You attack an " << monster.getName()
<< " with a " << mWeapon.mName << endl;
if( Random(0, 20) < mAccuracy )
{
int damage = Random(mWeapon.mDamageRange);
int totalDamage = damage - monster.getArmor();
if( totalDamage <= 0 )
{
cout << "Your attack failed to penetrate "
<< "the armor." << endl;
}
else
{
cout << "You attack for " << totalDamage
<< " damage!" << endl;
// Subtract from monster's hitpoints.
monster.takeDamage(totalDamage);
}
}
else
{
cout << "You miss!" << endl;
}
cout << endl;
break;
case 2:
// 25 % chance of being able to run.
int roll = Random(1, 4);
if( roll == 1 )
{
cout << "You run away!" << endl;
return true;//<--Return out of the function.
}
else
{
cout << "You could not escape!" << endl;
break;
}
}
}
|
What if you add a return false;
statement at the end?
at the end of what? the switch statement? one of the cases?
EDIT: Thanks Master Roshi i got it!
Last edited on
Topic archived. No new replies allowed.