当前位置: 首页>>代码示例>>C++>>正文


C++ PhysicalObject::getDescription方法代码示例

本文整理汇总了C++中PhysicalObject::getDescription方法的典型用法代码示例。如果您正苦于以下问题:C++ PhysicalObject::getDescription方法的具体用法?C++ PhysicalObject::getDescription怎么用?C++ PhysicalObject::getDescription使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在PhysicalObject的用法示例。


在下文中一共展示了PhysicalObject::getDescription方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: initCommands

void Player::initCommands() {
    std::function<void (std::vector<std::string> commandWords, std::function<bool(const std::vector<std::string> &)>)> addCommands = [this](std::vector<std::string> commandWords, std::function<bool(const std::vector<std::string> &)> operation){
        if(commandWords.size() < 1) {
            throw std::invalid_argument("You must at least have one word associated with the command");
        }
        uniqueCommands.push_back(commandWords.front());
        for(std::string word: commandWords){
            if(commands.find(word) != commands.end()) {
                throw std::invalid_argument("The word: " + word + " is associated with more than one command");
            }
            commands[word] = operation;
        }
        
    };
    
    std::function<bool (const std::vector<std::string> & commands, const std::string & helpText, const std::string & usageCommands)> isHelp = [](const std::vector<std::string> & commands, const std::string & helpText, const std::string & usageCommands){
        if(commands.size() != 2){ return false; };
        if(commands[1] != "help"){ return false; };
        
        std::cout << TEXT_DIVIDER << " HELP: " << commands[0] << " " << TEXT_DIVIDER << std::endl;
        if(helpText != "") {
            std::cout << helpText << std::endl;
        }
        std::cout << "Usage: " << commands[0] << " " << usageCommands << std::endl;
        return true;
    };
    
    addCommands({"go", "move", "goto"}, [this, isHelp](const std::vector<std::string> & commands) -> bool {
        if(isHelp(commands, "Used for navigating through the world.", "LOCATION")) { return false; }
            
        if(commands.size() != 2) {
            std::cout << "You forgot to write where you wanna go." << std::endl;
            return false;
        }
        
        
        int num = -1; 
        try {
            num = atoi(commands[1].c_str()) - 1;
        } catch(int) {
            std::cout << "That is not an option. Write one of the numbers given as option." << std::endl;
            return false;
        }
        
        auto dirs = getEnvironment()->getDirections();
        
        if(num >= dirs.size()) {
            std::cout << "That is not an option. Write one of the numbers given as option." << std::endl;
            return false;
        }
        
        if(!this->move(dirs[num])) {
                std::cout << "That is not an option. Write one of the numbers given as option." << std::endl;
                return false;
            }
        
        return true;
    });
    
    addCommands({"look"}, [this, isHelp](const std::vector<std::string> & commands) -> bool {
        if(isHelp(commands, "Look for example at different characters or on items to get more information. You can look at almost anything in the world.", "[CONTAINER] [OBJECT]")) { return false;}
        Environment * env = getEnvironment();
        if(commands.size() == 1) {
            printUpdateInfo();
        } else if(commands.size() == 2) {
            if(isCommandInventory(commands[1])) {
                std::cout << getInventory()->getDescription() << std::endl;
                return false;
            }
            if(isCommandEquipment(commands[1])) {
                std::cout << getEquipment()->getDescription() << std::endl;
                return false;
            }
            PhysicalObject * physicalObject = env->find(commands[1]);
            if(physicalObject == NULL) {
                std::cout << "Found no item named: " << commands[1] << std::endl;
            } else {
                std::cout << physicalObject->getDescription() << std::endl;
            }
            
            
        } else if(commands.size() == 3) {
            Item * item = NULL;
            if(isCommandInventory(commands[1])) {
                Inventory * inv = getInventory();
                item = inv->find(commands[2]);
                if(item == NULL) {
                    std::cout << "Found no item named: " << commands[2] << " in your inventory." << std::endl;
                    return false;
                }
            } else if(isCommandEquipment(commands[1])) {
                Equipment * eq = getEquipment();
                item = eq->find(commands[2]);
                if(item == NULL) {
                    std::cout << "Found no item named: " << commands[2] << " in your equipment." << std::endl;
                    return false;
                }
            } else {
                Container * con = env->find<Container>(OBJECT_TYPE_CONTAINER, commands[2]);
                if(con == NULL) {
//.........这里部分代码省略.........
开发者ID:wnr,项目名称:cprog13-game,代码行数:101,代码来源:Player.cpp


注:本文中的PhysicalObject::getDescription方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。