Как вернуть значение из класса в основную - PullRequest
0 голосов
/ 27 апреля 2020

Я пытаюсь построить простое подземелье и застреваю в битве. Получение урона работает, но я не могу вернуть новое значение здоровья, чтобы оно уменьшилось после первоначального значения. Каждый раз, когда l; oop повторяется, он возвращается к исходному значению. То же самое с сокровищами. Что дает? Как я могу вернуть значение из функции-члена в main?

#include <iostream>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
using namespace std;

class monster
{
public:
    int fight()
    {

    }
};

class chest
{
    int loot()
    {

    }
};

class movement
{
public:
    char wasd()
    {
        char mv;
        char up = 'w';
        char left = 'a';
        char right = 'd';
        char down = 's';

        cout << "\nMove using w for (up), a for (left), d for (right), and s for (down).\n\n";
        mv = _getch();

        if (mv == up)
        {
            cout << "You have moved up 1 space.\n";
        }
        else if (mv == left)
        {
            cout << "You have moved left 1 space.\n";
        }
        else if (mv == right)
        {
            cout << "You have moved right 1 space.\n";
        }
        else if (mv == down)
        {
            cout << "You have moved down 1 space.\n";
        }
        else
        {
            cout << "it didn't work.";

        }
        return 0;
    }
};

class random_enc
{   public:
    int treasure;
    int health = 12;
    public:
    int encounter(int)
    {
        int randnumber = rand() % 5 + 1;
        treasure = 0;
        if (randnumber == 1)
        {
            cout << "You have been attacked!!! You lose 1 hitpoint." << endl;
            health = --health;
            cout << "Hitpoints remaining: " << health << endl;
            return health;
        }
        else if (randnumber == 2)
        {
            cout << "You found treasure!" << endl;
            treasure = ++treasure;
            cout << "Treasure collected: " << treasure << endl;;
            return random_enc::treasure;
        }
        else if (randnumber == 3)
        {
            return health;
        }
        else if (randnumber == 4)
        {
            cout << "You step on a trap and take damage!! You lose 1 hit point.\n" << "Good bye." << endl;
            health = --health;
            cout << "Hitpoints remaining: " << health << endl;
        }
        return health;
    }
};



int main()
{
    string name;

    cout << "Welcome to the dungeon, enter your name:\n";
    cin >> name;
    cout << "\nGood luck in the dungeon " << name << "\n";
    int health = 12;

    while (health != 0)
    {
        movement mvmt;
        random_enc random;
        mvmt.wasd();

        random.encounter(health);
    }
    cout << "You have been killed. Goodbye.\n";
    return 0;
}

1 Ответ

0 голосов
/ 28 апреля 2020

Я заменил аргумент здоровья в функции столкновения в random_en c. Я заменил его указателем: void Встреча (Int & Health) Это передает ссылку, а не значение. Тогда здоровье определяется в функции-члене.

#include <iostream>
using namespace std;

void encounter(int& health)  // note the ampersand
{
    --health; // this will change health in main
}

int main()
{
    int health = 12;
    encounter(health);
    cout << health << '\n';  // prints 11
}
...