需要帮助改进我的 c++ 和类创建

Need help improving my c++ and class creation

本文关键字:c++ 创建 我的 帮助      更新时间:2023-10-16

我几乎是一个初学者/自学C++程序员。 我想测试一下自己的技能。 我做了这个程序,如果你的男孩/女孩能告诉我如何改进它,我会很高兴。这是我的第一堂课(请告诉我,如果你想清理一下)

 class race {public: 
enum raceOpt { orc, elf, human };
race(raceOpt c_raceOpt, int *STR, int *DEX, int *CON, int *INT, int* WIS, int* CHR);
//race opt outcomes 
void ORC(int** STR, int** INT, int** CHR) {
    **STR = **STR + 2;
    **INT = **INT - 2;
    **CHR = **CHR - 2;
}
void ELF(int** DEX) {
    //elf code and stat changes
}
//I still have to add human
};

// here's my constructor
  race::race(raceOpt c_raceOpt, int* STR, int* DEX, int* CON, int* INT, int* WIS, int* CHR) {
switch (c_raceOpt) {
case orc:
    ORC(&STR, &INT, &CHR);
    break;
case human:
    //human code
    break;
case elf:
    //elf code
    break;
}
}

这是我的下一个基类

class classes {
public:
enum classOpt { fighter, mage, soccerPlayer };
classes(classOpt c_classOpt, double ModStats[_MAX_PATH], int* Sneak, int* Bluff, int* Deplomacy, int* Swim, int* Mana, int* Run);
void Fighter(int** SWIM, int** RUN, int** BLUFF, double ModStats[_MAX_PATH]) {
    **SWIM = static_cast<int>(**SWIM + std::round(((ModStats[2] * 2.3))));
    **RUN = static_cast<int>(**RUN + (std::round(((ModStats[2] * 2)))));
    **BLUFF = static_cast<int>(**BLUFF + std::round(((ModStats[1] * 2.5))));
}
void Mage(int** MANA, int** DEPLOMACY, int** BLUFF, double ModStats[_MAX_PATH]) {
    //code to be added
}
void SoccerPlayer(int** RUN, int** SWIM, int** DEPLOMACY, int** SNEAK, double ModStats[_MAX_PATH]) {
    //code to be added
}
};
// here's my constructor
classes::classes(classOpt c_classOpt, double ModStats[_MAX_PATH], int* Sneak, int* Bluff, int* Deplomacy, int* Swim, int* Mana, int* Run) {
switch (c_classOpt) {
case fighter:
    Fighter(&Swim, &Run, &Bluff, ModStats);
    break;
case mage:
    Mage(&Mana, &Deplomacy, &Bluff, ModStats);
    break;
case soccerPlayer:
    SoccerPlayer(&Run, &Swim, &Deplomacy, &Sneak, ModStats);
    break;
}
}

这是我的最后一个英雄类构造函数和主要。

class hero {
protected:
int sneak = 0, bluff = 0, deplomacy = 0, swim = 0, mana = 0, run = 0;
int //stats
    Str = 16,
    Dex = 18,
    Con = 11,
    Int = 10,
    Wis = 8,
    Chr = 13;

double modStats[6] = {};
//vvvv gets proper stat mods based on D&D
void getAndSetMods(){
    double modStats[6] = {
        this->modStats[1] = std::round(Str / 2) - 5,
        this->modStats[2] = std::round(Dex / 2) - 5,
        this->modStats[3] = std::round(Con / 2) - 5,
        this->modStats[4] = std::round(Int / 2) - 5,
        this->modStats[5] = std::round(Wis / 2) - 5,
        this->modStats[6] = std::round(Chr / 2) - 5,
    };//stats above in order
}

public:
void raceAndClass() {
    race c_race(race::orc, &Str, &Dex, &Con, &Int, &Wis, &Chr);
    getAndSetMods();
    classes c_classes(classes::fighter, modStats, &sneak, &bluff, &deplomacy, &swim, &mana, &run);
        using 
namespace std;
        cout << Str << endl << Dex << endl << Con << endl << Int << endl << Wis << endl << Chr << endl;
        cout << endl << endl << endl;
        cout << sneak << endl << bluff << endl << deplomacy << endl << swim << endl << mana << endl << run << endl;
    }
};
     int main()
        {
                hero H;
                H.raceAndClass();
                while (true);
        }

第一个指针,你的指针太多了。

任何地方获取变量的地址,因为您需要在被调用的方法中更改它,因此您应该在被调用的方法中使用引用。

SomeCall(&someVar);
void SomeCall(int* theVar);

应该是

SomeCall(someVar);
void SomeCall(int& theVar);

创建帮助程序类统计信息

class Stats {
protected: // or private and write a bunch of access methods
int //default stats
    Str = 16,
    Dex = 18,
    Con = 11,
    Int = 10,
    Wis = 8,
    Chr = 13;
    Stats(int STR, int DEX, int CON, int INT, int WIS, int CHR) :
      Str(STR), Dex(DEX), Con(CON), Int(INT), Wis(WIS), Chr(CHR) {
    }
}

那么你的比赛类可以这样简化

class race {
public: 
    enum raceOpt { orc, elf, human };
race(raceOpt c_raceOpt, Stats& stats) {
  switch (c_raceOpt) {
    case orc:
       ORC(stats);
//race opt outcomes 
void ORC(Stats& stats) {
    stats.Str += 2;
    stats.Int -= 2; 
    stats.Chr -= 2;
}

重构可以继续

  • 使用 std::array 来保存 Stats 类中的统计信息。
  • 将 modStat 移动到 Stats 类,因为它们是相同的问题。 潜在
  • 为统计信息创建一个枚举以使用查找(不是枚举类,因为这在这里会很痛苦)。
  • 为每个主要概念创建一个类。