Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8415809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T01:33:16+00:00 2026-06-10T01:33:16+00:00

I’m trying to create a very simple turn-based text RPG game. The code compiles

  • 0

I’m trying to create a very simple turn-based text RPG game. The code compiles and runs but the attack and heal functions don’t seem to change the values of the variables. I’m pretty sure it’s a problem related to inheritance but I’m not sure.

#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;

struct Stats {
       int hp;
       int def;
       int atk;
       int skl;
};

class Character {
      public:
             Character();
             virtual void getHit(int) = 0;
             int attack(int);
             void defend();
             virtual int getHP() = 0;

      protected:
              Stats m_Stats;
};

Character::Character() {
   m_Stats.hp = 0;
   m_Stats.def = 0;
   m_Stats.atk = 0;
   m_Stats.skl = 0;
}

int Character::attack(int def) 
{
    return (m_Stats.atk - def);
}


class Player : public Character {
      public:
             Player();
             void getHit(int);
             void healSelf();
             void fireBall();
             int getAtk();
             int getHP();
};

Player::Player()
{
     m_Stats.hp = 100;
     m_Stats.atk = 30;
     m_Stats.def = 10;
     m_Stats.skl = 10;     
}

int Player::getHP()
{
    return m_Stats.hp;
}

int Player::getAtk()
{ return m_Stats.atk; }

void Player::getHit(int atk)
{
   m_Stats.atk += m_Stats.def - atk;     
 }

void Player::healSelf()
{
     m_Stats.hp += m_Stats.skl;
     if(m_Stats.hp > 100)
        m_Stats.hp = 100;
}


class Enemy : public Character {
      public:
             Enemy();
             void getHit(int);
             void chooseAction();
             void rage();
             int choose();
             void defend();
             void resetAtk();
             int getAtk();
             int getHP();
};

Enemy::Enemy()
{
     m_Stats.hp = 150;
     m_Stats.atk = 40;
     m_Stats.def = 5;
     m_Stats.skl = 5;
}

int Enemy::getHP()
{
    return m_Stats.hp;
}

int Enemy::getAtk()
{
     return m_Stats.atk;
}

void Enemy::resetAtk()
{
     m_Stats.atk = 40;
}

int Enemy::choose()
{
     srand(time(0));
     int c = rand() % 3;
     if(c == 0)
        cout << "Enemy chooses to attack!\n";
     else if(c == 1)
        cout << "Enemy is raging!\n";
     else if(c == 2)
        cout << "Enemy chooses to defend!\n";
     return c;
}

void Enemy::rage()
{
     m_Stats.atk += 3;
 }

void Enemy::getHit(int atk)
{
   m_Stats.atk += m_Stats.def - atk;   
   m_Stats.def = 5;  
}

void Enemy::defend()
{
     m_Stats.def += 2;
}


int main()
{
    Player p;
    Enemy e;
    bool done = false; int choice, move;
    cout << "The enemy faces you!\n";
    while(!done)
    {cout << "1) Attack  2) Heal Self. ";
     cin >> choice;
     if(choice == 1)
      {  e.getHit(p.getAtk());}
     else if(choice == 2)
        {p.healSelf(); }

     cout << "Your HP: " << p.getHP() << "\n";
     cout << "Enemy HP: " << e.getHP() << "\n";

     choice = e.choose();
     if(choice == 1)
         {p.getHit(e.getAtk());
         e.resetAtk();
         }
     else if(choice == 2)
         e.rage();
     else if(choice == 3)
         e.defend();

     cout << "Your HP: " << p.getHP() << "\n";
     cout << "Enemy HP: " << e.getHP() << "\n";


     if(p.getHP() == 0 || e.getHP() == 0)
         done = true;
     }

     if(p.getHP() > e.getHP())
         cout << "You won!" << endl;
     else
         cout << "You lost!" << endl;


    system("PAUSE");
    return 0;
}

sample output

The enemy faces you!
1) Attack  2) Heal Self. 1
Your HP: 100
Enemy HP: 150
Enemy chooses to defend!
Your HP: 100
Enemy HP: 150
1) Attack  2) Heal Self: 2
Your HP: 100
Enemy HP: 150

Yeah, the output is not pretty but I’m putting off clearing up output after the code is solid. Any help would be appreciated. Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T01:33:18+00:00Added an answer on June 10, 2026 at 1:33 am

    Why would you expect these values to change? The only place in your program where you change the value of hp field is the Player::healSelf method. Since the player’s HPs are capped at 100, and the player has 100 HP from the beginning, calling healSelf cannot rise the hp value above 100. This is why it doesn’t change.

    I would expect your getHit methods to change the hp value of player and enemy. But they don’t. See for yourself

    void Enemy::getHit(int atk)
    {
       m_Stats.atk += m_Stats.def - atk;   
       m_Stats.def = 5;  
    }
    

    There’s no mention of hp in that code. Apparenty, it is a bug. The same bug seems to be present in Player::getHit implementation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.