I am going to be flat out honest with you guys. My brain is totally fried and all I type now is just a bunch of words.. :/
I have a quiz due soon and I decided to go over the top for some odd reason, but I started it now and I want to finish it lol..
Long story short, the quiz's instructions says basically to create any program that uses Inheritance and Polymorphism and I decided to make a small turn based game, but that kind of turned horribly wrong..
Here's the code so you can see what I am talking about..
// Polymorphism and Inheritance
#include <iostream>
#include <string>
#include <time.h>
usingnamespace std;
class Entity
{
protected:
int health;
public:
Entity()
{
health = 100;
}
virtualvoid Attack()
{
cout << "Attack: " << endl;
}
friendbooloperator < (Entity player, Entity enemy)
{
if (player.health <= 0 || enemy.health <= 0)
returnfalse;
elseif (player.health >= 0 || enemy.health >= 0)
returntrue;
}
void SetHealth(int value)
{
health = value;
}
void DisplayHealth()
{
cout << health;
}
void Random()
{
double simNum = (static_cast<double> (rand()) / (RAND_MAX));
if(simNum < RAND_MAX) // If the random number is less than the accuracy...
{
health - 10;
cout << "Hit 10 dmg!" << endl;
}
else
{
cout << "Miss!" << endl;
}
}
};
class Player : public Entity
{
private:
int number;
string name;
bool playerAlive;
public:
void SetName(string player)
{
getline(cin, player);
name = player;
}
void DisplayName()
{
cout << name;
}
void Attack()
{
this->Random();
//cout << "Player Attack: " << atkPow << endl;
}
};
class Enemy : public Entity
{
void Damage()
{
this->Random();
cout << health << endl;
}
void Attack()
{
cout << "Player Attack: " << endl;
}
};
int main()
{
srand(time(0)); // Random seed (based on time).
Player players;
Enemy enemies;
//Entity *entity = &player;
Entity *player = new Player;
Entity *enemy = new Enemy;
string name;
char again = 'y';
cout << "Enter your player's name: ";
players.SetName(name);
cout << "Hello ";
players.DisplayName();
cout << "! " << "You start with ";
players.DisplayHealth();
cout << "HP." << endl;
// What is the player's health.
//player->SetHealth(100);
//->DisplayHealth();
// Display Enemy information
cout << "Your enemy has ";
enemies.DisplayHealth();
cout << "HP.\n" << endl;
while (again == 'y' || again == 'Y')
{
// What is the attack power.
//player->SetAtkPower(10);
player->Attack();
if (enemies < players)
{
cout << "Player wins" << endl;
}
elseif (players < enemies)
{
cout << "You lose" << endl;
}
cout << "Enter 'y' to continue, or 'n' to exit: ";
cin >> again;
}
if (again == 'n' || again == 'N')
{
cout << "\nThanks for playing." << endl;
}
else
{
cout << "Not a valid entry. Try again." << endl;
cout << "Enter 'y' to continue, or 'n' to exit: ";
cin >> again;
}
system("pause");
return 0;
}
So one of the big problems is that the rand function isn't working properly, which it should either get the player and the enemy to either hit or miss, then switches to the next player.. So like if the player decided to continue (shoot) then he will either hit or miss, if it's a hit then the other will get a 10 dmg hit and the goal is to whoever ends up dying the other person wins (whether the computer or the player).. meh...
EDIT: There's some redundant code in there because I'm trying to do different things but keep on failing lol..
EDIT2: I don't even know if I'm doing polymorphism right.. :/
Thank you for taking the time to help me out! And yes correct, the enemy should attack the player with the same setup as the player (same random), and in the end whoever dies first the other wins.. I am still trying to figure this part out..
I am open to any suggestions/help from anyone btw.. :D
EDIT: So now the HP goes down by 10 when it's the first hit, but after the first hit it stays at 90 (even if there's another hit).. Any idea why? Here's the updated code.. Shouldn't the enemy.health -= 10 for example keep on reducing 10 everytime there's a hit?
When you attack, you are passing player by value, which means the entitybeing attacked is copied into the function. The original player is left unchanged. Change the definition of Attack to
void Attack(Entity& player)
Edit: Both versions of Attack should be changed, not just the Enemy version. By the way, why didn't you just combine the Attack functions into one in the base class?
I have one tiny problem though that I need help with please. The problem is if the player chooses to not continue while both player's health and enemy's health is the same, it should say "It's a draw!"... Instead it says "You won!!!" How can I get it to say "It's a draw!"?
I probably wouldn't use the friendship in this matter, a Entity class should be created as an object - you would want the Entity class just to be derived from - more so as an interface - also you can easily make the operators templated.
Hello vas90, thank you for replying back.. But would you care to elaborate please? I don't fully understand what you mean.. Do you mean I shouldn't overload the boolean? Also, I'm still learning and at this point this is what I've learned so far from the class; like I really don't know how to do it in an other way..
The only problem that I am having right now is that it doesn't say It's a draw if the player decides to end the game and both player and enemy have the same hp.. It just says that the player won..
When you are checking '>' and '<', make sure your functions account for equal values. Right now your code just fall through and returns something that may not be correct. You could probably get away with: