Sign in to follow this  

Help with text-based RPG in C++

This topic is 4246 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi there, I'm having a problem with a battle system for a text-based RPG I'm programming in C++. What I want to achieve is this: 1) Prompt the player for action (attack, spell, item, etc.) 2) if player chooses attack, attack the monster 3) check if monster is alive (if not, return true; if so, continue from 1.) Seems like cake, right? I thought so too, and set up a prototype system using a couple of while loops and if statements, but it doesn't quite work as it should. The problem is this: the program works fine while the enemy's health is greater than 0. It loops from the choice until its health is 0 or less. But, when the monster's health hits 0 or less, it displays the message telling you that the monster is dead as it should, but does not return true, apparently, as it loops back to the choice again. If you attack the enemy a second time while its health is 0, the death message displays and it DOES return true as it should. Why would I be getting two different results in the (seemingly) same situation? Any help fixing my current model or advice on a different method would be greatly appreciated. You can take a look at the code here: http://www.chaoticbunny.com/grimesbackups/battle.txt This is far from complete, of course. It only allows for 1 enemy and 1 choice at this stage; although I have functions set up to help with things like multiple monsters and stuff, they are not yet implemented.

Share this post


Link to post
Share on other sites
Quote:
Original post by Silent Dragon
Try putting the check to see if the enemy is dead before you ask the user what they want to do?
Hope this helps,
Al

Your solution seems to have helped a little, since it no longer prompts you for action before returning true. But the same problem seems to exist: rather than having to attack twice, you get the death message twice. It seems as if the return statement doesn't work properly until it is reached a second time. Hmm...

Share this post


Link to post
Share on other sites
Can we see the function that calls battle()? It's possible that the error is in how you are calling it and interpreting the return value, not the code inside the battle() function itself.

Share this post


Link to post
Share on other sites
http://www.chaoticbunny.com/grimesbackups/battulearena.txt

There ya go. Please excuse some of the more ridiculous things in there. I try a little too hard to entertain myself...

Also, there's a lot of code in there that seems pointless right now, but it's there because of plans I have for passing multiple enemies to the battle function and such.

Share this post


Link to post
Share on other sites

battle(player, monsters, 1);

// temp

if (!battle(player, monsters, 1))
{
cout << "Noob! Pwned!";
wait();
// battle failed; lose half your gold & start in town
}


From this source code, it looks like you're battling the monster twice....
First you battle it, then you battle it again and check for the success/failure of the battle....remove the first battle. =)

Cheers and good luck!

Share this post


Link to post
Share on other sites
You're calling battle() twice, once with no checking, once with checking if the value returns true. Just remove the call to the battle funtion that has no checking, and remove the bit that i told you to add in before with the extra death check and you should be fine :)

Share this post


Link to post
Share on other sites
Yay! It works! I hadn't realized that when I checked for success or failure it actually had to call the function. For some reason I thought it would just check the value it returned when it was last called. Guess you only learn through mistakes though, right? Thank you guys so much! :D

Share this post


Link to post
Share on other sites

This topic is 4246 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this