Sign in to follow this  

Help with text adventure in C++

This topic is 4383 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

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;

class adventure {

public:
	adventure();

	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;

};

adventure::adventure() {
	life = 10;
	defense = 6;
	skLife = 5;
	skDef = 4;
	drLife = 15;
	drDef = 9;
	
}

void adventure::setEntrance() {
	adventure entrance;
	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();
	}
}

void adventure::setBattle(){
	adventure battle;

	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();
}

void adventure::setDirection() {
	adventure direction;
	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();
}
	

void adventure::setLeft() {
	adventure left;
	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();
}

void adventure::setRight() {
	adventure right;
	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();
}
void adventure::setDragon() {
	adventure dragonFight;

	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();
}

void adventure::end(){
	cout << "Thanks for playing, GOOD-BYE" << endl;
	
}

int main()
{
	adventure game;

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


Thanks

Share this post


Link to post
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 this post


Link to post
Share on other sites

This topic is 4383 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