Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using the RPG combat code, add the following functionality: Create a class calle

ID: 3732063 • 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)

Explanation / Answer

MAIN.CPP

#include <iostream>

#include <bits/stdc++.h>

#include "character.h"

using namespace std;

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

Character player_Charact(10, 5, 3, 5);

Character np_c(10, 4, 4, 3);

Potion p(3);

while(!player_Charact.isDead() && !np_c.isDead())

{

if(player_Charact.getSpeed() > np_c.getSpeed())

{

// player character attack

npc.defend(player_Charact.attack());

if(!np_c.isDead())

player_Charact.defend(np_c.attack());

} else {

// npc attack

player_Charact.defend(np_c.attack());

if(!player_Charact.isDead())

np_c.defend(player_Charact.attack());

}

}

if(!player_Charact.isDead())

cout << "Player wins!" << endl;

else

cout << "Computer wins!" << endl;

return 0;

}

CHARACTER.H

#include <iostream>
class Character{

private:
int maxHp;
int healthPoints;
int strength;
int defense;
int speed;

public:
Character(void);
Character(int, int, int, int);
int attack(void);
void defend(int);
void heal(int);
bool isDead(void);
int getSpeed(void);
}

CHARACTER.CPP

#include <iostream>

#include <bits/stdc++.h>

#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;

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote