⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 filer.cpp

📁 AI-CODE坦克机器人 《C++语言学习利器 —AI-CODE坦克机器人》-杜飞雪-源代码
💻 CPP
字号:
#include <airobot/cpp/SimpleRobot.hpp>
#include <fstream>
using namespace std;

/**
 * 这是一个演示读写文件的机器人。
 * 在比赛开始时,机器人读取自己上一场比赛的得分信息,显示在控制台上。
 * 在比赛结束时,机器人将自己的得分信息保存到文件中。
 */
class Filer : public SimpleRobot
{
	public:
		/**
		 * 每个单位时间都会触发
		 */
		void onTick(TickAction* action)
		{
			Bot* opponent = getFirstOpponent();
			if(opponent==NULL) return;
			double heading = Math::heading(getLocation(), opponent->getLocation());
			fire(heading, 3);
		}
		
		/** 
		 * 当开始一场新的比赛时触发
		 */
		void onMatchBegin(MatchBeginAction* action)
		{
			ifstream ifs("filer");
			if(ifs)
			{
				double score;
				ifs>>score;
				
				ostringstream oss;
				oss<<"The last match score is : "<<score;
				println(oss);
			}
			else println("Can not read the last match record.");
		}
		
		/**
		 * 当整场比赛结束时触发
		 */
		void onMatchFinish(MatchFinishAction* action)
		{
			ofstream ofs("filer");
			if(ofs)
			{
				ofs<<getScore();
			}
			else println("Can not save match record.");
		}
};

/**
 * 机器人程序入口
 */
int main(int argC, char* argV[])
{
	Robot* robot = new Filer();
	return startup(argC, argV, robot);
}


															

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -