2016-08-23 51 views
-1

我想只能打电话给一个地区并有权选择TAvark(镇Avark)和森林,因此我可以一边查看用户在cin中输入正确的文本并仍然允许它回到前一点,例如从森林回到城镇,但是因为目前你不能这样做。如何组织班级,这样我可以打电话给一个班级同时使用这两个对象

namespace areas { 

class TAvark { 
public: 
    string Avark; 
    int Blacksmith; 
    int Merchant; 
    int Magic; 
    int NPC_quest; 

}; 
class Forest{ 
public: 
    string RForest; 
    string Search; 
    string Return; 
    int heal; 
    using namespace areas; 
    TAvark TAvarkregion; 
    Forest Forestregion; 
    TAvarkregion.Avark; 
    Forestregion.RForest; 
      } 
    } 
    while (TAvarkregion.Avark != "Town" & TAvarkregion.Avark != "town" & Forestregion.RForest != "Forest" & Forestregion.RForest != "forest") 
     if (Charactergender.gender == "Male" & Charactergender.gender == "male") 
      cout << "Where would you like to go Sir " << Charactername.name << endl << endl; 
     else cout << "Where would you like to go Madam" << Charactername.name << endl << endl; 
     cout << "the Forest or the Town"; 
     cin << 


    Sleep(2000); 
}; 

这里是我的所有代码,如果需要

#include <iostream> 
#include <string> 
#include <windows.h> 
#include <conio.h> 
#include <stdlib.h> 

using namespace std; 

class mainhand { 
    int serratedblade; 
    int steelsword; 
    int cutless; 
    int falchion; 
    int staff; 
    int iron_dagger; 
    int enchanted_oak_staff; 
    int wand; 
    int enchanted_steel_dagger 
}; 

class offhand { 
    int wooden_shield; 
    int off_wand; 
    int off_steel_dagger; 
    int off_iron_dagger; 
    int tower_shield; 
}; 

class chest { 
    int leather_chest; 
    int iron_chest; 
    int robes; 
    int tunic; 
    int plated_mail; 
    int woven_mantle; 

}; 


class helmet { 
    int horned_helmet; 
    int wizard_hat; 
    int iron_helmet; 
    int full_helm; 
    int hood; 
}; 

namespace areas { 

    class TAvark { 
    public: 
     string Avark; 
     int Blacksmith; 
     int Merchant; 
     int Magic; 
     int NPC_quest; 

    }; 
    class Forest{ 
    public: 
     string RForest; 
     string Search; 
     string Return; 
     int heal; 

     class Goblin { 
      int health = rand() % 50 + 20; 
      int damage = rand() % 15 + 8; 
      int armor = rand() % 6 + 4; 
      int goldR = rand() % 30 + 5; 
      int experience = rand() % 60 + 40; 
      string serratedblade; 
     }; 

     class spider { 
      int health = rand() % 50 + 20; 
      int damage = rand() % 15 + 8; 
      int armor = rand() % 6 + 4; 
      int goldR = rand() % 30 + 5; 
      int experience = rand() % 60 + 40; 
     }; 

     class Haunted_Deer { 
      int health = rand() % 50 + 20; 
      int damage = rand() % 15 + 8; 
      int armor = rand() % 6 + 4; 
      int goldR = rand() % 30 + 5; 
      int experience = rand() % 60 + 40; 
     }; 

     class Witch { 
      int health = rand() % 50 + 20; 
      int damage = rand() % 15 + 8; 
      int armor = rand() % 6 + 4; 
      int goldR = rand() % 30 + 5; 
      int experience = rand() % 60 + 40; 
     }; 

     class ghost { 
      int health = rand() % 50 + 20; 
      int damage = rand() % 15 + 8; 
      int armor = rand() % 6 + 4; 
      int goldR = rand() % 30 + 5; 
      int experience = rand() % 60 + 40; 
     }; 

     class bear { 
      int health = rand() % 50 + 20; 
      int damage = rand() % 15 + 8; 
      int armor = rand() % 6 + 4; 
      int goldR = rand() % 30 + 5; 
      int experience = rand() % 60 + 40; 
     }; 
     class boss { 
      int health = rand() % 50 + 20; 
      int damage = rand() % 15 + 8; 
      int armor = rand() % 6 + 4; 
      int goldR = rand() % 30 + 5; 
      int experience = rand() % 60 + 40; 
     }; 
    }; 


}; 


class Character { 
public: 
    string name; 
    string gender; 
    string type; 
    string faction; 
    int strength; 
    int agility; 
    int intelligence; 
    int level; 
    int health; 
    int experience; 
    int mainweapon; 
    int offhand; 
    int chest; 
    int helmet; 
    int gold; 

}; 


int main() 
{ 
    Character Charactername; 
    Character Charactergender; 
    Character Charactertype; 
    Character Characteragility; 
    Character Characterstrength; 
    Character Characterintelligence; 
    Character Characterfaction; 
    string done; 

    while (done != "Y" & done != "y") { 
     cout << "what is your name?\n"; 
     cin >> Charactername.name; 
     while (Charactergender.gender != "Male" & Charactergender.gender != "Female" & Charactergender.gender != "male" & Charactergender.gender != "female") { 
      cout << "Male or Female\n"; 
      cin >> Charactergender.gender; 
      if (Charactergender.gender != "Male" & Charactergender.gender != "Female" & Charactergender.gender != "male" & Charactergender.gender != "female") 
       cout << "you have entered an invalid option\n"; 
     } 

     while (Charactertype.type != "Warrior" & Charactertype.type != "warrior" & Charactertype.type != "Mage" & Charactertype.type != "mage" & Charactertype.type != "Rogue" & Charactertype.type != "rogue") { 
      cout << "What class are you, Warrior, Mage or Rogue\n"; 
      cin >> Charactertype.type; 

      if (Charactertype.type == "Warrior" || Charactertype.type == "warrior") 
       Characteragility.agility = 6, Characterstrength.strength = 10, Characterintelligence.intelligence = 4; 
      else if (Charactertype.type == "Mage" || Charactertype.type == "mage") 
       Characteragility.agility = 4, Characterstrength.strength = 6, Characterintelligence.intelligence = 10; 
      else if (Charactertype.type == "Rogue" || Charactertype.type == "rogue") 
       Characteragility.agility = 10, Characterstrength.strength = 4, Characterintelligence.intelligence = 6; 
      else { 
       cout << "you have entered an invalid name\n"; 
      } 
     } 
     while (Characterfaction.faction != "Good" & Characterfaction.faction != "good" & Characterfaction.faction != "Bad" & Characterfaction.faction != "bad" & Characterfaction.faction != "Neutral" & Characterfaction.faction != "neutral") { 
      cout << "what faction are you in\n" << "Good\n" << "Bad\n" << "Neutral\n"; 
      cin >> Characterfaction.faction; 
     } 
     cout << "your name is " << Charactername.name << endl << " your gender is " << Charactergender.gender << endl << " your class is " << Charactertype.type << endl << " and your faction is " << Characterfaction.faction << endl << " are you sure you want to continue with these choices" << endl << "press Y to continue or any key to go back to the start "; 
     cin >> done; 
    } 
    cout << "Welcome to the Elysium " << Charactername.name << endl; 
    Sleep(5000); 

    Character Characterhealth; 
    Characterhealth.health = 100; 
    Character Charactergold; 
    Charactergold.gold = 0; 
    Character Characterlevel; 
    Characterlevel.level = 1; 
    Character Characterexperience; 
    Characterexperience.experience = 0; 

    using namespace areas; 
    TAvark TAvarkregion; 
    Forest Forestregion; 
    TAvarkregion.Avark; 
    Forestregion.RForest; 

    while (TAvarkregion.Avark != "Town" & TAvarkregion.Avark != "town" & Forestregion.RForest != "Forest" & Forestregion.RForest != "forest") 
     if (Charactergender.gender == "Male" & Charactergender.gender == "male") 
      cout << "Where would you like to go Sir " << Charactername.name << endl << endl; 
     else cout << "Where would you like to go Madam" << Charactername.name << endl << endl; 
     cout << "the Forest or the Town"; 
     cin << 


    Sleep(2000); 
}; 
+1

我错过了一些东西...... –

+0

它看起来像你正在寻找的是一种工厂方法。虽然问题不是很清楚 –

+0

对不起,不知道如何说出来 – gurkin

回答

0

您在C++中使用什么是继承。你定义一个基类:

class Region { 
// common parts 
}; 

,然后你说,这两个城镇和森林地区:

class Town : public Region { ... 
class Forest : public Region { ... 

,然后你说Avark是对象的类型镇:

Town Avark; // There could be other towns. 
相关问题