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

时间:2017-11-04 03:49:21

标签: c++

我几乎是一名初学/自学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;
}
}

这是我最后的英雄类构造函数和main。

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);
        }

1 个答案:

答案 0 :(得分:3)

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

你到处都取一个变量的地址,因为你需要在被调用的方法中改变它,你应该在被调用的方法中使用一个引用。

SomeCall(&someVar);

void SomeCall(int* theVar);

应该是

SomeCall(someVar);

void SomeCall(int& theVar);

制作帮助类Stats

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类,因为它们是同一个问题。潜在地
  • 为统计数据创建枚举以使用查找(不是枚举类,因为这会很痛苦。)
  • 为每个主要概念上课。