1

I have made a really simple game using c++. Most of it works just fine but whenever I try to replay the game, the value doesn't reset and continues where I last left off and that's is not what I want it to happens. Like lets say, in my first try, I defeated the enemy and it health is zero and when I retry it, the value doesn't starts off by 100 but it starts off from zero. And I really want a fix to that. Anyway, here is my code("It's pretty long):

#include <iostream> 
#include <string>
using namespace std;

class Health{
private:
    int health;
    Health(const Health& cpy){
        health = cpy.health;
    }
public:
     Health(){
        health = 100;
    }
    void display_health(){
        cout << "Health: " << health << endl;
    }

    Health& operator -=(int atk){
        health -= atk;
        return *this;
    }

    bool operator <(int compare){
        return (health < compare);
    }
    bool operator >(int compare){
        return !(this->operator<(compare));
    }
    bool operator &&(const int compare){
        return (health && compare);
    }
};

class Hero : public Health{
public:
    void kick(){
        cout << "Your kick damaged 45 health points" << endl;
    }
    void Punch(){
        cout << "Your punch damaged 25 health points" << endl;
    }
    void Knife(){
        cout << "Your kinfe damaged 60 health points" << endl;
    }
    void Tackle(){
        cout << "Your tackle damaged 10 health points" << endl;
    }
};

class Enemy : public Health{
public:
    void Basic_hit(){
        cout << "Enemy damaged you by 25 point" << endl;
    }
};

int main(){
    //local vaiables 
    string name;
    int choice;
    string ply_agn = "y";
    //instance
    Hero hero;
    Enemy enmy;
    //user interface
    do{ //entire game repeats
        cout << "Enter you character name: ";
        getline(cin, name);
        cout << "Your default health is : ";
        hero.display_health();
        cout << endl;
        do{
            //user interface
            cout << "0.Kick - 45" << endl;
            cout << "1.Punch - 25 " << endl;
            cout << "2.Kinfe -  60" << endl;
            cout << "3.Tackle - 10 " << endl;
            //players choice
            cout << "Which move you like to chose: ";
            cin >> choice;
            cout << endl;
            //switch function
            switch (choice)
            {
            case 0:
                hero.kick();
                cout << "Enemy ";
                enmy -= 45;
                enmy.display_health();
                break;
            case 1:
                hero.Punch();
                cout << "Enemy ";
                enmy -= 25;
                enmy.display_health();
                break;
            case 2:
                hero.Knife();
                cout << "Enemy ";
                enmy -= 60;
                enmy.display_health();
                break;
            case 3:
                hero.Tackle();
                cout << "Enemy ";
                enmy -= 10;
                enmy.display_health();
                break;
            default:
                cout << "Invalid move! Try again" << endl;
                break;
            }
            cout << endl;
            //Enemy move 
            enmy.Basic_hit();
            hero -= 25;
            cout << "The enemy hit you with a basic hit. You recieved 25 damage" << endl;
            cout << "Your ";
            hero.display_health();
            cout << endl;
        } while (enmy > 0);
        //winning condition
        if (enmy < 0 && hero > 0) {
            cout << "You won" << endl;
        }
        else if (hero < 0 && enmy > 0){
            cout << "You lose" << endl;
        }
        else if (hero < 0 && enmy < 0){
            cout << "This game is a draw" << endl;
        }
    } while (ply_agn == "y"); //This is where I think is messing up the game

        //repeating condition
        cout << "Would you like to play again: ";
        cin >> ply_agn;

    system("pause");
    return 0;
}

And thank you for your time

user2018675
  • 657
  • 2
  • 5
  • 15
Jim
  • 171
  • 8

3 Answers3

4

Put the

Hero hero;
Enemy enmy;

after the do{, like

...
string ply_agn = "y";
//user interface
do{ //entire game repeats
    //instance
    Hero hero;
    Enemy enmy;
    ...
PlasmaPower
  • 1,864
  • 15
  • 18
4

As others have stated, you need to put your Hero and Enemy objects inside your loop, so they get recreated after every loop of the game.

Another bug in your program is that you mix getline with cin, so in your next loop you won't be able to give your character a new name. To avoid this, you'll need to place a cin.ignore before your getline() function call, like this

cin.ignore (std::numeric_limits<std::streamsize>::max(), '\n'); 
getline(cin, name);

Here, std::numeric_limits is defined in the header <limits> and is used because it is the maximum size of the stream, so it will ignore as many newline characters \n were left in the buffer by cin.

As a final thought, you need to the repeat question before the loop test, like this

    cout << "Would you like to play again: ";
    cin >> ply_agn;
} while (ply_agn == "y");

otherwise ply_agn will be always y, and the user won't be able to quit your game ever :-)

Community
  • 1
  • 1
Natan Streppel
  • 5,759
  • 6
  • 35
  • 43
2

You never explicitly reset the health at the start of the game. If you're going to reset via looping like this, the first step in the game should be to reset both Hero and Enemy. Simply moving where you declare them to be the first line in your do while loop should do the trick.

aruisdante
  • 8,875
  • 2
  • 30
  • 37