我几乎是一个初学者/自学 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);
}
最佳答案
第一个指针,你的指针太多了。
因为需要在被调用方法中更改变量地址,所以在任何地方都应使用被调用方法中的引用。
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 类,因为它们是同一个问题。可能
- 为统计数据创建一个枚举以使用查找(不是枚举类,因为这在这里会很痛苦)。
- 为每个主要概念制作一个类。
关于c++ - 需要帮助改进我的 C++ 和类创建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47107182/