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


C++ Planet::getDistanceTo方法代码示例

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


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

示例1: processOrders


//.........这里部分代码省略.........
            tokens.erase(tokens.begin());
            if (tokens.size() == 4 && tokens[0] == "planet") {
                int planetId = atoi(tokens[1].c_str());
                const std::string& name = tokens[2];
                const std::string& value = tokens[3];

                Planet* planet = m_planets[planetId];
                planet->setProperty(name, value);
            }
            continue;
        }


        std::vector<std::string> tokens = Tokenize(line, " ");

        //Check whether the player bot output a bad thing.
        if (3 != tokens.size()) {
            std::stringstream message;
            message << "Error on line " << i << " of stdout output; expected 3 tokens on a move order line, have "
                    << tokens.size() << ".";
            player->logError(message.str());
            return false;
        }

        const int sourcePlanetId = atoi(tokens[0].c_str());
        const int destinationPlanetId = atoi(tokens[1].c_str());
        const int numShips = atoi(tokens[2].c_str());

        //Check whether the player has made any illegal moves.
        const int numPlanets = static_cast<int>(m_planets.size());

        if (sourcePlanetId < 0 || sourcePlanetId >= numPlanets) {
            std::stringstream message;
            message << "Error on line " << i << " of stdout output.  Source planet "
                    << sourcePlanetId << " does not exist.";
            player->logError(message.str());
            return false;
        }

        if (destinationPlanetId < 0 || destinationPlanetId >= numPlanets) {
            std::stringstream message;
            message << "Error on line " << i << " of stdout output.  Destination planet "
                    << destinationPlanetId << " does not exist.";
            player->logError(message.str());
            return false;
        }

        if (sourcePlanetId == destinationPlanetId) {
            std::stringstream message;
            message << "Error on line " << i
                    << " of stdout output.  Source planet and destination planet are the same. ";
            player->logError(message.str());
            return false;
        }

        Planet* sourcePlanet = m_planets[sourcePlanetId];
        Planet* destinationPlanet = m_planets[destinationPlanetId];

        if (sourcePlanet->getOwner()->getId() != player->getId()) {
            std::stringstream message;
            message << "Error on line " << i << " of stdout output.  Source planet "
                    << destinationPlanetId << " does not belong to this player.";
            player->logError(message.str());
            return false;
        }

        if (numShips > sourcePlanet->getNumShips() || numShips < 0) {
            std::stringstream message;
            message << "Error on line " << i << " of stdout output.  Cannot send " << numShips
                    << " ships from planet " << sourcePlanetId
                    << ".  Planet has " << sourcePlanet->getNumShips() << " ships.";
            player->logError(message.str());
            return false;
        }

        sourcePlanet->setNumShips(sourcePlanet->getNumShips() - numShips);

        //Create a new fleet.
        Fleet* fleet = new Fleet(this);
        fleet->setOwner(player);
        fleet->setSource(sourcePlanet);
        fleet->setDestination(destinationPlanet);
        fleet->setNumShips(numShips);
        const int distance = sourcePlanet->getDistanceTo(destinationPlanet);
        fleet->setTotalTripLength(distance);
        fleet->setTurnsRemaining(distance);

        m_newFleets.push_back(fleet);
        m_fleets.push_back(fleet);
    }

    if (!foundGo) {
        std::stringstream message;
        message << "Error: player did not send \"go\" within allotted time.";
        player->logError(message.str());
        return false;
    }

    return true;
}
开发者ID:kerzum,项目名称:PlanetWarrior,代码行数:101,代码来源:game.cpp


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