📄 firecircle.cpp
字号:
#include <airobot/cpp/SimpleRobot.hpp>
/**
* 这个机器人演示了如何用迭代的方法打击走直线的机器人
* @author xiemin
*/
class FireCircle : public SimpleRobot
{
private:
//发弹的能量
static const double POWER;
//对手上个单位时间的方向
double lastHeading;
public:
void onTick(TickAction* action)
{
Bot* bot = getFirstOpponent();
if(bot==NULL) return;
//计算机器人两个单位时间的方向的夹角
double bearing = Math::bearing(bot->getHeading(), lastHeading);
//初始化迭代
double nextHeading = bot->getHeading();
Point2D nextLocation = bot->getLocation();
long time = 0;
//进入迭代
while(true)
{
//判断子弹在time时间后能否打到在next位置的对手
double distance = Math::distance(nextLocation, getLocation());
if(distance/getBulletVelocity(POWER)<=time)
{
//能打到,迭代结束
fire(nextLocation, POWER); //开火
break;
}
//进入下一次迭代
time ++;
nextHeading += bearing;
//计算在下个时间对手的位置
nextLocation = Math::nextPoint(nextLocation,
nextHeading, bot->getVelocity());
}
lastHeading = bot->getHeading();
}
};
//开火时的炮弹能量
const double FireCircle::POWER = 2;
//启动机器人程序
int main(int argC, char* argV[])
{
Robot* robot = new FireCircle();
return startup(argC, argV, robot);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -