Archived

This topic is now archived and is closed to further replies.

graveyard filla

need help writing classes in c++

Recommended Posts

well i am pretty new to learning how to use classes now and after getting some help from people on this board i tried implementing classes into my program... well i changed everything . I am working on a simple and small text based RPG, and i moved most of my functions and re-wrote them into functions of an enemy and player class. and now when it compiles i get 35 errors.... all of them have to do with when i send an object to a function(the function that recieves the object could be in main or in another class)... that function never heard of any of the objects variables or internal functions... in fact i even get an error when i pass the object itself, at the line of it being passed. like enter_combat(player ¤t_player, enemy &monster) at this line i get an error and its even saying its never heard of either of these objects. can anyone help? here is the source. please let me know what im doing wrong. thanks
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <string>
using namespace std;



class player
{

	public:
	
	player(int init_health,int init_stam,int init_mana,int init_bullets);
	~player();
	int attack_prompt(enemy &drifter,string enemy_name);
	void attack(enemy &drifter);
	void take_dmg(int enemydmg);
	void use_stamina(int used_stamina);
	void use_mana(int used_mana);
	void use_bullets(int bullets);
	void cast_heal(int healed_for);
	void shoot(int used_bullets);
	
//private:

	int health;
	int stamina;
	int mana;
	int bullets;
	
};

player::player(int init_health,int init_stam,int init_mana,int init_bullets)
{
	 health = int init_health;
	 stamina = init_stam;
	 mana = init_mana;
	 bullets = init_bullets;
}

int player::attack_prompt(enemy &drifter,string enemy_name)
//void attack_prompt(string enemy_name,player ¤t_player, enemy &drifter)

//(int player_stamina,int player_mana,int bullets,int enemy_health,char &attack_type,string enemy_name)

{			

			cout<<"You have:              "<<"                   "<<enemy_name<<" has:     "<<endl;
			cout<<"***********************"<<"                   *********************"<<endl;
			cout<<"---"<<health<<"---"<<"--health------"<<"                   -------"<<enemy_health<<"------health"<<endl;
			cout<<"---"<<stamina<<"---"<<"---stamina-----"<<"                  *********************"<<endl;
			cout<<"---"<<mana<<"---"<<"---mana--------"<<endl<<endl;
			cout<<"---"<<bullets<<"---"<<"---bullets--------"<<endl<<endl;
			cout<<"********************************************************************************"<<endl<<endl;
		
			
			
			cout<<"Type the number for the attack type and press enter"<<endl<<endl;
			//prompts the user to enter what kind of attack they want to do

				if (bullets > 0) //if the player has bullets, show fire gun as an option

				{

					cout<<"[1 for sword attack,2 for magic,or 3 to fire gun]"<<endl;
					cin>>attack_type;
					
					if (attack_type == ''2'') //if the player chose magic, show the spell choice

					{
						cout<<"[1 for fireball, 2 for HEAL]"<<endl;
						cin>>attack_type;
						
						if (attack_type == ''1'') 
						{
							attack_type = ''2''; //this converts the users input from ''1'' to ''2'',

												//because attack_type of ''2'' will do a fireball

												//and before this attack_type is ''0''

						}
						else if (attack_type == ''2'')
						{
							attack_type = ''4''; //this convers the users input from ''2'' to ''4''

										      //because ''4'' is the heal spell 

						}
					}

					
					cout<<endl<<endl;
					system("cls");
				}

				else if (bullets <= 0) //if the player doesnt have bullets, dont show the gun

				{
					cout<<"[1 for sword attack,2 for magic]"<<endl;
					cin>>attack_type;
					
					if (attack_type == ''2'') //if the player chose magic, show the spell choice

					{
						cout<<"[1 for fireball, 2 for HEAL]"<<endl;
						cin>>attack_type;
						
						if (attack_type == ''1'') 
						{
							attack_type = ''2''; //this converts the users input from ''1'' to ''2'',

												//because attack_type of ''2'' will do a fireball

												//and before this attack_type is ''0''

						}
						else if (attack_type == ''2'')
						{
							attack_type = ''4''; //this convers the users input from ''2'' to ''4''

										      //because ''4'' is the heal spell 

						}

					}

									
					cout<<endl<<endl;
					system("cls");
					
				}

return attack_type;

}




void player::attack(enemy &drifter)
//int &enemy_health,char attack_type,int &player_stamina,int &player_mana,int &bullets) 

//calculates players attack and sends back updated enemy health, player stamina, and player mana


{
	int attack = 0;
	int defence = 0;
	int damage = 0;
	int used_stamina = 0;
	int used_mana = 0;
	int used_bullets = 0;
	int healed_for = 0;
	int attack_type = attack_prompt();

	attack = (rand () % 11) + 2; //generates random number for attack

									// a dice number (2-12)

	defence = (rand () % 11) + 2; //generats random number  for defence

									// a dice number

	
	if (attack > defence)  //compares attack to defence, if bigger, it is a hit and 

							//then calculates damage and updates enemy_health

						
	{
		
		if(attack_type == ''1'') //if the attack type was a sword attack, then

		{
			
			if (player_stamina < 4) //check to see if the player has enough stamina to execute 

									//the attack

			{
				cout<<"|=|=|=|=|You are exhausted|=|=|=|=|"<<endl<<endl; //if player doesnt have enough stamina,

												//will print "you are exhausted", and bail 

												//out of the function completely

			}
			else
			{
			
			cout<<"****You hit!****"<<endl<<endl; //else the player has enough stamina, print

									//you hit, then

			
			damage =  ((rand () % 11) + 2); //calculate damage (random number 1-12)

			
			used_stamina = 4; //and use 4 stamina

			}
			
			
			if (damage > 10) //if damage is more then 10, say you sliced him

			{
				cout<<"You sliced the enemy for "<<damage<<" damage!"<<endl<<endl;
			}
			else if (damage > 0) //if its less then 10 but more then 0 (ie you had enough stamina)

									//print you stabbed him

			{
				cout<<"You stabbed the enemy for "<<damage<<" damage!"<<endl<<endl;
			}
			
		}
		
		else if(attack_type == ''2'') //OR if attack type was a magic attack

		{
			
				if (player_mana >= 5) //check to see if player has enough mana

				{
					cout<<"****You hit!****"<<endl<<endl; //since the player does, say "you hit"

					damage =  ((rand () % 20) + 2); //get damage from random number 2-20

					used_mana = 5;//use 5 mana

					cout<<"The enemy was burned "<<damage<<" damage!"<<endl<<endl;
					//print the enemy was burned for "damage"

				
				}
			
			else //if player doesnt have enough mana

			{
				cout<<"|=|=|=|=|Not enough mana to cast spell|=|=|=|=|"<<endl<<endl; //say you dont have enough mana

			}
			
			
			
		}			
	
		else if (attack_type == ''3'') //OR you have a gun

		{
			damage = ((rand () % 25) + 2); //damage is a random 2-26

		
			
			if (bullets >= 1) //if you have bullets, say you hit, and use a bullet

			{
				used_bullets = 1;
				cout<<"***You hit!***"<<endl<<endl;
			}
			else if (bullets <= 0) //if you dont have bullets, say your out of ammo

			{
				used_bullets = 0;
				cout<<" YOU ARE OUT OF AMMO   "<<endl<<endl;

			}
			
			cout<<"The enemy was blasted for "<<damage<<" damage!"<<endl<<endl; //print damage

		}
		
		else if (attack_type == ''4'') //OR the player casted heal

		{
			
		
			
			if (player_mana >= 3) //if you have mana, then say success, use mana and add 1-36 health

			{
				used_mana = 3;
				healed_for = ((rand () % 35) + 2); //heals for a random 2-36

				cout<<"***The spell was a success!***"<<endl<<endl;
				cout<<"You healed yourself for "<<healed_for<<" health!"<<endl<<endl; //print damage

				
			}
			else if (player_mana < 3) //if you dont have bullets, say your out of ammo

			{
				used_mana = 0;
				cout<<" YOU ARE OUT OF MANA   "<<endl<<endl;

			}
			
			
		}
		
		
	
	}

	
	else if (defence >= attack) //if defence is bigger, you miss.

	{
		damage = 0;
		
		if (attack_type == ''1'') //if you miss and used a sword attack, do the follow:

		{
			if (player_stamina > 4) //check to see if they have enough stamina

			{

			used_stamina = 2; //if they do have enough stamina, take 2 away

			cout<<"******You missed!*****"<<endl<<endl; //print "you missed"

			}
			else if (player_stamina < 4) //else if they dont have enough stamina

			{
				cout<<"|=|=|=|=|You are exhausted|=|=|=|=|"<<endl<<endl; //print you are exhausted.

			}
		}
		
		else if (attack_type == ''2'') //if you misee and used magic,

		{
			if (player_mana >= 5) //check to see if they have enough mana

			{
			used_mana = 3; //if they do, use 3 mana and

			cout<<"|=|=|=|=|The spell fizzled|=|=|=|=|"<<endl<<endl; // say the spell fizzled

			}
			else if (player_mana < 5) //if they dont have enough mana

			{
				cout<<"|=|=|=|=|=|Not enough mana to cast spell|=|=|=|=|=|"<<endl<<endl; //say "do not have enough mana"

			}
		}
		else if (attack_type == ''3'')
		{
			if (bullets >= 1)
			{
				used_bullets = 1;
				cout<<"+++++++YOU MISEED!++++++"<<endl<<endl;
			}
			else if (bullets <= 0)
			{
				used_bullets = 0;
				cout<<" YOU ARE OUT OF AMMO   "<<endl<<endl;
			}
		}
		else if (attack_type == ''4'') //OR the player casted heal

		{
			
		
			
			if (player_mana >= 3) //if you have mana, then say success, use mana and add 1-36 health

			{
				used_mana = 2;
				cout<<"***The spell fizzled!***"<<endl<<endl;
			}
			
			else if (player_mana < 3) //if you dont have bullets, say your out of ammo

			{
				used_mana = 0;
				cout<<" YOU ARE OUT OF MANA   "<<endl<<endl;
			}
			
			
		}
	
	
	
	
	}
	
	
	
	drifter.take_dmg(damage); //update enemies health

	current_player.use_stamina(used_stamina); //update stamina used

	current_player.use_mana(used_mana); //update mana used 

	current_player.heal(healed_for);
	current_player.shoot(used_bullets);

	
}




void player::take_dmg(int enemydmg)
{
	health = health-enemydmg;
	
	if health = 0
	{
		cout<<"You died. Game over."<<endl;
	}
}

void player::use_stamina(int used_stamina)
{
	stamina = stamina - used_stamina;
}

void player::use_mana(int used_mana)
{
	mana = used_mana - mana;
}

void player::cast_heal(int healed_for)
{
	health = healed_for + health;
}

void player::shoot(int used_bullets)
{
	bullets = bullets - used_bullets;
}

class enemy
{

		public:

		void attack(player ¤t_player);

		void take_damage(int damage);

		//private:

		
		int health;
		

};



void enemy::attack(player ¤t_player)
{	
	
	int attack;
	int defence;
	int enemydmg;
	int attack_type;
	
	
	attack = (rand () % 11) + 2; //makes attack a random between 1-12


	defence = (rand () % 11) + 2; //makes defence a random between 1-12

	
	

	if (attack > defence) //if attack > defence, the PC hits the player

	{
		
		attack_type = rand () % 100; //generates a random number between 1-100, calling it attack_type


		if(attack_type > 65) //if the number is > 50, then do a melee attack

		{
			
			cout<<"$=$=$=You were hit!=$=$=$"<<endl<<endl; //prints you were hit

			enemydmg =  (rand () % 11) + 2; //enemy damage for this melee attack is a random

											//between 1-12

			cout<<"Enemy bashed you for "<<enemydmg<<" damage!"<<endl<<endl;
			//print the damage done

		}

		else //if the number is < 50, then do a magic attack

		{
			
			cout<<"$=$=$=$=You were hit!=$=$=$=$"<<endl<<endl; //prints you were hit

			enemydmg =  (rand () % 20) + 2; //enemy damage for this "lightning" attack

											//is a random between 2-20

			cout<<"Enemy bolted you for "<<enemydmg<<" damage!"<<endl<<endl;
			//print the damage done

			
			
		}
	}
	
	else if (defence >= attack)  //if defence > attack, the pc missed the player

	
	{
		enemydmg = 0;
		cout<<"=$=$=$=$They missed you!=$=$=$=$"<<endl<<endl; //print they missed you

	}
	
	//updated player health

current_player.take_damage(enemydmg)

}

void enemy::take_dmg(int player_damage);
{
	health = health - damage;
}

		
		
		
		








string random_enemy_name();
//all this does is give a random chance of the person you fights name. soon will add this to

//effect stats, hps, attack types, etc. maybe then name it to enemy_stats or something


bool random_bool();
//a function that randomizes the event of an event (block of code) happening. such as drop loot,

//etc.it basically just returns true or false on a 50/50 chance. could be switch to 70/30 or different.


void intro_screen (); //a simple print screen function that prints the welcome screen


void attack_prompt(int player_stamina,int player_mana,int bullets,int enemy_health,char &attack_type,string enemy_name);
//just a print screen of the current stats for player/enemy. also prompts for next attack

//and returns attack_type




//this function calculates the enemies attack, and sends back the players updated health


void player_movement (int &spaces_moved);//this prints the movment keys and also

											//starts a counter called spaces_moved

											//which records how many spaces the player moved


void rand_attack (bool &battle_now); //this calculates the chances of a random

													//attack. it doesnt start untill the player

													//moves at least one space. it returns a 

													//boolean value of true if combat is started

													//and false if no combat started.


void enter_combat(int &player_stamina,int &player_mana,int &bullets);
//this manages all combat. it called the player_attack and enemy_attack functions, updates

//all health,stamina, and mana. it also prints out results of each hit etc.





int main()
{
	
	player current_player(200,120,65,0);
	enemy drifter;
	int spaces_moved = 0;
	bool battle_now;
	
	srand (time (NULL)); //set the seed to the internal clock of the computer

	
	
	intro_screen(); //simply prints the intro screen to the screen

	
	
	do
	{
	
	player_movement(spaces_moved); //pop up the movement screen


	rand_attack(battle_now);//check for random attack (80% chance of attack)

	
	
		if (battle_now == true) //if the attack is true, then enter combat, if not, go back

			//to the top, ask for movement, and look for another random attack

		{
			enter_combat(current_player, enemy drifter);
		}
	
	}

	while (current_player.health > 0 && spaces_moved < 20); //do this all untill the player dies

													//or beats the game

	
			
	if (player_health < 0) //if the player died, say game over.

		{

			cout<<"You are dead"<<endl;

			cout<<"Game Over"<<endl;
		}
	
	else if (spaces_moved == 20) //if the player reached the end, say you win

	{
		cout<<"**************************************"<<endl;
		cout<<" YOU WIN!! You have reached the end!!!"<<endl<<endl;
		cout<<"**************************************"<<endl;

	}

	return 0;
}

void intro_screen () //simply prints the welcome screen to the player. 

{
	cout<<"********************************************************************************"<<endl;
	cout<<"*********************************WELCOME TO FTA*********************************"<<endl;
	cout<<"********************************************************************************"<<endl;
	cout<<"**************************DONKEY PUNCH PRODUCTIONS INC.*************************"<<endl;
	cout<<"********************************************************************************"<<endl;
}



void player_movement (int &spaces_moved)
{
	
	char direction;
	
	cout<<"Press ''W'' to move up"<<endl;
	cout<<"Press ''A'' to move left"<<endl;
	cout<<"Press ''S'' to move right"<<endl;
	cout<<"Press ''Z'' to move backwards"<<endl<<endl;
	cin>>direction;

	if (direction == ''w'')
	{
		cout<<"You moved forward."<<endl<<endl;
	}
	else if (direction == ''a'')
	{
		cout<<"You moved left."<<endl<<endl;
	}
	else if (direction == ''s'')
	{
		cout<<"You moved right."<<endl<<endl;
	}
	else
	{
		cout<<"You moved backwards."<<endl<<endl;
	}
	
	spaces_moved = spaces_moved + 1;
	cout<<"You moved a total of "<<spaces_moved<<" spaces."<<endl<<endl;	
}

void rand_attack (bool &battle_now)
{

		
		int attack_chance = (rand () % 100) + 1; //this sets up a random fight wiht 15% chance of happening



		if (attack_chance > 65)
		{
			battle_now = true;
		}
		else if (attack_chance <=65 )
		{
			battle_now = false;
		}
	

}

void enter_combat(player ¤t_player,enemy &drifter)
{

			
	
			string enemy_name = random_enemy_name();
			int bullets_found = 0;
			bool gun;
			
			system("cls");
			
			cout<<"**************************YOU HAVE ENTERED COMBAT!*****************************"<<endl<<endl;			
	do 
	//show enemy/player status

	//do player attack

	//do enemy attack

	//while player and enemy are both still alive

	{
			current_player.attack_prompt(enemy &drifter, string enemy_name);
			
			//player_attack (enemy_health,attack_type,player_stamina,player_mana,bullets);

			current_player.attack(enemy &drifter);	
			//this calculates the players attack, and updates enemyhealth/player stamina/mana

		
			drifter.attack(player ¤t_player);
			
			//this calculates the enemies attack, and updates player health.

	}
		while ((current_player.health > 0) && (drifter.health > 0)); 
		//runs this battle untill one of the player dies.

		
		
		
		//this down here is clean up code after the enemy dies

		//it prints if i defeated them, it resets the enemies health to 40

		//and it gives a random chance for a gun to drop.

		if (drifter.health < 0)
		{
			cout<<"You have defeated the enemy"<<endl;
			
			drifter.health = 40;
		
			gun = random_bool();

			if (gun == true)
			{
				cout<<"Enemy dropped a gun"<<endl<<endl;
				bullets_found = 12;
			}

		}

		
		 current_player.bullets = bullets_found + bullets;


}
	
bool random_bool()
{
	
	int random = rand() % 100;

	if (random >= 50)
	{
		return true;
	}
	else 
	{
		return false;
	}

}




string random_enemy_name()
{
	string enemy_name;
	int name_chance = rand() % 100;

		
		if (name_chance < 25)
		{
			enemy_name = "Hit Man";
		}
		else if (name_chance > 25 && name_chance < 50)
		{
			enemy_name = "Orc Gunner";
		}
		else if (name_chance > 50 && name_chance < 75)
		{
			enemy_name = "Drifter";
		}
		else
		{
			enemy_name = "Cop";
		}

		return enemy_name;
}

Share this post


Link to post
Share on other sites
ps i kept everything public because i didnt want to write accessor functions if i couldnt write the classes properly in the first place. (apparently i couldnt)


also in my code, hitman is the object of enemy, instead of monster, this enemy is called "hitman". i couldnt think of a better name.

[edited by - graveyard filla on January 17, 2004 5:26:45 AM]

[edited by - graveyard filla on January 17, 2004 5:27:18 AM]

Share this post


Link to post
Share on other sites
but i see you just call the value enemy_health without identifieng from where it comes.
you handle these variables like global ones not the ones from the enemy class.

something like this:

class a
{
public:
int bla;

a();
~a();
};

a foo;

foo.bla

will work.


second:
current_player.use_stamina(used_stamina); <--- well thats quite some bogus.
just leave the "current_player." away and it will work fine or use this:
this->use_stamina(used_stamina);

and because you define the class enemy after the class player the player class can't use it because it just don't know about it.
solution: put a class enemy; and a class player; at the beginning of your file and its fine.

another tip:
use header files for the class definitions (seperate ones are nice because of the modularity effect) also put the source in seperate files (same reason as with the header files)

hope i helped a litte (have a little hangover *gg*)


[edited by - zeon on January 17, 2004 6:11:57 AM]

Share this post


Link to post
Share on other sites
thanks alot for your help... you cut my errors down from 35 to 21. but i have some more questions.

what does this->use_stamina(used_stamina) mean?

i understand the second half, i just dont know what this->

and does it have to be the word this, or is it non specific, and if so how do i specify it?> also, im still having problems with classes not knowing who other classes are. you said to put both class enemy and class player at the begining, i wanst sure what you meant. did you mean make 2 copies of each one at the begining? i tried that and it still wont work.

(what i did was put
class enemy{
blah blah
}
class player
}
blah blah
}
class enemy
{
(again)
}
class player
{
(again)
}

and it didnt work... although i should have known it wouldnt work because it looks really stupid... so what did you mean exactly by put them both at the beginig of the file? thanks for your help man.

Share this post


Link to post
Share on other sites
ok, easiest thing first ... and this is a little piece of advice you didn''t ask for, sorry. I recommend you use an "enum" for your attack type, because it will make your code read better ... with an enum your attack types would be stuff like: sword, dager ... or slashing, piercing ... or whatever you want them to be ... not ''2'' ''3'' - etc ...

but enough on that -

look at the following lines of code:


MyClass someObject();
MyClass *someObjectPointer = new MyClass();

someObject.Function(parameter1);
someObjectPointer->Function(parameter1);
(*someObjectPointer).Function(parameter1);


So you see, the * operator is used to delare an object as a pointer, and to dereference that pointer. The -> is just a special operator that applies the * operator and the . operator together, to elimate the need for (parenthesis) in very common operations, such as this:

myObjectPointer->nextOnjectPointer->hisChildPointer->value

is much nicer to read than:

(*(*(*myObjectPointer).nextObjectPointer).hisChildPointer).value

which may not even be right (cause I can''t remember precendence completely off the top of my head, and I always use the -> operator instead ...

THINK of the -> operator as the . operator, but the -> operator is for pointers, and the . operator is for non-pointers.

Share this post


Link to post
Share on other sites
The easiest way for classes to know about each other is to declare them friends, but that should only be used in cases where it makes sense (a FileManager should be allowed to access the internals of all File objects it manages). I haven''t read through your code, but I think that better design would get rid of the interdependencies between classes. So in other words declaring friends would make it a quick hack, which you should then proceed to fix immediately.

Also, urging us to help you isn''t going to make the answer any easier. Neither is saying "only 21 errors" left, even though it may feel better to give your program a number to describe its problems.

Share this post


Link to post
Share on other sites
so, how do i let classes know about each other? i have to declare them friends? how do i do this??? also, you say this is just a quick fix, then how should i fix this in a good, long term way? i just dont understand because in a different post i posted yesterday, zahlman told me instead of sending a value of an object, you send the entire object as a reference and then call the part of the object you wanted to use. but how do i do this if the class never heard of the object? (because that objects class was declared before the first class.)

Share this post


Link to post
Share on other sites
Header file

// This is the part Zeon was talking about.

class Player;
class Enemy;
// Notice no {}''s. These statements are declarations, and later

// you will provide definitions. It''s basically the same as when

// you do something like

// int foo;

// and later

// foo = 1;

// instead of all at once.


Source file

class Player {
// I can refer to the "Enemy" class in here now

// since the header told the compiler that I am going to define it.

// Declare the methods of this object.

// We could define them in here instead, by using {} instead of

// ; and putting the code inside the {}.

// Sometimes you will see this whole "class Player" block inside

// the header file, with the code being in the source file. You

// shouldn''t ever put real code in your header file though, as a

// rule.

public:
void doSomething(Enemy & enemy);
int foo;
}

class Enemy {
// Similarly, I can refer to "Player" in here.

// Without the pre-declaration, I could still refer to "Player"

// in here (assuming that class definition compiled OK). The

// compiler doesn''t know how to read things out of order, see.

// So if you have a circularity of reference you need to declare

// ahead of time what you''re going to do.

}

void Player::doSomething(Enemy & hitman) {
// the Player:: means that this function is a [b]method[/b] of Player.

// Within this function (and the same would be true if you

// wrote this up in the class Player{} block, except there you

// wouldn''t need or want the Player::), the keyword [b]this[/b]

// is a [i]pointer[/i] to some particular [b]instance[/b] of

// Player. "this" is a special keyword; you can''t change it

// (except maybe with preprocessor #define, but don''t!!!) and

// you don''t have to do anything special (just use it).

// This function cannot be called on its own like

// "doSomething(hitman)". Instead, you need to invoke it from

// an existing Player. E.g.

// Player you;

// you.doSomething(hitman);

// Then, in this code, the value "this" is a pointer to "you".

// So you can for example write

this->foo++;
// and the foo [b]member[/b] (or [b]field[/b]) of "you" is incremented.

// This works because the foo->bar is a shortcut for (*foo).bar

// like Xai described.

// Now, the parameter ''hitman'' is an instance of Enemy, passed by reference. Note:

// (1) that''s not a pointer, so we use . rather than ->.

// (2) This method belongs to Player, which isn''t allowed to

// access anything [b]private[/b] or [b]protected[/b] in Enemy.

// Restricting yourself like that is a Good Thing; it lets you

// make changes within Enemy without having to worry about the

// rest of the code (unless you change the "public interface"

// of Enemy).

}

void myFunction(Enemy &hitman, Player &you) {
// This is a normal function that doesn''t belong to any object.

// You could use it as a wrapper for the classes, like:

you.doSomething(hitman);
// You can''t use "this" in here, because there''s nothing that

// it could refer to!

}

int main(int argc, char **argv) { // Here''s your entry point. Objects describe things that they

// [i]can[/i] do when asked; you need to create and then ask

// them.

}

Share this post


Link to post
Share on other sites
thanks for the help, but i cant figure out what im doing wrong. im still getting errors. here is the source

#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <string>
class player;
class enemy;
using namespace std;



class player
{

public:

player(int init_health,int init_stam,int init_mana,int init_bullets);
~player();
char attack_prompt(enemy &drifter,string enemy_name);
void attack(enemy &drifter);
void take_dmg(int enemydmg);
void use_stamina(int used_stamina);
void use_mana(int used_mana);
void use_bullets(int bullets);
void cast_heal(int healed_for);
void shoot(int used_bullets);

//private:

int health;
int stamina;
int mana;
int bullets;

}current_player(200,120,65,0);




player::player(int init_health,int init_stam,int init_mana,int init_bullets)
{
health = init_health;
stamina = init_stam;
mana = init_mana;
bullets = init_bullets;
}

char player::attack_prompt(enemy &drifter,string enemy_name)
//void attack_prompt(string enemy_name,player ¤t_player, enemy &drifter)

//(int player_stamina,int player_mana,int bullets,int enemy_health,char &attack_type,string enemy_name)

{
char attack_type;
cout<<"You have: "<<" "<<enemy_name<<" has: "<<endl;
cout<<"***********************"<<" *********************"<<endl;
cout<<"---"<<health<<"---"<<"--health------"<<" -------"<<drifter.health<<"------health"<<endl;
cout<<"---"<<stamina<<"---"<<"---stamina-----"<<" *********************"<<endl;
cout<<"---"<<mana<<"---"<<"---mana--------"<<endl<<endl;
cout<<"---"<<bullets<<"---"<<"---bullets--------"<<endl<<endl;
cout<<"********************************************************************************"<<endl<<endl;



cout<<"Type the number for the attack type and press enter"<<endl<<endl;
//prompts the user to enter what kind of attack they want to do

if (bullets > 0) //if the player has bullets, show fire gun as an option

{

cout<<"[1 for sword attack,2 for magic,or 3 to fire gun]"<<endl;
cin>>attack_type;

if (attack_type == ''2'') //if the player chose magic, show the spell choice

{
cout<<"[1 for fireball, 2 for HEAL]"<<endl;
cin>>attack_type;

if (attack_type == ''1'')
{
attack_type = ''2''; //this converts the users input from ''1'' to ''2'',

//because attack_type of ''2'' will do a fireball

//and before this attack_type is ''0''

}
else if (attack_type == ''2'')
{
attack_type = ''4''; //this convers the users input from ''2'' to ''4''

//because ''4'' is the heal spell

}
}


cout<<endl<<endl;
system("cls");
}

else if (bullets <= 0) //if the player doesnt have bullets, dont show the gun

{
cout<<"[1 for sword attack,2 for magic]"<<endl;
cin>>attack_type;

if (attack_type == ''2'') //if the player chose magic, show the spell choice

{
cout<<"[1 for fireball, 2 for HEAL]"<<endl;
cin>>attack_type;

if (attack_type == ''1'')
{
attack_type = ''2''; //this converts the users input from ''1'' to ''2'',

//because attack_type of ''2'' will do a fireball

//and before this attack_type is ''0''

}
else if (attack_type == ''2'')
{
attack_type = ''4''; //this converts the users input from ''2'' to ''4''

//because ''4'' is the heal spell

}

}


cout<<endl<<endl;
system("cls");

}

return attack_type;

}




void player::attack(enemy &drifter)
//int &enemy_health,char attack_type,int &player_stamina,int &player_mana,int &bullets)

//calculates players attack and sends back updated enemy health, player stamina, and player mana


{
int attack = 0;
int defence = 0;
int damage = 0;
int used_stamina = 0;
int used_mana = 0;
int used_bullets = 0;
int healed_for = 0;
char attack_type = attack_prompt (enemy drifter,enemy_name);

attack = (rand () % 11) + 2; //generates random number for attack

// a dice number (2-12)

defence = (rand () % 11) + 2; //generats random number for defence

// a dice number


if (attack > defence) //compares attack to defence, if bigger, it is a hit and

//then calculates damage and updates enemy_health


{

if(attack_type == ''1'') //if the attack type was a sword attack, then

{

if (stamina < 4) //check to see if the player has enough stamina to execute

//the attack

{
cout<<"|=|=|=|=|You are exhausted|=|=|=|=|"<<endl<<endl; //if player doesnt have enough stamina,

//will print "you are exhausted", and bail

//out of the function completely

}
else
{

cout<<"****You hit!****"<<endl<<endl; //else the player has enough stamina, print

//you hit, then


damage = ((rand () % 11) + 2); //calculate damage (random number 1-12)


used_stamina = 4; //and use 4 stamina

}


if (damage > 10) //if damage is more then 10, say you sliced him

{
cout<<"You sliced the enemy for "<<damage<<" damage!"<<endl<<endl;
}
else if (damage > 0) //if its less then 10 but more then 0 (ie you had enough stamina)

//print you stabbed him

{
cout<<"You stabbed the enemy for "<<damage<<" damage!"<<endl<<endl;
}

}

else if(attack_type == ''2'') //OR if attack type was a magic attack

{

if (mana >= 5) //check to see if player has enough mana

{
cout<<"****You hit!****"<<endl<<endl; //since the player does, say "you hit"

damage = ((rand () % 20) + 2); //get damage from random number 2-20

used_mana = 5;//use 5 mana

cout<<"The enemy was burned "<<damage<<" damage!"<<endl<<endl;
//print the enemy was burned for "damage"


}

else //if player doesnt have enough mana

{
cout<<"|=|=|=|=|Not enough mana to cast spell|=|=|=|=|"<<endl<<endl; //say you dont have enough mana

}



}

else if (attack_type == ''3'') //OR you have a gun

{
damage = ((rand () % 25) + 2); //damage is a random 2-26



if (bullets >= 1) //if you have bullets, say you hit, and use a bullet

{
used_bullets = 1;
cout<<"***You hit!***"<<endl<<endl;
}
else if (bullets <= 0) //if you dont have bullets, say your out of ammo

{
used_bullets = 0;
cout<<" YOU ARE OUT OF AMMO "<<endl<<endl;

}

cout<<"The enemy was blasted for "<<damage<<" damage!"<<endl<<endl; //print damage

}

else if (attack_type == ''4'') //OR the player casted heal

{



if (mana >= 3) //if you have mana, then say success, use mana and add 1-36 health

{
used_mana = 3;
healed_for = ((rand () % 35) + 2); //heals for a random 2-36

cout<<"***The spell was a success!***"<<endl<<endl;
cout<<"You healed yourself for "<<healed_for<<" health!"<<endl<<endl; //print damage


}
else if (mana < 3) //if you dont have bullets, say your out of ammo

{
used_mana = 0;
cout<<" YOU ARE OUT OF MANA "<<endl<<endl;

}


}



}


else if (defence >= attack) //if defence is bigger, you miss.

{
damage = 0;

if (attack_type == ''1'') //if you miss and used a sword attack, do the follow:

{
if (stamina > 4) //check to see if they have enough stamina

{

used_stamina = 2; //if they do have enough stamina, take 2 away

cout<<"******You missed!*****"<<endl<<endl; //print "you missed"

}
else if (stamina < 4) //else if they dont have enough stamina

{
cout<<"|=|=|=|=|You are exhausted|=|=|=|=|"<<endl<<endl; //print you are exhausted.

}
}

else if (attack_type == ''2'') //if you misee and used magic,

{
if (mana >= 5) //check to see if they have enough mana

{
used_mana = 3; //if they do, use 3 mana and

cout<<"|=|=|=|=|The spell fizzled|=|=|=|=|"<<endl<<endl; // say the spell fizzled

}
else if (mana < 5) //if they dont have enough mana

{
cout<<"|=|=|=|=|=|Not enough mana to cast spell|=|=|=|=|=|"<<endl<<endl; //say "do not have enough mana"

}
}
else if (attack_type == ''3'')
{
if (bullets >= 1)
{
used_bullets = 1;
cout<<"+++++++YOU MISEED!++++++"<<endl<<endl;
}
else if (bullets <= 0)
{
used_bullets = 0;
cout<<" YOU ARE OUT OF AMMO "<<endl<<endl;
}
}
else if (attack_type == ''4'') //OR the player casted heal

{



if (mana >= 3) //if you have mana, then say success, use mana and add 1-36 health

{
used_mana = 2;
cout<<"***The spell fizzled!***"<<endl<<endl;
}

else if (mana < 3) //if you dont have bullets, say your out of ammo

{
used_mana = 0;
cout<<" YOU ARE OUT OF MANA "<<endl<<endl;
}


}




}



drifter.take_damage (damage); //update enemies health

use_stamina(used_stamina); //update stamina used

use_mana(used_mana); //update mana used

heal(healed_for); //updates healed for (if player chose heal)

shoot(used_bullets);//updates new amount of bullets (if player used gun)



}




void player::take_dmg(int enemydmg)
{
health = health - enemydmg;

if (health = 0)
{
cout<<"You died. Game over."<<endl;
}
}

void player::use_stamina(int used_stamina)
{
stamina = stamina - used_stamina;
}

void player::use_mana(int used_mana)
{
mana = used_mana - mana;
}

void player::cast_heal(int healed_for)
{
health = healed_for + health;
}

void player::shoot(int used_bullets)
{
bullets = bullets - used_bullets;
}

class enemy
{

public:

void attack(player ¤t_player);

void take_damage(int damage);

// private:


int health;


};



void enemy::attack(player ¤t_player)
{

int attack;
int defence;
int enemydmg;
int attack_type;


attack = (rand () % 11) + 2; //makes attack a random between 1-12


defence = (rand () % 11) + 2; //makes defence a random between 1-12




if (attack > defence) //if attack > defence, the PC hits the player

{

attack_type = rand () % 100; //generates a random number between 1-100, calling it attack_type


if(attack_type > 65) //if the number is > 50, then do a melee attack

{

cout<<"$=$=$=You were hit!=$=$=$"<<endl<<endl; //prints you were hit

enemydmg = (rand () % 11) + 2; //enemy damage for this melee attack is a random

//between 1-12

cout<<"Enemy bashed you for "<<enemydmg<<" damage!"<<endl<<endl;
//print the damage done

}

else //if the number is < 50, then do a magic attack

{

cout<<"$=$=$=$=You were hit!=$=$=$=$"<<endl<<endl; //prints you were hit

enemydmg = (rand () % 20) + 2; //enemy damage for this "lightning" attack

//is a random between 2-20

cout<<"Enemy bolted you for "<<enemydmg<<" damage!"<<endl<<endl;
//print the damage done



}
}

else if (defence >= attack) //if defence > attack, the pc missed the player


{
enemydmg = 0;
cout<<"=$=$=$=$They missed you!=$=$=$=$"<<endl<<endl; //print they missed you

}

//updated player health

current_player.take_damage(enemydmg)

}

void enemy::take_damage (int player_damage);
{
health = health - damage;
}













string random_enemy_name();
//all this does is give a random chance of the person you fights name. soon will add this to

//effect stats, hps, attack types, etc. maybe then name it to enemy_stats or something


bool random_bool();
//a function that randomizes the event of an event (block of code) happening. such as drop loot,

//etc.it basically just returns true or false on a 50/50 chance. could be switch to 70/30 or different.


void intro_screen (); //a simple print screen function that prints the welcome screen


void attack_prompt(int player_stamina,int player_mana,int bullets,int enemy_health,char &attack_type,string enemy_name);
//just a print screen of the current stats for player/enemy. also prompts for next attack

//and returns attack_type




//this function calculates the enemies attack, and sends back the players updated health


void player_movement (int &spaces_moved);//this prints the movment keys and also

//starts a counter called spaces_moved

//which records how many spaces the player moved


void rand_attack (bool &battle_now); //this calculates the chances of a random

//attack. it doesnt start untill the player

//moves at least one space. it returns a

//boolean value of true if combat is started

//and false if no combat started.


void enter_combat(int &player_stamina,int &player_mana,int &bullets);
//this manages all combat. it called the player_attack and enemy_attack functions, updates

//all health,stamina, and mana. it also prints out results of each hit etc.





int main(int argc, char **argv)
{



int spaces_moved = 0;
bool battle_now;

srand (time (NULL)); //set the seed to the internal clock of the computer



intro_screen(); //simply prints the intro screen to the screen



do
{

player_movement(spaces_moved); //pop up the movement screen


rand_attack(battle_now);//check for random attack (80% chance of attack)



if (battle_now == true) //if the attack is true, then enter combat, if not, go back

//to the top, ask for movement, and look for another random attack

{
enter_combat(current_player, enemy drifter);
}

}

while (current_player.health > 0 && spaces_moved < 20); //do this all untill the player dies

//or beats the game



if (player_health < 0) //if the player died, say game over.

{

cout<<"You are dead"<<endl;

cout<<"Game Over"<<endl;
}

else if (spaces_moved == 20) //if the player reached the end, say you win

{
cout<<"**************************************"<<endl;
cout<<" YOU WIN!! You have reached the end!!!"<<endl<<endl;
cout<<"**************************************"<<endl;

}

return 0;
}

void intro_screen () //simply prints the welcome screen to the player.

{
cout<<"********************************************************************************"<<endl;
cout<<"*********************************WELCOME TO FTA*********************************"<<endl;
cout<<"********************************************************************************"<<endl;
cout<<"**************************DONKEY PUNCH PRODUCTIONS INC.*************************"<<endl;
cout<<"********************************************************************************"<<endl;
}



void player_movement (int &spaces_moved)
{

char direction;

cout<<"Press ''W'' to move up"<<endl;
cout<<"Press ''A'' to move left"<<endl;
cout<<"Press ''S'' to move right"<<endl;
cout<<"Press ''Z'' to move backwards"<<endl<<endl;
cin>>direction;

if (direction == ''w'')
{
cout<<"You moved forward."<<endl<<endl;
}
else if (direction == ''a'')
{
cout<<"You moved left."<<endl<<endl;
}
else if (direction == ''s'')
{
cout<<"You moved right."<<endl<<endl;
}
else
{
cout<<"You moved backwards."<<endl<<endl;
}

spaces_moved = spaces_moved + 1;
cout<<"You moved a total of "<<spaces_moved<<" spaces."<<endl<<endl;
}

void rand_attack (bool &battle_now)
{


int attack_chance = (rand () % 100) + 1; //this sets up a random fight wiht 15% chance of happening



if (attack_chance > 65)
{
battle_now = true;
}
else if (attack_chance <=65 )
{
battle_now = false;
}


}

void enter_combat(player ¤t_player,enemy &drifter)
{



string enemy_name = random_enemy_name();
int bullets_found = 0;
bool gun;

system("cls");

cout<<"**************************YOU HAVE ENTERED COMBAT!*****************************"<<endl<<endl;
do
//show enemy/player status

//do player attack

//do enemy attack

//while player and enemy are both still alive

{
current_player.attack_prompt(enemy &drifter, string enemy_name);

//player_attack (enemy_health,attack_type,player_stamina,player_mana,bullets);

current_player.attack(enemy &drifter);
//this calculates the players attack, and updates enemyhealth/player stamina/mana


drifter.attack(player ¤t_player);

//this calculates the enemies attack, and updates player health.

}
while ((current_player.health > 0) && (drifter.health > 0));
//runs this battle untill one of the player dies.




//this down here is clean up code after the enemy dies

//it prints if i defeated them, it resets the enemies health to 40

//and it gives a random chance for a gun to drop.

if (drifter.health < 0)
{
cout<<"You have defeated the enemy"<<endl;

drifter.health = 40;

gun = random_bool();

if (gun == true)
{
cout<<"Enemy dropped a gun"<<endl<<endl;
bullets_found = 12;
}

}


current_player.bullets = bullets_found + bullets;


}

bool random_bool()
{

int random = rand() % 100;

if (random >= 50)
{
return true;
}
else
{
return false;
}

}




string random_enemy_name()
{
string enemy_name;
int name_chance = rand() % 100;


if (name_chance < 25)
{
enemy_name = "Hit Man";
}
else if (name_chance > 25 && name_chance < 50)
{
enemy_name = "Orc Gunner";
}
else if (name_chance > 50 && name_chance < 75)
{
enemy_name = "Drifter";
}
else
{
enemy_name = "Cop";
}

return enemy_name;
}



thanks for any help

Share this post


Link to post
Share on other sites
When you say you''ve getting errors, post the errors. Most of the time we can work out what''s wrong just from the error message and the surrounding code. The length of the code you''re posting is unreasonable to read through without even knowing what error to look for. The course of action I would take is to load up the code into my development environment and try to compile it. But that all takes more time.

Post the error messages and you''ll get more answers.

Share this post


Link to post
Share on other sites