Help with text-based RPG in C++

Started by
6 comments, last by Grimes_ 17 years, 11 months ago
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.
Advertisement
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
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...
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.
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.
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!
Jeromy Walsh
Sr. Tools & Engine Programmer | Software Engineer
Microsoft Windows Phone Team
Chronicles of Elyria (An In-development MMORPG)
GameDevelopedia.com - Blog & Tutorials
GDNet Mentoring: XNA Workshop | C# Workshop | C++ Workshop
"The question is not how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed?" - Il Duche, Boondock Saints
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 :)

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

This topic is closed to new replies.

Advertisement