合工大机器人技术第一次个人作业

一点说明:

  • 由于处理see信息的模式字符串过于复杂,而且耗费精力,此处仅仅使用了字典来进行处理,而且也没有分清楚每个数字具体的物理意义。最后将字典格式的json文件写入了文件。可以使用format()函数来进行字符串格式化

涉及知识点:

  • 文件读写
  • 深拷贝

    向list之中添加字典对象的实现方式是拷贝一份原对象的引用过去,也就是浅拷贝,为了实现在list之中添加多条同字典信息,需要实现字典对象的深拷贝,也就是使用copy.deepcopy(obj)方法

  • 字符串处理的split方法
from ctypes import string_at
import json
import re
from turtle import right

#其实也可以将这些样例字符串放入到文件或者数组之中,懒得做了
originString ="(hear 1022 -30 passto(23,24))(see 1022 ((ball) -20 20 1 -2) ((player hfut 2) 45 23 0.5 1 22 40 )((goal r) 12 20) ((Line r) -30))"
originString2 = "(see 2022 ((player hfut2 8) 40 23 1 6 21 33)((goal r) 15 30)((f r t 20)5 24))(hear 2022 -10 “passball”)"
originString3 = "(see 2023 ((player hfut2 8) 10 20 1 2 6 8)((player hfut1 9) 6 7 8 10 20 3)((f r t 10) 15 20))(hear 2023 10 “dash”)"

#标准信息格式的定义
seeBallInfo = {
    "ballDirection":'',
    "ballDistance":'',
    "ballDistChng":'',
    "ballDirChng":'',
   
}
seePlayerInfo = {
    "player":'',
    "playerDirection":'',
    "playerDistance":'',
    "playerDistChng":'',
    "playerDirChng":'',
    "playerBodyDir":'',
    'playerHeadDir':''
}
seeFlagInfo = {
    "flag":'',
    'flagDirection':'',
    "flagDistance":''
}
seeLineInfo = {
    "line":'',
    "lineDistance":''
}
seeGoalInfo = {
    'goal':'',
    'goalInfo':''
}

#该字典用于存储看见的信息,infos为list类型,用来存储数量不定的信息
InfoDict = {
    'time':' ',
    'infos':[]
}


def splitStr(str):
    '''
    * 该方法可以实现将服务器传来的信息分割成hear和see两部分.
    * 参数:string,服务器传来的字符串
    * 返回值:[hear,see],list中第一个元素是hearInfo,the orther one is seeInfo
    '''
    leftBarcket = 0
    rightBarcket = 0
    stack = []
    for i in range(len(str)):
        if (rightBarcket == leftBarcket and rightBarcket!=0):
            break
        else:
            if (str[i]=='(' or str[i] == '['):
                leftBarcket+=1
            elif(str[i]==')' or str[i] == "]"):
                rightBarcket+=1
        stack.append(str[i])
    finalStr = ''
    while(len(stack) != 0):
        finalStr+=stack.pop(0)
    secondInfo = str.replace(finalStr,'')
    # return [finalStr,secondInfo]
    if(finalStr.count('see')):
        return [secondInfo,finalStr]
    else:
        return [finalStr,secondInfo]

        

def infoProcess(str):
    if('hear' in str):
        hear = str[1:-1]
        hear = hear.split(' ')
        hearInfo = '在第'+hear[1]+'周期'+hear[0]+'到了来自'+hear[-2]+'方向的消息:'+hear[-1]
        print(hearInfo)
        #将信息写入文件
        with open('hear.txt','w',encoding='utf-8') as fileobject:
            fileobject.write(hearInfo)

    elif ('see' in str):
        tempStore = ''   #临时存储
        leftBarckt = 0
        rightBarcket = 0
        str = str[1:-1]   #首先去除两侧括号
        for i in range(len(str)):
            print(i)            
            #碰到的第一个数字是周期数
            #第四五六七位必然为周期,继续使用切片,此过程仅仅循环一次
            if(i<4):
                continue
            elif(i>3 and i<8):
                InfoDict['time']+=str[i]
            elif(str[i] == '('):
                #将see和time信息清除
                leftBarckt+=1
            elif(str[i] == ')'):
                rightBarcket+=1
                if(leftBarckt == rightBarcket and leftBarckt!=0):
                    if ('player' in tempStore):
                        strArray = tempStore.split(' ')
                        seePlayerInfo['player'] = strArray[0]+' '+strArray[1]+' '+strArray[2]
                        #至于那个位置的信息具体是什么,这个没有仔细区分
                        seePlayerInfo['playerBodyDir'] = strArray[3]
                        seePlayerInfo['playerDirChng'] = strArray[4]
                        seePlayerInfo['playerDirection'] = strArray[5]
                        seePlayerInfo['playerDistance'] = strArray[6]
                        seePlayerInfo['playerDistChng'] = strArray[7]
                        seePlayerInfo['playerHeadDir'] = strArray[8]
                        #还有一点要实现深拷贝
                        InfoDict['infos'].append(copy.deepcopy(seePlayerInfo))
                        # seePlayerInfo.clear()
                    elif ('ball' in tempStore):
                        strArray = tempStore.split(' ')
                        seeBallInfo['ballDirChng'] = str[1]
                        seeBallInfo['ballDirection'] = str[2]
                        seeBallInfo['ballDistance'] = str[3]
                        seeBallInfo['ballDistChng'] = str[4]
                        InfoDict['infos'].append(copy.deepcopy(seeBallInfo))
                        # seeBallInfo.clear()
                    elif ('goal' in tempStore):
                        strArray = tempStore.split(' ')
                        seeGoalInfo['goal'] = strArray[0]
                        seeGoalInfo['goalInfo'] = strArray[1]+strArray[2]
                        InfoDict['infos'].append(copy.deepcopy(seeGoalInfo))
                    elif('Line' in tempStore):
                        strArray = tempStore.split(' ')
                        seeLineInfo['line'] = strArray[0] + strArray[1]
                        seeLineInfo['lineDistance'] = strArray[2]
                        InfoDict['infos'].append(copy.deepcopy(seeLineInfo))
                    elif (tempStore.startswith('f')):
                        strArray = tempStore.split(' ')
                        seeFlagInfo['flag'] = strArray[0] +" "+ strArray[1]+" "+strArray[2]+" "+strArray[3]
                        seeFlagInfo['flagDirection'] = strArray[4]
                        seeFlagInfo['flagDistance'] = strArray[-1]
                        InfoDict['infos'].append(copy.deepcopy(seeFlagInfo))
                    tempStore = tempStore.replace(tempStore,'') #说明一组信息已经结束,将其置空
            else:
                tempStore+=str[i]
        print(InfoDict) 
        with open('seeInfo.txt','w',encoding='utf-8') as fileobject:
            fileobject.write(json.dumps(InfoDict))
            

你可能感兴趣的:(机器人技术课程,python)