Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Beginner Question

Status
Not open for further replies.

Beaverbutt8

Programmer
Nov 4, 2005
4
US
Hey guys,

I've been doing c++ for a few weeks now, and i have started to program a game (text game) called "Zalador". Well, i have written a function for the hero guy to fight an enemy, but i need a way to loop it until one of them dies, Here's the source code.

#include <iostream.h>
#include <cstdlib>
#include <ctime>





void fight();



void fight()
{


int hit(int health);

int hite(int healthe);



srand(time(0));

int randomNumber = rand();

int rnd = (randomNumber % 3) + 1;

int rnde = (randomNumber % 3) + 1;

//your health

int health = 80;

//enemy's health

int healthe = 80;



//tell


if ( rnd == 2 )
{


health = hit(health);
cout << "The monster takes a whack at you, and gets you -- Health: " << health << "\n\n";

}
else
{

cout << "The monster takes a whack at you, and misses -- Health: " << health << endl;

}





if ( rnde == 1 )
{


healthe = hite(healthe);
cout << "You strike the monster -- Monster Health: " << healthe << "\n\n";


}
else
{

cout << "You try to strike the monster, but miss -- MonsterHealth: " << healthe << endl;

}





if ( health == 0 )
{
cout << "Sadly, you are killed by the monster" << endl << endl << endl;
cout << " GAME OVER " << endl;
exit (0);
}
else
{

}

if ( healthe == 0 )
{

cout << "You have slain the monster!!!! Well done!" << endl;

}
else
{

}






system ("PAUSE");


}









inline int hit(int health)
{
return (health - 10);

}

inline int hite(int healthe)
{
return (healthe - 10);

}


As i said, i need a way to keep it going until one of them dies. I would greatly apprecaite it if someone could help me.


Thanks :)


Mike
 
Change this bit
Code:
   int randomNumber = rand();
    
   int rnd = (randomNumber % 3) + 1;
   
    int rnde = (randomNumber % 3) + 1;
    
    //your health
    
    int health = 80;
    
    //enemy's health
    
    int healthe = 80;
    do {
       rnd = (rand () % 3) + 1;
       rnde = (rand() % 3) + 1;

and this bit

Code:
} while (health > 0 && healthe > 0);

if ( health == 0 )
 
The question is, how do i do i write i while loop and access health and healthE, since those two integers are inside the function?
 
you could try something like
Code:
void test()
{
	int outcome = fight(80,80);
	if (outcome = 0)
		cout << "Sadly, you are killed by the monster" << endl << endl << endl;
		cout << "		 GAME OVER				  " << endl;
	else if (outcome = 1)
		cout << "You have slain the monster!!!! Well done!" << endl;
	else
	{
		cout << oops, an unscheduled blanket of fog has obscured the fight\nand you have no idea if you won or not";
	}
}

int fight(int heroHealth, int enemyHealth)
{
	int outcome = -1;
	srand(time(0));

	while (enemyHealth > 0 && heroHealth > 0)
	{
		int rnd = (rand() % 3) + 1; 
		int rnde = (rand() % 3) + 1; 

		//tell
		if ( rnde == 2 )
		{
			heroHealth -= EnemyDamage();
			cout << "The monster takes a whack at you, and gets you -- Health: " << heroHealth << "\n\n";
		}
		else
		{
			cout << "The monster takes a whack at you, and misses -- Health: " << heroHealth << "\n\n";
		}
		if heroHealth > 0)
		{
			if ( rnd == 1 )
			{
				enemyHealth -= heroDamage();
				cout << "You strike the monster -- Monster Health: " << enemyHealth << "\n\n";
			}
			else
			{
				cout << "You try to strike the monster, but miss -- MonsterHealth: " << enemyHealth << "\n\n";l;
			}
		}
	system ("PAUSE"); 
	}
	
	if ( heroHealth <= 0 )
	{
		outcome = 0;
	}
	else if ( (enemyHealth <= 0 )
	{
		outcome = 1;
	}
	system ("PAUSE"); 
}

int heroDamage()
{
	return -10;
}

int enemyDamage()
{
	return -10;
}

"If it could have gone wrong earlier and it didn't, it ultimately would have been beneficial for it to have." : Murphy's Ultimate Corollary
 
I tried that thing you wrote, it said "the system file cannot be specified" !!!!!!!!!!!!!!
 
I created a hello world console app in vc++ 6 and replaced the void main(){...} with the following code - changed and debugged it from above and it seems work fine

Code:
// zalador.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream.h>
#include <cstdlib>
#include <ctime>

int fight(int heroHealth, int enemyHealth);
int swingAtFoe(int,int,int);
int heroDamage();
int enemyDamage();

int main(int argc, char* argv[])
{
	int outcome = fight(80,80);
	//hero has died
	if (outcome == 0)
	{
		cout << "Sadly, you are killed by the enemy \n\n\n\t\tGAME OVER\n"<<flush;
	}
	//monster has died
	else if (outcome > 0)
	{
		cout << "You have slain the enemy!!!! Well done!\n"<<flush;
	}
	//error handling
	else
	{
		cout << "oops, an unscheduled blanket of fog has obscured the fight\nand you have no idea if you won or not"<<flush;
	}
	system ("PAUSE");
	return 0;
}

int fight(int heroHealth, int enemyHealth)
{
	int outcome = -1;
	srand(time(0));
	//while there is some life left in both the fighters
	while (enemyHealth > 0 && heroHealth > 0)
	{
		//if the enemy hits
		if ( swingAtFoe(3,3,1))
		{
			heroHealth -= enemyDamage();
			cout << "The enemy takes a whack at you, and gets you -- Health: " << heroHealth << "\n\n";
		}
		else
		{
			cout << "The enemy takes a whack at you, and misses -- Health: " << heroHealth << "\n\n";
		}
		//if the hero isnt dead, he can take a swing
		if (heroHealth > 0)
		{
			//if the hero hits
			if ( swingAtFoe(3,3,1))
			{
				enemyHealth -= heroDamage();
				cout << "You strike the enemy -- enemy Health: " << enemyHealth << "\n\n";
			}
			else
			{
				cout << "You try to strike the enemy , but miss -- enemy Health: " << enemyHealth << "\n\n";
			}
		}
	}
	outcome = heroHealth;
	return outcome;
}
int swingAtFoe(int chance,int limit,int offset)
{
	int swing = (rand() % limit) + offset;
	if (swing >= chance)
		return 1;
	else
		return 0;
}

int heroDamage()
{
	//vary this function to change the amount of damage the hero does
	return 10;
}

int enemyDamage()
{
	//vary this function to change the amount of damage the ememy does
	return 10;
}

"If it could have gone wrong earlier and it didn't, it ultimately would have been beneficial for it to have." : Murphy's Ultimate Corollary
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top