ロボットサッカーシミュレーションの最初の仕事

ロボットサッカーシミュレーションの最初の仕事

ロボットフットボールシミュレーションは、RoboCupに関連するコースで、プレーヤーの意思決定、チーム開発、その他の知識について話します。
これは教師によって割り当てられた最初の宿題です。基本的なタスクは文字列を解析することです。その機能はuvaのPraseの機能と同等です。

仕事の内容と要件

要件:プレーヤーが見たり聞いたりする情報を解析するプログラムを作成します。
例:(1022 -30 passto(23,24)を聞く)(1022((ball)-20 20 1 -2を参照)((player hfut1 2)45 23 0.5 1 22
40)((goal r)12 20)( (行r)-30))
出力:
1022期間の-30の方向でpassto(23,24)を聞きます。1022期間のボール距離を参照してください。私の方向は-20、距離は20、DistChngは1、DirChng
は-2; player hfut 2私の方向までの距離は45、距離は23、DistChngは0.5、DirChngは1、BodyDirは22、HeadDirは44、ゴールrは私の方向までの距離は12、距離は20です。
ラインrと私の間の角度は-30度です

しかし、ここでは、情報の形式が
(Time Sender Messageを聞く)
(Time ObjInfo ObjInfo…を参照であるため、サンプル出力はやや問題があると思います
。ObjInfoはビジュアルオブジェクトの情報を表します。形式は次のとおりです。
(ObjName Direction Distance DirChng DistChng BodyDir HeadDir)
したがって、例では(1022((ball)-20 20 1 -2)((player hfut1 2)45 23 0.5 1 22 40)を参照してください。ここで、1 -2および0.5 1は、それぞれ方向変更量と距離変更量である必要があります。

ソリューション

プレイヤーが受け取る情報は、「聞いた」と「見た」に分けることができます。聞いた情報は、情報を送信した人(つまり方向)と情報の内容です。見た情報は、プレイヤーに分けられます。情報、ボール情報、ゴール情報、直線情報。定義クラスはこの情報をカプセル化し、クラスのメンバー関数を使用して情報を分析および出力します。

コード

ヘッドファイル:

/*file: robocup2d1st.h
brief:各个类的定义(接收到的信息类)*/

#include<string>
#include<algorithm>
#include<vector>
using namespace std;

class Player{
    
      //有关接收到的球员信息类
    public:
        Player(vector<char*> &vecChar);
        ~Player();
        void showPlayer();    //打印接收到的球员的信息
    private:
        string playerName;  //判断是哪边的球员
        double dirPlayer;   //距离观察者的相对方向
        double distPlayer;  //距离观察者的距离
        double dirChngPlayer;  //方向的改变量
        double distChngPlayer;  //距离的改变量
        double bodyDir;       //该球员相对于观察者身体的角度
        double headDir;      //该球员相对于观察者头的角度
};

class Ball{
    
       //有关球的信息内容
    public:
        Ball(vector<char*> &vecChar);
        ~Ball();
        void showBall();    //打印接收到的球的信息
    private:
        double dirBall;     //球距离观察者的相对方向
        double distBall;     //球距离观察者的距离
        double distChngBall;  //球方向的改变量
        double dirChngBall;   //球距离的改变量
};

class Goal{
    
       //有关球门的信息内容
    public:
        Goal(vector<char*> &vecChar);
        ~Goal();
        void showGoal();
    private:
        string typeGoal; //判断是哪边的球门r or l
        double dirGoal;  //球门与观察者的相对方向
        double distGoal;  //球门距离观察者的距离
};

class Line{
    
      //有关线的信息内容
    public:
        Line(vector<char*> &vecChar);
        ~Line();
        void showLine();
    private:
        string typeLine;  //r or l
        double angleLine;  //该线相对于观察者的角度
};

.cppファイル

/*file:robocup2d1st.cpp
compiler: VS Code
function: parsing information
*/
#include<iostream>
#include<stdlib.h>
#include"robocup2d1st.h"

using namespace std;

Player::Player(vector<char*> &vecChar){
    
    
    vector<char*>::iterator it;
    string s{
    
    "player"};
    it = find(vecChar.begin(), vecChar.end(), s);
    //若接收到的信息中没有player的信息
    if(it == vecChar.end()) {
    
      
        cout<<"No information about player"<<endl;
    }else {
    
    
        it++;
        it++;
        playerName = *(it++);
        dirPlayer = atof(*(it++));
        distPlayer = atof(*(it++));
        dirChngPlayer = atof(*(it++));
        distChngPlayer = atof(*(it++));
        bodyDir = atof(*(it++));
        headDir = atof(*(it++));
    }

}

Player::~Player(){
    
    }

void Player::showPlayer(){
    
    
    cout<<" player hfut "<<playerName
        <<"距离我的 Direction是 "<<dirPlayer
        <<",Distance 是"<<distPlayer
        <<",DirChng 是 "<<dirChngPlayer
        <<",DistChng 是 "<<distChngPlayer
        <<",它的 BodyDir 是 "<<bodyDir
        <<",HeadDir 是 "<<headDir<<";";
}

Ball::Ball(vector<char*> &vecChar){
    
    
    vector<char*>::iterator it;
    string s{
    
    "ball"};
    it = find(vecChar.begin(), vecChar.end(), s);
    if(it == vecChar.end()){
    
    
        cout<<"No information about ball"<<endl;
    }else {
    
    
        it++;
        dirBall = atof(*(it++));
        distBall = atof(*(it++));
        dirChngBall = atof(*(it++));
        distChngBall = atof(*(it++));
    }
}

Ball::~Ball(){
    
    }

void Ball::showBall(){
    
    
    cout<<"Ball 距离我的 Direction是 "<<dirBall
        <<",Distance 是 "<<distBall
        <<",DirChng 是 "<<dirChngBall
        <<",DistChng 是 "<<distChngBall;
}

Goal::Goal(vector<char*> &vecChar){
    
    
    vector<char*>::iterator it;
    string s{
    
    "goal"};
    it = find(vecChar.begin(), vecChar.end(), s);
    if(it == vecChar.end()) {
    
    
        cout<<"No information about goal"<<endl;
    }else {
    
    
        it++;
        typeGoal = *(it++);
        dirGoal = atof(*(it++));
        distGoal = atof(*(it++));
    }
}

void Goal::showGoal(){
    
    
    cout<<" goal "<<typeGoal
            <<"距离我的 Direction是 "<<dirGoal
            <<",Distance 是 "<<distGoal<<";";
}

Goal::~Goal(){
    
    }

Line::Line(vector<char*> &vecChar){
    
    
    vector<char*>::iterator it;
    string s{
    
    "Line"};
    it = find(vecChar.begin(), vecChar.end(), s);
    if(it == vecChar.end()) {
    
    
        cout<<"No information about Line"<<endl;
    }else {
    
    
        it++;
        typeLine = *(it++);
        angleLine = atof(*(it++));
    }
}

Line::~Line(){
    
    }

void Line::showLine(){
    
    
    cout<<" Line "<<typeLine
            <<" 和我的角度是 "<<angleLine
            <<" 度";
}

//用特定的分隔符分割字符串,并将分割后的字符串存储在vector中
void cutString(char *ch, vector<char*> &vecChar){
    
    
    const char *mark = "( )";
    char *p = strtok(ch, mark);
    while(p){
    
    
        vecChar.push_back(p);
        p = strtok(NULL, mark);
    }
    return ;
}

//处理字符串
void dealWithStr(vector<char*> &vecChar){
    
    
    Player p(vecChar);     //声明各个信息类的对象
    Ball b(vecChar);
    Goal g(vecChar);
    Line l(vecChar);
    vector<char*>::iterator it = vecChar.begin();
    string s1{
    
    "hear"};
    it = find(vecChar.begin(), vecChar.end(), s1);
    //听到的信息
    if(it == vecChar.end()) {
    
    
        cout<<"Didn't hear anything"<<endl;  //若没有听到信息
    }else {
    
    
        it++;
        cout<<"在"<<*(it++)<<"周期 hear "
        <<"从"<<*(it++)<<"方向 听到了 "<<*(it++)
        <<"("<<*(it++)<<")"<<endl;
    }
    string s2{
    
    "see"};
    it = find(vecChar.begin(), vecChar.end(), s2);
    //看到的信息
    if(it == vecChar.end()) {
    
    
        cout<<"Didn't see anything"<<endl;  //若没有看到信息
    }else {
    
    
        it++;
        cout<<"在"<<*(it++)<<"周期 see ";
        b.showBall();
        p.showPlayer();
        g.showGoal();
        l.showLine();
    }
    
    return ;
}

int main(){
    
    
    vector<char*> vecChar;
    char ch[2000] = "(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))";
    cutString(ch, vecChar);
    dealWithStr(vecChar);
    
    return 0;
}

出力:(VSコードを使用してコンパイルおよび実行)
ここに写真の説明を挿入
私の考えは、情報をクラスにカプセル化して、さまざまな情報の処理メソッドが類似するようにすることですが、この処理メソッドもより面倒で面倒です。実際、文字列出力を直接処理することもできます。 。このアイデアは参照用です。訂正してください。

おすすめ

転載: blog.csdn.net/qq_45800517/article/details/108802389