로봇 축구 시뮬레이션의 첫 번째 작업

로봇 축구 시뮬레이션의 첫 번째 작업

로봇 축구 시뮬레이션은 선수의 의사 결정, 팀 개발 및 기타 지식에 대해 이야기하는 RoboCup과 관련된 과정입니다.
교사가 배정한 첫 번째 숙제입니다. 기본 과제는 문자열을 파싱하는 것입니다. 그 기능은 uva의 Prase 기능과 동일합니다.

직무 내용 및 요구 사항

요구 사항 : 플레이어가보고 듣는 정보를 분석하는 프로그램을 작성하십시오.
예 : (hear 1022-30 passto (23,24)) (see 1022 ((ball) -20201-2) ((player hfut1 2) 45 23 0.5 1 22
40) ((goal r) 12 20) ( (Line r) -30))
출력 :
1022 기간에 -30 방향으로 passto (23,24) 듣기; 1022 기간의 볼 거리 참조. My Direction은 -20, Distance는 20, DistChng은 1, DirChng
은 -2; player hfut 2 내 방향까지의 거리는 45, 거리는 23, DistChng은 0.5, DirChng은 1, BodyDir은 22, HeadDir은 44입니다. 목표 r은 내 방향까지의 거리는 12, 거리는 20입니다.
Line r과 나 사이의 각도는 -30도

그러나 여기서는 정보의 형식이
(Time Sender Message를 듣습니다)
(Time ObjInfo ObjInfo… 참조)
ObjInfo가 시각적 개체의 정보를 나타 내기 때문에 샘플 출력이 다소 문제가 있다고 생각 합니다. 형식은 다음과 같습니다.
(ObjName 방향 거리 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