为类使用数组?

时间:2019-04-21 01:39:13

标签: c++ c++11

如何使用数组来存储基于类的代码,例如下面的代码?

我要做的是创建一个利用类的字符创建程序。

我发现必须以某种形式利用数组来实现自己的目的。但是,根据我在下面列出的内容,我在理解操作方法时遇到了一些麻烦。

    #include<iostream>
    #include<string>
    #include<iomanip>
    #include<cstdlib>
    #include<ctime>
    #include<random>


    using namespace std;

    char choiceY;

    class character{

        string choice;
        string name;
        string inventory[10] = {"5 Gold", "<weapons>", "<potions>"};
        string player_class;
        int athletics;
        int hitPoints;
        int armorClass; 
        int mind;
        int sprit;
        int level;


        public:

            string getName() {

                cout<<"What Should Your Name Be? "<<endl;
                cout<<"Name: ";
                getline(cin, name);

                return name;
        }

            string classChoice() {

                cout<<"Please Select a Class: " <<endl;
                cout<<"The Cleric, a holy disciple. Select C"<<endl;
                cout<<"The Fighter, an unstoppable warrior! Select F"<<endl;
                cout<<"The Paladin, a holy warrior! Select P"<<endl;
                cout<<"The Rogue, a sneaky thief. Select R"<<endl;
                cout<<"The Wizard, a wizened magician! Select W"<<endl;
                getline(cin, choice);

                while(choice != "C" && choice != "c" && choice != "F" && choice != "f" && choice != "P" && choice != "p" 
                && choice != "R" && choice != "r" && choice != "W" && choice != "w")
                {
                cout<<"Please Enter a Given Class!"<<endl;
                getline(cin, choice);
                }

                return choice;
        }

            string playerClass() {


                if(choice == "C" || choice == "c"){
                cout<<"Class: Cleric" <<endl;
                }    
                else if(choice == "F" || choice == "f"){
                cout<<"Class: Fighter" <<endl;
                }   
                else if(choice == "P" || choice == "p"){
                cout<<"Class: Paladin" <<endl;
                }
                else if(choice == "R" || choice == "r"){
                cout<<"Class: Rogue" <<endl;    
                }
                else if(choice == "W" || choice == "w"){
                cout<<"Class: Wizard" <<endl;   
                }

                return player_class;
        }   

            void setStats(string choice) {

                if(choice == "C" || choice == "c"){

                    srand(time(0));
                    athletics = rand() % (15 - 8) + 8;
                    mind = rand() % (15 - 8) + 8;
                    sprit = rand() % (19 - 12) + 12;

                    armorClass = 10 + ((athletics - 10) / 2);
                    hitPoints = 8 + (athletics / 3);

                }

                else if(choice == "F" || choice == "f"){

                    srand(time(0));
                    athletics = rand() % (19 - 12) + 12;
                    mind = rand() % (13 - 6) + 6;
                    sprit = rand() % (15 - 12) + 12;

                    armorClass = 10 + ((athletics - 10) / 2);
                    hitPoints = 10 + (athletics / 3);

                }

                else if(choice == "P" || choice == "p"){

                    srand(time(0));
                    athletics = rand() % (15 - 12) + 12;
                    mind = rand() % (15 - 8) + 8;
                    sprit = rand() % (19 - 12) + 12;

                    armorClass = 10 + ((athletics - 10) / 2);
                    hitPoints = 10 + (athletics / 3);

                }

                else if(choice == "R" || choice == "r"){

                    srand(time(0));
                    athletics = rand() % (19 - 12) + 12;
                    mind = rand() % (15 - 8) + 8;
                    sprit = rand() % (15 - 8) + 8;

                    armorClass = 10 + ((athletics - 10) / 2);
                    hitPoints = 8 + (athletics / 3);

                }

                else if(choice == "W" || choice == "w"){

                    srand(time(0));
                    athletics = rand() % (13 - 6) + 6;
                    mind = rand() % (19 - 12) + 12;
                    sprit = rand() % (15 - 12) + 12;

                    armorClass = 10 + ((athletics - 10) / 2);
                    hitPoints = 6 + (athletics / 3);
                }
            }

            void setInventory() {

            cout<<"Your Inventory Includes: " << inventory [0] <<endl;
            cout<<"Your Inventory Includes: " << inventory [1] <<endl;
            cout<<"Your Inventory Includes: " << inventory [2] <<endl;

        }

        void displayCharacter() {

            cout<<"***********************************"<< endl;
            cout<<"Your character is: "<< endl;
            cout<<"***********************************"<< endl; 
            cout<<"Name: " << name <<endl;
            playerClass();      
            cout<<"Stats:"<<endl;
            cout<<"         Athletics: "<< athletics <<endl;
            cout<<"         Mind: "<< mind <<endl;
            cout<<"         Sprit: "<< sprit <<endl;
            cout<<"Armor Class: "<< armorClass <<endl;
            cout<<"HP: "<< hitPoints << "\tLevel: "<< level <<endl;
            setInventory();
            cout<<"Is this Okay?"<<endl;
            cin>>choiceY;
            cin.ignore();

        }

        void setLevel() {

                if(choice == "C" || choice == "c"){

                    level = 1;
                }

                else if(choice == "F" || choice == "f"){

                    level = 1;
                }

                else if(choice == "P" || choice == "p"){

                    level = 1;
                }

                else if(choice == "R" || choice == "r"){

                    level = 1;
                }

                else if(choice == "W" || choice == "w"){

                    level = 1;
                }   

        }

        void createCharacter() {

            getName();
            classChoice();
            setStats(choice);
            setLevel();
            displayCharacter();
        }

    };

    int main() {

        character creation;

     do{


        creation.createCharacter();

    }while(choiceY != 'Y' && choiceY != 'y');

    //Array Utilized in some way, instead of do-while?  

    }

在下面的代码中,我想做的是两个创建两个单独的字符,并且当用户关闭程序时,都列出了这两个字符以及对功能/能力的描述。

0 个答案:

没有答案