samedi 20 avril 2019

Utilizing an array for a class?

How can I utilize an array for a class-based code like the one I have below?

What I am trying to do is create a character-creation program that utilizes classes.

I have discovered that an array must be utilized in some form, for what I want to do. However, with what i have listed below, I am having some trouble understanding how.

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

    }

What I want to do, with the code I have below, is two create two separate characters, and as the user closes the program, both are listed along with descriptions of the characters powers/abilities.

Aucun commentaire:

Enregistrer un commentaire