合肥工业大学2021机器人技术作业一
·
题目 : 在机器人足球比赛中, server 和球员 client 之间通过发送字符串来进行信息交互,其中 server 要把某球员的听觉和视觉信息发送给该球员,信息的格式如下所示:
(hear Time Sender Message)
(see Time ObjInfo ObjInfo …)
(hear Time Sender Message)的具体含义如下:
➢ Time:当前的仿真周期。
➢ Sender
➢ 如果是其他球员发送的消息,那么是发送者的相当方向( Direction )
➢ self :发送者是自己本人。
➢ referee :裁判是发送者。
➢ online_coach_left 或者 online_coach_ringt :发送者是在线教练。
➢ Message :消息内容。
(see Time ObjInfo ObjInfo … )的具体含义如下:
➢ Time:当前的仿真周期。
➢ ObjInfo 表示了可视对象的信息。其格式为:
(ObjName Direction Distance DirChng DistChng BodyDir HeadDir )
ObjName = ( player Teamname Unum )
➢ | ( goal Side )
➢ | ( ball )
➢ | ( flag c )
➢ | ( flag [ l | c | r] [ t | b ] )
➢ | ( flag p [ l | r] [ t | c | b ] )
➢ | ( flag [ t | b] [ l | r ] [10 | 20 | 30 | 40 |50 ] )
➢ | ( flag [ l | r] [ t | b ] [10 | 20 | 30 ] )
➢ | ( flag [ l | r | t | b ] 0 )
➢ | ( line [ l | r | t | b ] )
Direction , Distance 表示目标的相对距离和相对方向
DirChng 和 DistChng 分别表示目标距离和方向的相对变化,如果是固定物体(球和球员以外的所有对象)则没有改项值
BodyDir 和 HeadDir ,分别是被观察球员相对观察者的身体和头部的相对角度,只有是球队对象才有这一项值。
要求:编写程序解析球员所看到和听到的信息。
示例: (hear 1022 -30 passto(23,24))(see 1022 ((ball) -20 20 1 -2) ((player hfut1 2) 45 23 0.5 1 22
40 ) ((goal r) 12 20) ((Line r) -30))
输出:
在 1022 周期 hear 从 -30 方向 听到了 passto(23,24) ;
在 1022 周期 see Ball 距离我的 Direction 是 -20, Distance 是 20 , DistChng 是 1 , DirChng是-2 ; player hfut 2 距离我的 Direction 是 45 , Distance 是 23 , DistChng 是 0.5 , DirChng 是 1,它的 BodyDir 是 22 和 HeadDir 是 44 ; goal r 距离我的 Direction 是 12 , Distance 是 20 。 Line r 和我的角度是 -30 度。
思路:对所要解析的字符串进行遍历:分成‘hear’和’see‘两个类别分别进行解析,核心是通过对括号的匹配(左括号和右括号一对),记录他们在字符串中的下标位置,然后根据题目要求截取对应信息并存储,最后输出到文件中。
具体实现:
语言:C++ 工具:Visual Studio2019。
源代码:(仅供参考,水平有限,有错请指出)
#include<iostream>
#include<vector>
#include<algorithm>
#include <fstream>
using namespace std;
/*在 1022 周期 see Ball 距离我的 Direction 是 -20, Distance 是 20,DistChng 是 1,DirChng
是-2;player hfut 2 距离我的 Direction 是 45, Distance 是 23,DistChng 是 0.5,DirChng 是 1,它的 BodyDir 是 22 和 HeadDir 是 44;goal r 距离我的 Direction 是 12,Distance 是 20。
Line r 和我的角度是-30 度*/
class Player
{
public:
string ObjName, Distance, Direction, DistChng, DirChng, BodyDir, HeadDir;
Player(string ObjName1, string Distance1, string Direction1, string DistChng1, string DirChng1, string BodyDir1, string HeadDir1,string &s)
{
ObjName = ObjName1; Distance = Distance1; Direction = Direction1;
DistChng = DistChng1; DirChng = DirChng1; BodyDir = BodyDir1; HeadDir = HeadDir1;
s=this->rs();
}
string rs()
{
string see = ObjName + " 距离我的 Direction 是 " + Direction + ", Distance 是 " + Distance + ", DistChng 是 " + DistChng +
",DirChng 是 " + DirChng + ", 它的 BodyDir 是 " + BodyDir + "和 HeadDir 是 " + HeadDir+";";
return see;
}
};
class Ball
{
public:
string ObjName, Distance, Direction, DistChng, DirChng;
Ball(string ObjName1, string Distance1, string Direction1, string DistChng1, string DirChng1, string& s)
{
ObjName = ObjName1; Distance = Distance1; Direction = Direction1;
DistChng = DistChng1; DirChng = DirChng1;
s=this->rs();
}
string rs()
{
string see = ObjName + " 距离我的 Direction 是 " + Direction + ", Distance 是 " + Distance + ", DistChng 是 " + DistChng + ", DirChng 是 " + DirChng + ";";
return see;
}
};
class Goal
{
public:
string ObjName, Distance, Direction;
Goal(string ObjName1, string Distance1, string Direction1, string& s)
{
ObjName = ObjName1; Distance = Distance1; Direction = Direction1;
s=this->rs();
}
string rs()
{
string see = ObjName + " 距离我的 Direction 是 " + Direction + ", Distance 是 " + Distance + ";";
return see;
}
};
class Line
{
public:
string ObjName, Angle;
Line(string ObjName1, string Angle1, string& s)
{
ObjName = ObjName1; Angle = Angle1;
s=this->rs();
}
string rs()
{
string see = ObjName + " 和我的角度是 " + Angle + " 度" + ";";
return see;
}
};
class Flag
{
public:
string ObjName, Distance, Direction, DistChng, DirChng;
Flag(string ObjName1, string Distance1, string Direction1, string DistChng1, string DirChng1, string& s)
{
ObjName = ObjName1; Distance = Distance1; Direction = Direction1;
DistChng = DistChng1; DirChng = DirChng1;
s=this->rs();
}
string rs()
{
string see = ObjName + " 距离我的 Direction 是 " + Direction + ", Distance 是 " + Distance + ", DistChng 是 " + DistChng
+ ", DirChng 是 " + DirChng+";";
return see;
}
};
class Solution {
public:
vector<int>v1, v2, v3, v4, Vs1, Vs2;
vector<string>objinfos;
string hear, see;
int Seenum = 0;
bool is_first = true;
void match(string s);//括号匹配
void distribution(string s);//分配hear和see两个字符串
void See();//处理see字符串
void Hear();//处理hear字符串
void Write();//写文件
};
void Solution::match(string s)
{
bool over = false;
for (int i = 0; i < s.size(); i++)
{
if (s[i] == '(')
{
v1.push_back(i);
}
else if (s[i] == ')')
{
v2.push_back(i);
}
if (v1.size() == v2.size() && !over)
{
v3 = v1; v4 = v2;
v1.clear(); v2.clear();
over = true;
}
else continue;
}
}
void Solution::distribution(string s)
{
if (s[1] == 'h')
{
hear = s.substr(v3[0] + 6, v4[v4.size() - 1] - (v3[0] + 6));
see = s.substr(v1[0], v2[v2.size() - 1] - (v1[0]) + 1);
Seenum = (v1.size() - 1) / 2;
for (int j = 0; j < v1.size(); j++)
{
v1[j] = v1[j] - v4[v4.size() - 1] - 1;
v2[j] = v2[j] - v4[v4.size() - 1] - 1;
}
Vs1 = v1; Vs2 = v2;
}
else
{
is_first = false;
see = s.substr(v3[0], v4[v4.size() - 1] - (v3[0]) + 1);
hear = s.substr(v1[0] + 6, v2[v2.size() - 1] - (v1[0] + 6));
Seenum = (v3.size() - 1) / 2;
for (int j = 0; j < v1.size(); j++)
{
v1[j] = v1[j] - v4[v4.size() - 1] - 1;
v2[j] = v2[j] - v4[v4.size() - 1] - 1;
}
Vs1 = v3; Vs2 = v4;
}
//cout << hear << endl;
//cout << see<< endl;
}
void Solution::See()
{
string Time;
vector<int> flag2s;
for (int i = 0; i < see.size(); i++)
{
if (see[i] == ' ')
{
flag2s.push_back(i);
}
else continue;
}
Time = see.substr(5, flag2s[1] - 5);
for (int i = 0, j = 1; i < Seenum; i++, j++)//分配see的对象字符串
{
string object = see.substr(Vs1[j], Vs2[j] - Vs1[j] + 1);
j++;
//cout << object << endl;
objinfos.push_back(object);
}
vector<int>spaces, right;
for (int i = 0; i < objinfos.size(); i++)
{
for (int j = 0; j < objinfos[i].size(); j++)
{
if (objinfos[i][j] == ' ')
{
spaces.push_back(j);
}
else if (objinfos[i][j] == ')')
{
right.push_back(j);
}
else continue;
}
string obj = objinfos[i].substr(2, right[0] - 2);
if (obj[0] == 'b')//如果是球
{
string Distance, Direction, DistChng, DirChng;
Direction = objinfos[i].substr(right[0] + 2, spaces[1] - right[0] - 1);
Distance = objinfos[i].substr(spaces[1] + 1, spaces[2] - spaces[1]);
DistChng = objinfos[i].substr(spaces[2] + 1, spaces[3] - spaces[2]);
DirChng = objinfos[i].substr(spaces[3] + 1, right[1] - spaces[3] - 1);
Ball b(obj, Distance, Direction, DistChng, DirChng, objinfos[i]);
}
else if (obj[0] == 'p')//如果是人
{
for (int k = 0; k < 2; k++)
{
if (spaces[0] < right[0])
{
spaces.erase(spaces.begin());
}
}
string Distance, Direction, DistChng, DirChng, BodyDir, HeadDir;
Direction = objinfos[i].substr(right[0] + 2, spaces[1] - right[0] - 1);
Distance = objinfos[i].substr(spaces[1] + 1, spaces[2] - spaces[1]);
DistChng = objinfos[i].substr(spaces[2] + 1, spaces[3] - spaces[2]);
DirChng = objinfos[i].substr(spaces[3] + 1, spaces[4] - spaces[3]);
BodyDir = objinfos[i].substr(spaces[4] + 1, spaces[5] - spaces[4]);
HeadDir = objinfos[i].substr(spaces[5] + 1, right[1] - spaces[5] - 1);
Player p(obj, Distance, Direction, DistChng, DirChng, BodyDir, HeadDir, objinfos[i]);
}
else if (obj[0] == 'f')//如果是flag点
{
string Distance, Direction, DistChng, DirChng;
for (int k = 0; k < 2; k++)
{
if (spaces[0] < right[0])
{
spaces.erase(spaces.begin());
}
}
Direction = objinfos[i].substr(right[0] + 2, spaces[1] - right[0] - 1);
Distance = objinfos[i].substr(spaces[1] + 1, spaces[2] - spaces[1]);
DistChng = objinfos[i].substr(spaces[2] + 1, spaces[3] - spaces[2]);
DirChng = objinfos[i].substr(spaces[3] + 1, right[1] - spaces[3] - 1);
Flag f(obj, Distance, Direction, DistChng, DirChng, objinfos[i]);
}
else if (obj[0] == 'g')//如果是flag点
{
string Distance, Direction;
Direction = objinfos[i].substr(right[0] + 2, spaces[2] - right[0] - 1);
Distance = objinfos[i].substr(spaces[2] + 1, right[1] - spaces[2] - 1);
Goal g(obj, Distance, Direction, objinfos[i]);
}
else//如果是line
{
string Angle;
Angle = objinfos[i].substr(right[0] + 2, right[1] - right[0] - 2);
Line l(obj, Angle, objinfos[i]);
}
spaces.clear();
right.clear();
}
see.clear();
see = "在 " + Time + " 周期 see ";
for (auto h : objinfos)
{
see += h + '\n';
}
}
void Solution::Hear()
{
string Time, sender, message, object;
vector<int> flag1s;
for (int i = 0; i < hear.size(); i++)
{
if (hear[i] == ' ')
{
flag1s.push_back(i);
}
else continue;
}
Time = hear.substr(0, flag1s[0] - 0);
sender = hear.substr(flag1s[0] + 1, flag1s[1] - flag1s[0]);
message = hear.substr(flag1s[1] + 1, hear.size() - flag1s[1]);
hear.clear();
if (sender[0]=='s')
{
object = "自己说 ";
hear = "在" + Time + "周期 hear " + object + message +";"+ '\n';
}
else if (sender[0]=='r')
{
object = "裁判说 ";
hear = "在" + Time + "周期 hear " + object + message + ";" + '\n';
}
else if (sender[0]=='o')
{
object = "教练说 ";
hear = "在" + Time + "周期 hear " + object + message + ";" + '\n';
}
else
{
hear = "在 " + Time + " 周期 hear 从 " + sender + " 方向 听到了 " + message + ";" + '\n';
}
}
void Solution::Write()
{
ofstream ofs;
ofs.open("Translation.txt", ios::out);
if (is_first)
{
ofs << hear;
ofs << see;
}
else
{
ofs << see;
ofs << hear;
}
}
int main()
{
Solution s;
string s1 = "(hear 1022 -30 passto(23,24))(see 1022 ((ball) -20 20 1 -2) ((player hfut1 2) 45 23 0.5 1 22 40 ) ((goal r) 12 20) ((Line r) - 30))";
s.match(s1);
s.distribution(s1);
s.Hear();
s.See();
s.Write();
cout << "解析完成" << endl;
return 0;
}
效果示例 :
其他作业:
合肥工业大学2021机器人技术作业二
https://blog.csdn.net/qq_52791068/article/details/122642609
DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。
更多推荐

所有评论(0)