Использование массива для класса? - PullRequest
0 голосов
/ 21 апреля 2019

Как я могу использовать массив для кода на основе классов, подобного тому, который у меня есть ниже?

Что я пытаюсь сделать, это создать программу для создания символов, которая использует классы.

Я обнаружил, что массив должен использоваться в той или иной форме, для того, что я хочу сделать.Однако из-за того, что я перечислил ниже, у меня возникли некоторые проблемы с пониманием того, как.

    #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?  

    }

Что я хочу сделать с помощью приведенного ниже кода, это два создать два отдельных символа, и как пользовательзакрывает программу, оба перечисляются вместе с описаниями способностей / способностей персонажей.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...