import time#据反馈，在robomaster中执行请删除此行！！！
marklist = RmList()#定义各个变量和列表
llist = RmList()
num = RmList()
zx = RmList()
i = 0
a = 0
b = 0
c = 0
d = 0
an = 0
bn = 0
cn = 0
dn = 0
on = 0
operative = 0
count = 0
df = 1000
n = 0
def f(q, w, e, r):#进行赋值操作
    global a
    global b
    global c
    global d
    global an
    global bn
    global cn
    global dn
    global llist
    global num
    a = llist[q]
    b = llist[w]
    c = llist[e]
    d = llist[r]
    an = num[q]
    bn = num[w]
    cn = num[e]
    dn = num[r]
def shoot(n):#执行击打操作
    global a#申明全局变量
    global b
    global c
    global d
    global an
    global bn
    global cn
    global dn
    global llist
    global num
    global zx
    if marklist[n] < 22:#输出数字
        print(marklist[n]-10)
    elif marklist[n] == 50:#输出符号
        print('+')
    elif marklist[n] == 51:
        print('-')
    elif marklist[n] == 52:
        print('/')
    elif marklist[n] == 43:
        print('*')
    gimbal_ctrl.angle_ctrl((marklist[n+1]-zx[1])*96, (zx[2]-marklist[n+2])*64)#将云台转动至指定位置
    time.sleep(0.1)#等待0.1s防止云台转动中击打，使弹道偏移
    gun_ctrl.fire_once()
    time.sleep(0.1)#等待0.1s防止云台转动中击打，使弹道偏移
def point24():#暴力破解
    global i#申明全局变量
    global a
    global b
    global c
    global d
    global an
    global bn
    global cn
    global dn
    global on
    global llist
    global operative
    global df
    for i in range(1,25):#循环24次
        if 1:   # 24种排列方式
            if i == 1:
                f(1, 2, 3, 4)#对a,b,c,d,an,bn,cn,dn赋值
            elif i == 2:
                f(1, 2, 4, 3)
            elif i == 3:
                f(1, 3, 2, 4)
            elif i == 4:
                f(1, 3, 4, 2)
            elif i == 5:
                f(1, 4, 2, 3)
            elif i == 6:
                f(1, 4, 3, 2)
            elif i == 7:
                f(2, 1, 3, 4)
            elif i == 8:
                f(2, 1, 4, 3)
            elif i == 9:
                f(2, 3, 1, 4)
            elif i == 10:
                f(2, 3, 4, 1)
            elif i == 11:
                f(2, 4, 1, 3)
            elif i == 12:
                f(2, 4, 3, 1)
            elif i == 13:
                f(3, 1, 2, 4)
            elif i == 14:
                f(3, 1, 4, 2)
            elif i == 15:
                f(3, 2, 1, 4)
            elif i == 16:
                f(3, 2, 4, 1)
            elif i == 17:
                f(3, 4, 1, 2)
            elif i == 18:
                f(3, 4, 2, 1)
            elif i == 19:
                f(4, 1, 2, 3)
            elif i == 20:
                f(4, 1, 3, 2)
            elif i == 21:
                f(4, 2, 1, 3)
            elif i == 22:
                f(4, 2, 3, 1)
            elif i == 23:
                f(4, 3, 1, 2)
            elif i == 24:
                f(4, 3, 2, 1)
        if operative == 50:  # +
            if (a * 10 + b + c == 24 and a != 0):  # 12+3	尝试击打方式
                print(11)#击打方法
                print(' ')#将已输出内容和将要输出的内容分隔开
                shoot(an)#击打a
                shoot(bn)#击打b
                shoot(on)#击打符号
                shoot(cn)#击打c
                return#结束程序
            elif a * 10 + b + c * 10 + d == 24 and a != 0 and c != 0:  # 12+34
                print(12)
                print(' ')
                shoot(an)
                shoot(bn)
                shoot(on)
                shoot(cn)
                shoot(dn)
                return
            elif a + b + c == 24 and a != 0 and c != 0:  # 1+2+3
                print(13)
                print(' ')
                shoot(an)
                shoot(on)
                shoot(bn)
                shoot(on)
                shoot(cn)
                return
            elif a * 10 + b + c + d == 24 and a != 0 and c != 0 and d != 0:  # 12+3+4
                print(14)
                print(' ')
                shoot(an)
                shoot(bn)
                shoot(on)
                shoot(cn)
                shoot(on)
                shoot(dn)
                return
            elif a + b + c + d == 24 and a != 0 and b != 0 and c != 0 and d != 0:  # 1+2+3+4
                print(15)
                print(' ')
                shoot(an)
                shoot(on)
                shoot(bn)
                shoot(on)
                shoot(cn)
                shoot(on)
                shoot(dn)
                return
        elif operative == 51:  # -  
            if a * 10 + b - c == 24 and a != 0:  # 12-3
                print(21)
                print(' ')
                shoot(an)
                shoot(bn)
                shoot(on)
                shoot(cn)
                return
            elif a * 10 + b - c * 10 - d == 24 and a != 0 and c != 0:  # 12-34
                print(22)
                print(' ')
                shoot(an)
                shoot(bn)
                shoot(on)
                shoot(cn)
                shoot(dn)
                return
            elif a * 10 + b - c - d == 24 and a != 0 and c != 0 and d != 0:  # 12-3-4
                print(23)
                print(' ')
                shoot(an)
                shoot(bn)
                shoot(on)
                shoot(cn)
                shoot(on)
                shoot(dn)
                return
        elif operative == 43:  # *  
            if a * b == 24 and a != 0 and b != 0 and a != 1 and b != 1:  # 1*2
                print(31)
                print(' ')
                shoot(an)
                shoot(on)
                shoot(bn)
                return
            elif (a * 10 + b) * c == 24 and a != 0 and c != 0:  # 12*3
                print(32)
                print(' ')
                shoot(an)
                shoot(bn)
                shoot(on)
                shoot(cn)
                return
            elif a * b * c == 24 and a != 0 and b != 0 and c != 0 and a != 1 and b != 1 and c != 1:  # 1*2*3
                print(33)
                print(' ')
                shoot(an)
                shoot(on)
                shoot(bn)
                shoot(on)
                shoot(cn)
                return
            elif a * b * c * d == 24 and a != 0 and b != 0 and c != 0 and d != 0 and a != 1 and b != 1 and c != 1 and d != 1:  # 1*2*3*4
                print(34)
                print(' ')
                shoot(an)
                shoot(on)
                shoot(bn)
                shoot(on)
                shoot(cn)
                shoot(on)
                shoot(dn)
                return
        elif operative == 52:  # /  
            if (a * 10 + b) == 24 * c and a != 0 and c != 0:  # 12/3
                print(41)
                print(' ')
                shoot(an)
                shoot(bn)
                shoot(on)
                shoot(cn)
                return
            elif (a * 10 + b) == 24 * c * d and  b != 0 and c != 0 and d != 0 and c != 1 and d != 1:  # 12/3/4
                print(42)
                print(' ')
                shoot(an)
                shoot(bn)
                shoot(on)
                shoot(cn)
                shoot(on)
                shoot(dn)
                return
            elif (a * 100 + b * 10 + c) == 24 * d and a != 0 and c != 0 and c != 1:  # 123/4
                print(43)
                print(' ')
                shoot(an)
                shoot(bn)
                shoot(cn)
                shoot(on)
                shoot(dn)
                return
def start():
    global i#申明全局变量
    global a
    global b
    global c
    global d
    global an
    global bn
    global cn
    global dn
    global marklist
    global llist
    global on
    global operative
    global count
    global num
    global zx
    global n
    zx=RmList(media_ctrl.get_sight_bead_position())#将zx设为准星坐标
    robot_ctrl.set_mode(rm_define.robot_mode_free)#设置为自由模式
    gun_ctrl.set_fire_count(1)#设置每次发射一颗子弹
    gimbal_ctrl.set_rotate_speed(360)#调整云台转速为360度每秒
    vision_ctrl.enable_detection(rm_define.vision_detection_marker)#开启视觉标签识别
    vision_ctrl.set_marker_detection_distance(3)#设置识别距离为3m
    vision_ctrl.marker_detection_color_set(rm_define.marker_detection_color_red)#设置默认识别颜色为红
    i = 1
    gimbal_ctrl.angle_ctrl(0,0)#将云台回中
    gimbal_ctrl.recenter()
    time.sleep(0.1)
    while not 26 <= len(marklist):#将红、蓝、绿三种情况写在一起（防止比赛时未调整阵营）
        i = i % 3
        n = n + 1
        marklist = RmList(vision_ctrl.get_marker_detection_info())#marklist设为识别到的视觉标签信息
        if i == 1:
            print("try red")
            vision_ctrl.marker_detection_color_set(rm_define.marker_detection_color_red)#尝试红色
        elif i == 2:
            print("try blue")
            vision_ctrl.marker_detection_color_set(rm_define.marker_detection_color_blue)#尝试蓝色
        elif i == 3:
            print("try green")
            vision_ctrl.marker_detection_color_set(rm_define.marker_detection_color_green)#尝试绿色
        i = i + 1
        if n >= 90:#若90次未扫描出则退出
            return#结束程序
    i = 2
    for i in range(2, 27, 5):  # 读取数字并存入llist中或读取符号存入o中
        if 10 <= marklist[i] <= 19:
            llist.append(marklist[i]-10)
            num.append(i)#记录数字在marklist中的位置，防止出现相同数字时重复击打
        else:
            operative = marklist[i]#与数字同理
            on = i
    print(marklist)#输出识别数据
    print(llist)#输出识别到的数
    print(operative)#输出识别到的符号
    point24()#开启暴力搜索函数