Nov 30, 2013 at 9:37am Nov 30, 2013 at 9:37am UTC
Hi everyone,
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..
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
// Polymorphism and Inheritance
#include <iostream>
#include <string>
#include <time.h>
using namespace std;
class Entity
{
protected :
int health;
public :
Entity()
{
health = 100;
}
virtual void Attack()
{
cout << "Attack: " << endl;
}
friend bool operator < (Entity player, Entity enemy)
{
if (player.health <= 0 || enemy.health <= 0)
return false ;
else if (player.health >= 0 || enemy.health >= 0)
return true ;
}
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;
}
else if (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.. :/
Last edited on Nov 30, 2013 at 8:48pm Nov 30, 2013 at 8:48pm UTC
Nov 30, 2013 at 11:12am Nov 30, 2013 at 11:12am UTC
Ok so changed the code a bit, got the rand to work finally.. But I am having lots of issues, I can't even run the program.. D:
Here's the new code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
// Polymorphism and Inheritance
#include <iostream>
#include <string>
#include <time.h>
using namespace std;
class Entity
{
public :
int health;
Entity()
{
health = 100;
}
//virtual void Attack()
//{
// cout << "Attack: " << endl;
//}
friend bool operator < (Entity player, Entity enemy)
{
if (player.health <= 0 || enemy.health <= 0)
return false ;
else if (player.health >= 0 || enemy.health >= 0)
return true ;
}
void SetHealth(int value)
{
health = value;
}
void DisplayHealth()
{
cout << health;
}
double Random()
{
//double randNum = (static_cast<double> (rand()%100 + 1) / (RAND_MAX));
double randNum = rand()%100 + 1;
return randNum;
}
};
class Player : public Entity
{
private :
string name;
bool playerAlive;
public :
void SetName(string player)
{
getline(cin, player);
name = player;
}
void DisplayName()
{
cout << name;
}
void Attack(Enemy enemy)
{
double randNum = this ->Random();
//cout << "Player Attack: " << atkPow << endl;
if (randNum < 50) // If the random number is less than the accuracy...
{
enemy.health -= 10;
cout << "Hit 10 dmg!" << endl;
}
else
{
cout << "Miss!" << endl;
}
}
};
class Enemy : public Entity
{
public :
void Damage(Player player)
{
double randNum = this ->Random();
cout << health << endl;
if (simNum < 50) // If the random number is less than the accuracy...
{
player.health -= 10;
cout << "Hit 10 dmg!" << endl;
}
else
{
cout << "Miss!" << endl;
}
}
void Attack()
{
this ->Random();
}
};
int main()
{
srand(time(0)); // Random seed (based on time).
Player player;
Enemy enemy;
//Entity *entity = &player;
//Entity *player = new Player;
//Entity *enemy = new Enemy;
string name;
char again = 'y' ;
cout << "Enter your player's name: " ;
player.SetName(name);
cout << "Hello " ;
player.DisplayName();
cout << "! " << "You start with " ;
player.DisplayHealth();
cout << "HP." << endl;
// What is the player's health.
//player->SetHealth(100);
//->DisplayHealth();
// Display Enemy information
cout << "Your enemy has " ;
enemy.DisplayHealth();
cout << "HP.\n" << endl;
while (again == 'y' || again == 'Y' )
{
// What is the attack power.
//player->SetAtkPower(10);
player.Attack(enemy);
if (enemy < player)
{
cout << "Player wins" << endl;
}
else if (player < enemy)
{
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;
}
Last edited on Nov 30, 2013 at 11:20am Nov 30, 2013 at 11:20am UTC
Dec 2, 2013 at 8:16pm Dec 2, 2013 at 8:16pm UTC
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?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
// Polymorphism and Inheritance
#include <iostream>
#include <string>
#include <time.h>
using namespace std;
class Entity
{
public :
int health;
Entity()
{
health = 100;
}
friend bool operator < (Entity player, Entity enemy)
{
if (player.health <= 0 || enemy.health <= 0)
return false ;
else if (player.health >= 0 || enemy.health >= 0)
return true ;
}
void SetHealth(int value)
{
health = value;
}
void DisplayHealth()
{
cout << health;
}
double Random()
{
double randNum = rand()%100 + 1;
return randNum;
}
};
class Player : public Entity
{
private :
string name;
public :
void SetName(string player)
{
getline(cin, player);
name = player;
}
void DisplayName()
{
cout << name;
}
void Attack(Entity enemy)
{
double randNum = this ->Random();
if (randNum < 50) // If the random number is less than the accuracy...
{
enemy.health -= 10;
cout << "You hit the enemy with 10 dmg!" << endl;
// What is the enemy's HP after the player attacks?
cout << "Enemy's HP is now: " ;
enemy.DisplayHealth();
cout << "HP.\n" << endl;
}
else
{
cout << "You missed!" << endl;
}
}
};
class Enemy : public Entity
{
public :
//void Damage(Entity player)
//{
// double randNum = this->Random();
// cout << health << endl;
// if(randNum < 50) // If the random number is less than the accuracy...
// {
// player.health -= 10;
// cout << "You hit the enemy with 10 dmg!" << endl;
// }
// else
// {
// cout << "You missed!\n" << endl;
// }
//}
void Attack(Entity player)
{
double randNum = this ->Random();
if (randNum < 50) // If the random number is less than the accuracy...
{
player.health -= 10;
cout << "The enemy hit you with 10 dmg!" << endl;
// What is the player's HP after the enemy attacks?
cout << "Your HP is now: " ;
player.DisplayHealth();
cout << "HP.\n" << endl;
}
else
{
cout << "The enemy has missed!\n" << endl;
}
}
};
int main()
{
srand(time(0)); // Random seed (based on time).
Player player;
Enemy enemy;
string name;
char again = 'y' ;
cout << "Enter your player's name: " ;
player.SetName(name);
cout << "Hello " ;
player.DisplayName();
cout << "! " << "You start with " ;
player.DisplayHealth();
cout << "HP." << endl;
// Display Enemy information
cout << "Your enemy has " ;
enemy.DisplayHealth();
cout << "HP.\n" << endl;
while (again == 'y' || again == 'Y' )
{
// Player attack.
player.Attack(enemy);
// Enemy attack.
cout << "Enemy's turn: " ;
enemy.Attack(player);
if (enemy < player)
{
cout << "Player wins" << endl;
}
else if (player < enemy)
{
cout << "You lose" << endl;
}
else
{
cout << "It's a draw!" << endl;
}
cout << "Enter 'y' to continue, or 'n' to exit: " ;
cin >> again;
again |= 32;
while (again != 'n' && again != 'y' )
{
cout << "Not a valid entry. Try again." << endl;
cout << "Enter 'y' to continue, or 'n' to exit: " ;
cin >> again;
}
}
cout << "\nThanks for playing." << endl;
system("pause" );
return 0;
}
Last edited on Dec 2, 2013 at 8:51pm Dec 2, 2013 at 8:51pm UTC
Dec 2, 2013 at 10:18pm Dec 2, 2013 at 10:18pm UTC
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?
Last edited on Dec 2, 2013 at 10:21pm Dec 2, 2013 at 10:21pm UTC
Dec 2, 2013 at 11:04pm Dec 2, 2013 at 11:04pm UTC
Thanks doug! I honestly didn't know how to do that so I just did it this way.. :S
Dec 3, 2013 at 2:11am Dec 3, 2013 at 2:11am UTC
Woohoo! I got the program to work!
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!"?
Here's my updated code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
// Polymorphism and Inheritance
#include <iostream>
#include <string>
#include <time.h>
using namespace std;
class Entity
{
protected :
string name;
public :
int health;
Entity()
{
health = 100;
}
friend bool operator == (Entity player, Entity enemy)
{
if (player.health == enemy.health)
return true ;
//else
//return false;
}
friend bool operator == (Entity obj, int x) // Polymorphism (I think)
{
if (obj.health == x)
return true ;
else
return false ;
}
friend bool operator > (Entity player, Entity enemy)
{
if (player.health > enemy.health)
return true ;
else if (player.health < enemy.health)
return false ;
}
friend bool operator < (Entity player, Entity enemy)
{
if (player.health > enemy.health)
return false ;
else if (player.health < enemy.health)
return true ;
}
void SetHealth(int value)
{
health = value;
}
void DisplayHealth()
{
cout << health;
}
double Random()
{
double randNum = (rand() % 100 + 1);
return randNum;
}
};
class Player : public Entity
{
public :
void SetName(string player)
{
getline(cin, player);
name = player;
}
void DisplayName()
{
cout << name;
}
void Attack(Entity& enemy)
{
double randNum = this ->Random();
if (randNum < 50) // If the random number is less than the accuracy...
{
enemy.health -= 10;
cout << "You hit the enemy with 10 dmg!\n" << endl;
// What is the enemy's HP after the player attacks?
cout << "Enemy's HP is now: " ;
enemy.DisplayHealth();
cout << " HP.\n" << endl;
}
else
{
cout << "You missed!\n" << endl;
}
}
};
class Enemy : public Entity
{
public :
void Attack(Entity& player)
{
double randNum = this ->Random();
if (randNum < 50) // If the random number is less than the accuracy...
{
player.health -= 10;
cout << "The enemy hit you with 10 dmg!" << endl;
// What is the player's HP after the enemy attacks?
cout << "Your HP is now: " ;
player.DisplayHealth();
cout << " HP.\n" << endl;
}
else
{
cout << "The enemy has missed!\n" << endl;
}
}
};
int main()
{
srand(time(0)); // Random seed (based on time).
Player player;
Enemy enemy;
string name;
char again = 'y' ;
cout << "Enter your player's name: " ;
player.SetName(name);
cout << "Hello " ;
player.DisplayName();
cout << "! " << "You start with " ;
player.DisplayHealth();
cout << " HP." << endl;
// Display Enemy information
cout << "Your enemy has " ;
enemy.DisplayHealth();
cout << " HP.\n" << endl;
while (again == 'y' || again == 'Y' )
{
// Player attack.
player.Attack(enemy);
// Enemy attack.
cout << "Enemy's turn: " ;
enemy.Attack(player);
if (enemy == 0 || player == 0)
break ;
cout << "Enter 'y' to continue shooting, or 'n' to exit: " ;
cin >> again;
again |= 32;
while (again != 'n' && again != 'y' )
{
cout << "Not a valid entry. Try again." << endl;
cout << "Enter 'y' to continue shooting, or 'n' to exit: " ;
cin >> again;
}
}
cout << "\nResults:" << endl;
player.DisplayName();
cout << " \b has: " ;
player.DisplayHealth();
cout << " HP." << endl;
cout << "The enemy has: " ;
enemy.DisplayHealth();
cout << " HP." << endl;
if (player > enemy)
{
cout << "\nYou win!!!" << endl;
}
else if (player < enemy)
{
cout << "\nYou lose!" << endl;
}
else if (player == enemy)
{
cout << "\nIt's a draw!" << endl;
}
cout << "\nThanks for playing." << endl;
system("pause" );
return 0;
}
Last edited on Dec 3, 2013 at 3:09am Dec 3, 2013 at 3:09am UTC
Dec 3, 2013 at 4:14am Dec 3, 2013 at 4:14am UTC
Hi bandora,
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.
Dec 3, 2013 at 4:35am Dec 3, 2013 at 4:35am UTC
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..
Dec 3, 2013 at 4:23pm Dec 3, 2013 at 4:23pm UTC
Your overloaded operators should look like this:
bool operator ==(const Entity& other) { return (health == other.health); }
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:
bool operator <(const Entity& other) { return (health < other.health);
Last edited on Dec 3, 2013 at 4:24pm Dec 3, 2013 at 4:24pm UTC
Dec 4, 2013 at 7:50pm Dec 4, 2013 at 7:50pm UTC
Awesome!! I've changed those two and that works perfectly!! Thank you Doug for your help, I really appreciate it! :)