Help with text adventure in C++

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

Recommended Posts

I am making a fairly simple Text Adventure for my C++ Final Project. I use Visual C++ 6.0. Here is the deal. I got most of it done. However, I am running into infinite loops somewhere and I can't find out where. At the end, when you win the last fight, it loops between the exit messages and won't exit. When your Life drops to 0 or lower it keeps going, it is supposed to end. And then when you beat the monster at that point it loops back to right after the first fight. Now keep in mind, this is meant to be simple, and it still needs to be neatened up a little.
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <string>
using namespace std;

public:

void setEntrance();
void setBattle();
void setDirection();
void setLeft();
void setRight();
void setDragon();
void end();

private:
int fight, enter, dragon;
int life, defense, skLife, skDef, drLife, drDef;
int attack, skAtt, drAtt, damage, skDam, drDam;

};

life = 10;
defense = 6;
skLife = 5;
skDef = 4;
drLife = 15;
drDef = 9;

}

cout<< "Your Life Points = " << life << endl;
cout << "Would you like to enter the Dungeon? " << endl;
cout << "1 = yes, 2 = no ";
cin >> enter;

if (enter == 1){
entrance.setBattle();
}
else {
//entrance.end();
}
}

cout << "You enter the dungeon.  Inside you find dark hallways with rubble here and there." << endl;
cout << "\nYou suddenly hear the sound of bone on stone as walking Skeleton "<< endl
<< "comes around the corner, looking at you with death in its eyes." << endl;
while(life > 0){

while(skLife > 0)
{
cout <<  "What is your action?  1 = Attack, 2 = Run Away" << endl;
cin >> fight;
if (fight == 1){
attack = rand()%10+1;

if (attack >= skDef)
{
damage = rand()%3+1;
skLife = skLife - damage;
cout << "You dealt " << damage << " points of damage." << endl;
}
else{
cout << "You missed." << endl;
}

skAtt = (rand()%10+1) - 1;

if (skAtt >= defense) {
skDam = rand()%2+1;
life = life - skDam;
cout << "The Skeleton hit you for " << skDam << "points of damage." << endl;
cout << "LP = " << life << endl;
}
else
{
cout << "The Skeleton missed you." << endl;
}

}
else{
cout << "You run away from the fight, coward." << endl;
battle.setEntrance();
}
}

cout << "You destroy the Skeleton into dust." << endl;
battle.setDirection();
}
cout << "You have met your end.  Have fun in the after life." << endl;
battle.end();
}

cout << "\nBefore you is a T intersection.  In front of you is a door that is locked." << endl;
cout << "\nTo the right is a wooded barrier that can be broken apart to pass." << endl;
cout << "However, as you do so, your sword breaks." << endl;
cout << "\nTo the left is an open corridor." << endl;
system("PAUSE");
direction.setLeft();
}

cout << "\nYou find a gleaming sword imbeded in the stone floor.  The name of the stone says EXCALIBUR." << endl;
cout << "With great strength, you yank the sword from its resting place.  Your combat ability has now improved." << endl;
cout << "\nYou return to the barricade, and with one great swing, you shatter the wood." << endl;
system("PAUSE");
left.setRight();
}

cout << "\nAt the end of this corridor, you find a chest." << endl;
cout << "Inside, you find a Key and a Healing Potion." << endl;
cout << "\nYou return to the Door and insert the Key." << endl;
cout << "The door slides open, allowing you to pass." << endl;
system("PAUSE");

right.setDragon();
}

cout << "As you enter this room, the door slides shut, preventing any escape." << endl;
cout << "Before you is a massive Dragon, which roars in hunger." << endl;
defense = 8;
while(life > 0)
{
while(drLife > 0)
{
cout << "What is your Action? 1 = Attack 2 = Drink Healing Potion" << endl;
cin >> dragon;

if (dragon == 1)
{
attack = (rand()%10+1) + 5;

if (attack >= drDef)
{
damage = (rand()%5+1) + 2;
drLife = drLife - damage;
cout << "You strike the Dragon for " << damage << "points of damage." << endl;
}
else
{
cout << "You missed the Dragon." << endl;
}

}
else
{
cout << "You take a sip of the potion and your wounds heal." << endl;
life = 10;
cout << "LP = " << life << endl;
}

drAtt = (rand()%10+1) + 3;

if (drAtt >= defense)
{
drDam = (rand()%4+1) + 1;
life = life - drDam;
cout << "The Dragon hits you for " << drDam << "points of damage." << endl;
cout << "LP = " << life << endl;
//if (life <= 0){

//}
}
else
{
cout << "The Dragon misses it's attack." << endl;
}
}
cout << "The Dragon falls to the ground dead." << endl;
cout << "You have lifted the curse and saved the village, Congratulations." << endl;
system("PAUSE");

dragonFight.end();
}
cout << "The Dragon grabs you in its mighty claws, and devours you whole.  You have failed in your mission." << endl;

dragonFight.end();
}

cout << "Thanks for playing, GOOD-BYE" << endl;

}

int main()
{

srand(time(NULL));
game.setEntrance();
//game.end();
system("PAUSE");
return 0;


Thanks

Share on other sites
I think one problem is that you doesn't leave your loops, when you win your fight, as your only using life as loop-criterion, and your life is still greater 0 after winning the fight...
The way you thought of, I think is, to put an return in each method after the corresponding adventure::end call like dragonfight::end

The same goes for your inner loop here you just checks for the live of your monster and of your hero...so long as your monster stays alive you won't leave the loop...

I hope this helps...

I would have implemented more of a loop of this way

while IamLife and MonsterALife {

IAmLife = life > 0;
MonsterAlife = drlife > 0;
}

If IamLife { cout << "You Won"; }
If MonsterALife{ cout << "you lost";}

I think you get the point.

Share on other sites
Sweet, it works now. I hate it when the obvious is stareing you right in the face. Thanks for the help. :)

1. 1
Rutin
46
2. 2
3. 3
4. 4
5. 5
JoeJ
19

• 11
• 15
• 9
• 10
• 13
• Forum Statistics

• Total Topics
633004
• Total Posts
3009840
• Who's Online (See full list)

There are no registered users currently online

×