Need advice on a c++ rpg project [已关闭]

alen0pnh  于 2023-05-19  发布在  其他
关注(0)|答案(1)|浏览(93)

**关闭。**此题需要debugging details。目前不接受答复。

编辑问题以包含desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将帮助其他人回答这个问题。
20小时前关闭。
Improve this question
我目前正在学习C++,我在尝试构建的RPG游戏中不断遇到问题。我收到一条错误消息

no instance of constructor 
"gladiator:gladiator" matches the argument list 
argument types are: (char, int, std::string, int)

退一步说我很困惑
main.cpp

#include <iostream>
#include <Windows.h>
#include <string>
#include <cmath>
#include <ctime>
#include <conio.h>
#include <iomanip>
#include "player.h"
#include "gladiator.h"
#include "weapons.h"

using namespace std;

player battle(player account);
player calcEXP(player account, gladiator glad1);
player levelUp(player account);
void death();

int main()
{;
string name;
int option1;
cout << "Welcome, please enter your name\n";
cin >> name;
string weapons[4] = { "Sword", "Hammer", "Spear", "Daggers" };
player account(name, weapons[0], 1, 0);
cout << "\nWelcome " << account.getName() << " you find your self " << account.getWeapon() << "\nand you are not sure how you ended up here\n";
while (1)
{
    Sleep(500);
    cout << "write 1 to walk forward or 2 to walk left or 3 to walk right\n";
    cin >> option1;
    if (option1 >= 1 && option1 <= 3)
    {
        Sleep(50 * (option1));
        srand(time(NULL));
        if (rand() % 3 == option1 - 1) {
            account = battle(account);
        }

    }
    else {
        cout << "\n#@#Error#@# Please enter a number between 1 and 3\n\n";
        cin.clear();
        cin.ignore();
    }
}
return 0;
}

void death()
{
    cout << "Well at least you tried, good luck next time (HA!HA!HA!)";
}


player battle(player account)
{
    string option;
    string weapons[4] = { "Sword","Hammer","Spear","Daggers" };
    string gladiators[3] = { "Commodus", "Spartacus", "Augustus" };
    Sleep(20);
    srand(time(NULL));
    int ranG = (rand() % 3); //random gladiator
    int ranD = (rand() % 5) + 1; //random diff
    gladiator glad1(gladiators[account.getLevel() - 1][ranG], account.getLevel(), account.getWeapon(), ranD);
    cout << "Suddently you meet a " << glad1.getName() << ", be ready for battle" << "\n";
    Sleep(2000);
    do
    {
        cout << "\n\n\n ######################################\nHP:" << account.getHealth() << "              " << glad1.getName() << "HP:" << glad1.getHealth() << " difficulty:" << glad1.getDifficulty() << "\n";
        cout << "Write A for attack or R for retreat" << "\n";
        cin >> option;
        srand(time(NULL));
        if (option == "R" || option == "r")
        {
            if ((rand() % 2) == 1) {
                cout << "dogde sucessfull, you take 0 damage" << "\n";
                account.setHealth(account.getHealth() - 0);
            }
            else {
                cout << "dogde failed, the gladiator get a free attack and you lose 5 health\n";
                account.setHealth(account.getHealth() - 5);
                option = "A";
            }
        }
        if (option == "A" || option == "a")
        {
            int attack = rand() % (account.getDamage());
            srand(time(NULL));
            int gladattack = rand() % (glad1.getDamage());
            glad1.setHealth(glad1.getHealth() - attack);
            account.setHealth(account.getHealth() - gladattack);
            cout << "you attack the monster for " << attack << " damage\n";
            Sleep(500);
            cout << "the monster counter attacks for " << gladattack << " damage\n";
            Sleep(500);
        }
    } while (glad1.getHealth() > 0 && account.getHealth() > 0);
    cout << "\n\n\n ######################################\nHP:" << account.getHealth() << "                                         " << glad1.getName() << "HP:" << glad1.getHealth() << " difficulty:" << glad1.getDifficulty() << "\n";
    if (account.getHealth() <= 0)
    {
        death();
        exit(0);
    }
    account = calcEXP(account, glad1);
    return account;
}

// XP and level up functions //Fixt this section, include all fights
player calcEXP(player account, gladiator glad1)
{
    cout << "#########\ncalculating EXP\n#########\n";
    Sleep(500);
    account.setEXP(account.getEXP() + glad1.getEXP());
    cout << "EXP: " << account.getEXP() << "/" << account.getEXPReq() << "\n";
    if (account.getEXP() >= account.getEXPReq())
    {
        levelUp(account);
    }
    return account;
}   

player levelUp(player account)
{
    account.setLevel(account.getLevel() + 1);
    account.setEXPReq();
    account.setMaxHealth();
    account.setHealth(account.getMaxHealth());
    cout << "Level up! you are now level: " << account.getLevel() << "!\n";
    return account;
}

player.h

#pragma once
#include <string>

class player
{
public:
    player(std::string, std::string, int, int); //name, level, exp.
    void setName(std::string);
    void setWeapon(std::string);
    void setLevel(int);
   // void setEXP(double);        //fishy, no definition might delete
    void setHealth(double);
    void setMaxHealth();
    void setDamage();
    std::string getName();
    std::string getWeapon();
    int getLevel();
    double getHealth();
    double getMaxHealth();
    int getDamage();
    int getEXP();
    void setEXP(int);
    int getEXPReq();
    void setEXPReq();
private:
    std::string playerName;
    std::string playerWeapon;
    int playerLevel;
    double playerHealth;
    double playerMaxHealth;
    int playerDamage;
    int EXP;
    int EXPReq;
};

player.cpp

#include <string>
#include "player.h"

player::player(std::string name, std::string weapon, int level = 1, int EXP = 0)
{
    setName(name);
    setLevel(level);
    setEXP(EXP);
    setMaxHealth();
    setHealth(playerMaxHealth);
    setDamage();
    setEXPReq();
}

void player::setName(std::string name)
{
    playerName = name;
}
void player::setWeapon(std::string weapon)
{
    playerWeapon = weapon;
}
void player::setLevel(int level)
{
    playerLevel = 1;
}
void player::setHealth(double health)
{
    playerHealth = health;
}
void player::setMaxHealth()
{
    playerMaxHealth = (100 * getLevel());
}
void player::setDamage()
{
    playerDamage = (30 * getLevel());
}
void player::setEXP(int _EXP)
{
    EXP = _EXP;
}
void player::setEXPReq()
{
    EXPReq = 70 + ((getLevel() * getLevel()) * 35);
}

std::string player::getName()
{
    return playerName;
}
std::string player::getWeapon()
{
    return playerWeapon;
}
int player::getLevel()
{
    return playerLevel;
}
double player::getHealth()
{
    return playerHealth;
}
double player::getMaxHealth()
{
    return playerMaxHealth;
}
int player::getDamage()
{
    return playerDamage;
}
int player::getEXP()
{
    return EXP;
}
int player::getEXPReq()
{
    return EXPReq;
}

gladiator.h

#pragma once
#include <string>           //get the fights in different tabs

//First fight
class gladiator
{
public:
    gladiator(std::string, std::string, int, int); // name,lvl,difficulty
    void setName(std::string);
    void setWeapon(std::string);
    void setLevel(int);
    void setDamage();
    void setHealth(double);
    void setMaxHealth();
    void setDifficulty(int);
    std::string getName();
    std::string getWeapon();
    int getLevel();
    int getDamage();
    double getHealth();
    double getMaxHealth();
    int getDifficulty();
    int getEXP();
    void setEXP();
private:
    std::string gladName;
    std::string gladWeapon;
    int gladLevel;
    int gladDamage;
    double gladHealth;
    double gladMaxHealth;
    int gladDifficulty;
    int EXP;
};

gladiator.cpp

#include <string>
#include "gladiator.h"

gladiator::gladiator(std::string name, std::string wepaon, int level, int difficulty)
{
    setName(name);
    setLevel(level);
    setDifficulty(difficulty);
    setDamage();
    setMaxHealth();
    setHealth(gladMaxHealth);
    setEXP();
}

void gladiator::setName(std::string name)
{
    gladName = name;
}

void gladiator::setWeapon(std::string weapon)
{
    gladWeapon = weapon;
}

void gladiator::setLevel(int level)
{
    gladLevel = level;
}

void gladiator::setDifficulty(int difficulty)
{
    gladDifficulty = difficulty;
}

void gladiator::setDamage()
{
    gladDamage = (3 * (getLevel()) + ((getDifficulty() * getLevel()) / 2));
}

void gladiator::setHealth(double health)
{
    gladHealth = health;
}
void gladiator::setMaxHealth()
{
    gladMaxHealth = (15 * (getDifficulty() + getLevel()));
}

void gladiator::setEXP()
{
    EXP = (getLevel() * 35);
}

std::string gladiator::getName()
{
    return gladName;
}
std::string gladiator::getWeapon()
{
    return gladWeapon;
}
int gladiator::getLevel()
{
    return gladLevel;
}
int gladiator::getDifficulty()
{
    return gladDifficulty;
}
int gladiator::getDamage()
{
    return gladDamage;
}
double gladiator::getHealth()
{
    return gladHealth;
}
double gladiator::getMaxHealth()
{
    return gladMaxHealth;
}

int gladiator::getEXP()
{
    return EXP;
}

我得到的错误代码是在玩家战斗(玩家帐户)功能和行是在那里它说

gladiator glad1(gladiators[account.getLevel() - 1][ranG], account.getLevel()

我一直在努力让这个工作了一段时间,我甚至使用了一些代码从网络上,仍然什么也没有。

8cdiaqws

8cdiaqws1#

你的“角斗士”需要4个变量:2个字符串和2个整数。但是,您正在将“角斗士武器”字符串传递给“帐户级别”整数,反之亦然。
更新后的代码将像这样工作:
gladiator glad1(gladiators[account.getLevel()][ranG], account.getWeapon(), account.getLevel(), ranD);

相关问题