Using the RPG combat code, add the following functionality: Create a class calle
ID: 3732259 • Letter: U
Question
Using the RPG combat code, add the following functionality:Create a class called "Potion" Potions are used to heal a character The potion should have 2 private members: count and healingAmount count holds the number of potions the player has available healingAmount holds the amount of health recovered by the character The potion constructor should allow the programmer to specify how many potions are available The class should have a member function called "use" the use function should be passed a character pointer or reference for the character being healed the function should invoke the heal member function of the character using the potion when the use function is called, the potion count should drop by 1 if the count value is 0 or lower, no healing should be performed The class should have an accesser function called getCount() which returns the number of potions available The combat code should now prompt the user whether they want to attack the creature or use a potion if the user selects to use a potion, the npc/enemy character should still attack them if the playerCharacter is slower than the npc/enemy character, they should be attacked before they get the chance to heal you may collect user input to select attack or use potion however you want (collect a 1 or 2 from the user, etc.) Add a name string member to the Character class Add a string called "name" to the character class Create an accesser function called getName() to the Character class Allow the name to be set in the constructor for Character (BONUS!) Add some flavor text to describe the premise for how the encounter began (some short story as to how the player ended up fighting the enemy character)
1-#include "character.h"
using namespace std;
// Constructors Character::Character(){ healthPoints = 0; strength = 0; defense = 0; speed = 0;
maxHp = 0; }
Character::Character(int hp, int str, int def, int spd){ if(hp < 0) healthPoints = 0; else healthPoints = hp;
if(str < 0) strength = 0; else strength = str;
if(def < 0) defense = 0; else defense = def;
if(spd < 0) speed = 0; else speed = spd;
maxHp = healthPoints; }
// Action Member Functions int Character::attack(Character & c){ c.defend(strength); }
void Character::defend(int damage){ int damageReceived = damage - defense; if(damageReceived < 0) damageReceived = 0; healthPoints = healthPoints - damageReceived; if(healthPoints < 0) healthPoints = 0;
}
void Character::heal(int healing){ healthPoints += healing; if(healthPoints > maxHp) healPoints = maxHp; }
// Status Member Functions: bool Character::isDead(void){ if(healthPoints <= 0) return true; else return false; }
int Character::getSpeed(){ return speed; }
}
2-#include <iostream>
class Character{ // Private data members (these are only accessible from inside the class) private: int maxHp; int healthPoints; int strength; int defense; int speed; // Public data members and member functions (these are accessible anywhere) public: Character(void); Character(int, int, int, int); int attack(void); void defend(int); void heal(int); bool isDead(void); int getSpeed(void); };
3-#include <iostream> #include "character.h"
using namespace std;
int main(int argc, char * argv[]){ Character playerCharacter(10, 5, 3, 5); Character npc(10, 4, 4, 3); Potion p(3);
while(!playerCharacter.isDead() && !npc.isDead()){ if(playerCharacter.getSpeed() > npc.getSpeed()){ // player character attack npc.defend(playerCharacter.attack()); if(!npc.isDead()) playerCharacter.defend(npc.attack()); } else { // npc attack playerCharacter.defend(npc.attack()); if(!playerCharacter.isDead()) npc.defend(playerCharacter.attack()); }
}
if(!playerCharacter.isDead()) cout << "Player wins!" << endl; else cout << "Computer wins!" << endl;
return 0; }
Using the RPG combat code, add the following functionality:
Create a class called "Potion" Potions are used to heal a character The potion should have 2 private members: count and healingAmount count holds the number of potions the player has available healingAmount holds the amount of health recovered by the character The potion constructor should allow the programmer to specify how many potions are available The class should have a member function called "use" the use function should be passed a character pointer or reference for the character being healed the function should invoke the heal member function of the character using the potion when the use function is called, the potion count should drop by 1 if the count value is 0 or lower, no healing should be performed The class should have an accesser function called getCount() which returns the number of potions available The combat code should now prompt the user whether they want to attack the creature or use a potion if the user selects to use a potion, the npc/enemy character should still attack them if the playerCharacter is slower than the npc/enemy character, they should be attacked before they get the chance to heal you may collect user input to select attack or use potion however you want (collect a 1 or 2 from the user, etc.) Add a name string member to the Character class Add a string called "name" to the character class Create an accesser function called getName() to the Character class Allow the name to be set in the constructor for Character (BONUS!) Add some flavor text to describe the premise for how the encounter began (some short story as to how the player ended up fighting the enemy character)
1-#include "character.h"
using namespace std;
// Constructors Character::Character(){ healthPoints = 0; strength = 0; defense = 0; speed = 0;
maxHp = 0; }
Character::Character(int hp, int str, int def, int spd){ if(hp < 0) healthPoints = 0; else healthPoints = hp;
if(str < 0) strength = 0; else strength = str;
if(def < 0) defense = 0; else defense = def;
if(spd < 0) speed = 0; else speed = spd;
maxHp = healthPoints; }
// Action Member Functions int Character::attack(Character & c){ c.defend(strength); }
void Character::defend(int damage){ int damageReceived = damage - defense; if(damageReceived < 0) damageReceived = 0; healthPoints = healthPoints - damageReceived; if(healthPoints < 0) healthPoints = 0;
}
void Character::heal(int healing){ healthPoints += healing; if(healthPoints > maxHp) healPoints = maxHp; }
// Status Member Functions: bool Character::isDead(void){ if(healthPoints <= 0) return true; else return false; }
int Character::getSpeed(){ return speed; }
}
2-#include <iostream>
class Character{ // Private data members (these are only accessible from inside the class) private: int maxHp; int healthPoints; int strength; int defense; int speed; // Public data members and member functions (these are accessible anywhere) public: Character(void); Character(int, int, int, int); int attack(void); void defend(int); void heal(int); bool isDead(void); int getSpeed(void); };
3-#include <iostream> #include "character.h"
using namespace std;
int main(int argc, char * argv[]){ Character playerCharacter(10, 5, 3, 5); Character npc(10, 4, 4, 3); Potion p(3);
while(!playerCharacter.isDead() && !npc.isDead()){ if(playerCharacter.getSpeed() > npc.getSpeed()){ // player character attack npc.defend(playerCharacter.attack()); if(!npc.isDead()) playerCharacter.defend(npc.attack()); } else { // npc attack playerCharacter.defend(npc.attack()); if(!playerCharacter.isDead()) npc.defend(playerCharacter.attack()); }
}
if(!playerCharacter.isDead()) cout << "Player wins!" << endl; else cout << "Computer wins!" << endl;
return 0; }
Using the RPG combat code, add the following functionality:
Create a class called "Potion" Potions are used to heal a character The potion should have 2 private members: count and healingAmount count holds the number of potions the player has available healingAmount holds the amount of health recovered by the character The potion constructor should allow the programmer to specify how many potions are available The class should have a member function called "use" the use function should be passed a character pointer or reference for the character being healed the function should invoke the heal member function of the character using the potion when the use function is called, the potion count should drop by 1 if the count value is 0 or lower, no healing should be performed The class should have an accesser function called getCount() which returns the number of potions available The combat code should now prompt the user whether they want to attack the creature or use a potion if the user selects to use a potion, the npc/enemy character should still attack them if the playerCharacter is slower than the npc/enemy character, they should be attacked before they get the chance to heal you may collect user input to select attack or use potion however you want (collect a 1 or 2 from the user, etc.) Add a name string member to the Character class Add a string called "name" to the character class Create an accesser function called getName() to the Character class Allow the name to be set in the constructor for Character (BONUS!) Add some flavor text to describe the premise for how the encounter began (some short story as to how the player ended up fighting the enemy character)
1-#include "character.h"
using namespace std;
// Constructors Character::Character(){ healthPoints = 0; strength = 0; defense = 0; speed = 0;
maxHp = 0; }
Character::Character(int hp, int str, int def, int spd){ if(hp < 0) healthPoints = 0; else healthPoints = hp;
if(str < 0) strength = 0; else strength = str;
if(def < 0) defense = 0; else defense = def;
if(spd < 0) speed = 0; else speed = spd;
maxHp = healthPoints; }
// Action Member Functions int Character::attack(Character & c){ c.defend(strength); }
void Character::defend(int damage){ int damageReceived = damage - defense; if(damageReceived < 0) damageReceived = 0; healthPoints = healthPoints - damageReceived; if(healthPoints < 0) healthPoints = 0;
}
void Character::heal(int healing){ healthPoints += healing; if(healthPoints > maxHp) healPoints = maxHp; }
// Status Member Functions: bool Character::isDead(void){ if(healthPoints <= 0) return true; else return false; }
int Character::getSpeed(){ return speed; }
}
2-#include <iostream>
class Character{ // Private data members (these are only accessible from inside the class) private: int maxHp; int healthPoints; int strength; int defense; int speed; // Public data members and member functions (these are accessible anywhere) public: Character(void); Character(int, int, int, int); int attack(void); void defend(int); void heal(int); bool isDead(void); int getSpeed(void); };
3-#include <iostream> #include "character.h"
using namespace std;
int main(int argc, char * argv[]){ Character playerCharacter(10, 5, 3, 5); Character npc(10, 4, 4, 3); Potion p(3);
while(!playerCharacter.isDead() && !npc.isDead()){ if(playerCharacter.getSpeed() > npc.getSpeed()){ // player character attack npc.defend(playerCharacter.attack()); if(!npc.isDead()) playerCharacter.defend(npc.attack()); } else { // npc attack playerCharacter.defend(npc.attack()); if(!playerCharacter.isDead()) npc.defend(playerCharacter.attack()); }
}
if(!playerCharacter.isDead()) cout << "Player wins!" << endl; else cout << "Computer wins!" << endl;
return 0; }
Using the RPG combat code, add the following functionality:
Create a class called "Potion" Potions are used to heal a character The potion should have 2 private members: count and healingAmount count holds the number of potions the player has available healingAmount holds the amount of health recovered by the character The potion constructor should allow the programmer to specify how many potions are available The class should have a member function called "use" the use function should be passed a character pointer or reference for the character being healed the function should invoke the heal member function of the character using the potion when the use function is called, the potion count should drop by 1 if the count value is 0 or lower, no healing should be performed The class should have an accesser function called getCount() which returns the number of potions available The combat code should now prompt the user whether they want to attack the creature or use a potion if the user selects to use a potion, the npc/enemy character should still attack them if the playerCharacter is slower than the npc/enemy character, they should be attacked before they get the chance to heal you may collect user input to select attack or use potion however you want (collect a 1 or 2 from the user, etc.) Add a name string member to the Character class Add a string called "name" to the character class Create an accesser function called getName() to the Character class Allow the name to be set in the constructor for Character (BONUS!) Add some flavor text to describe the premise for how the encounter began (some short story as to how the player ended up fighting the enemy character)
1-#include "character.h"
using namespace std;
// Constructors Character::Character(){ healthPoints = 0; strength = 0; defense = 0; speed = 0;
maxHp = 0; }
Character::Character(int hp, int str, int def, int spd){ if(hp < 0) healthPoints = 0; else healthPoints = hp;
if(str < 0) strength = 0; else strength = str;
if(def < 0) defense = 0; else defense = def;
if(spd < 0) speed = 0; else speed = spd;
maxHp = healthPoints; }
// Action Member Functions int Character::attack(Character & c){ c.defend(strength); }
void Character::defend(int damage){ int damageReceived = damage - defense; if(damageReceived < 0) damageReceived = 0; healthPoints = healthPoints - damageReceived; if(healthPoints < 0) healthPoints = 0;
}
void Character::heal(int healing){ healthPoints += healing; if(healthPoints > maxHp) healPoints = maxHp; }
// Status Member Functions: bool Character::isDead(void){ if(healthPoints <= 0) return true; else return false; }
int Character::getSpeed(){ return speed; }
}
2-#include <iostream>
class Character{ // Private data members (these are only accessible from inside the class) private: int maxHp; int healthPoints; int strength; int defense; int speed; // Public data members and member functions (these are accessible anywhere) public: Character(void); Character(int, int, int, int); int attack(void); void defend(int); void heal(int); bool isDead(void); int getSpeed(void); };
3-#include <iostream> #include "character.h"
using namespace std;
int main(int argc, char * argv[]){ Character playerCharacter(10, 5, 3, 5); Character npc(10, 4, 4, 3); Potion p(3);
while(!playerCharacter.isDead() && !npc.isDead()){ if(playerCharacter.getSpeed() > npc.getSpeed()){ // player character attack npc.defend(playerCharacter.attack()); if(!npc.isDead()) playerCharacter.defend(npc.attack()); } else { // npc attack playerCharacter.defend(npc.attack()); if(!playerCharacter.isDead()) npc.defend(playerCharacter.attack()); }
}
if(!playerCharacter.isDead()) cout << "Player wins!" << endl; else cout << "Computer wins!" << endl;
return 0; }
Explanation / Answer
ANS:-
Given that,
Using the RPG combat code, add the following functionality:
Create a class called "Potion"
Potions are used to heal a character
The potion should have 2 private members: count and healingAmount
count holds the number of potions the player has available
healingAmount holds the amount of health recovered by the character
The potion constructor should allow the programmer to specify how many potions are available
The class should have a member function called "use"
the use function should be passed a character pointer or reference for the character being healed
the function should invoke the heal member function of the character using the potion
when the use function is called, the potion count should drop by 1
if the count value is 0 or lower, no healing should be performed
The class should have an accesser function called getCount() which returns the number of potions available
The combat code should now prompt the user whether they want to attack the creature or use a potion
if the user selects to use a potion, the npc/enemy character should still attack them
if the playerCharacter is slower than the npc/enemy character, they should be attacked before they get the chance to heal.
PROGRAM:-
#include<iostream>
using namespace std;
class Character{
string name;
//other data member same
public:
void heal(int healAmount){
//same
}
Character(){
//same
name = "";
}
Character(/*same....*/string name){
//same
this->name = name;
}
//same function which were there earlier
void setName(string name){
this->name = name;
}
string getName()const{
return name;
}
};
class Potion{
int count;
int healAmount;
public:
Potion(){
//give default value here for count and healAmount
count = 10;
healAmount = 100;
}
Potion(int count,int healAmount){
this->count = count;
this->healAmount = healAmount;
}
void use(Character &ch){
ch.heal(healAmount);
count--;
}
int getCount()const{
return count;
}
};
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.