# -*- encoding: utf-8 -*-
"""
 @Author: packy945
 @FileName: data.py
 @DateTime: 2023/5/11 14:36
 @SoftWare: PyCharm
"""
from itertools import permutations
import random
import tkinter as tk

MARK = '#ff0000'
NOMARK = '#cccccc'


class Aexp:
    def __init__(self, a, b, c, d):
        self.Aexp_cur = 0
        self.num = 0
        self.card = [a, b, c, d]
        self.answer = []
        self.Color = []
        self.Aexp(a, b, c, d)

    def refresh(self, a, b, c, d):
        self.Aexp_cur = 0
        self.num = 0
        self.card = [a, b, c, d]
        # print(self.card)
        self.answer = []
        self.Color = []
        self.Aexp(a, b, c, d)

    def Aexp(self, one, two, three, four):
        '''
        输入四个数，求24点
        :param one:
        :param two:
        :param three:
        :param four:
        :return: 求解24点结果。若无法得出则返回“无法求得结果”
        '''
        self.Aexp_cur = 0
        my_list = [one, two, three, four]
        result = [c for c in permutations(my_list, 4)]
        list2 = []  # 算出24的排列组合的列表
        symbols = ["+", "-", "*", "/"]
        flag = False
        for one, two, three, four in result:
            for s1 in symbols:
                for s2 in symbols:
                    for s3 in symbols:
                        express = ["(({0}{1}{2}){3}{4}){5}{6}".format(one[1], s1, two[1], s2, three[1], s3, four[1]),
                                   "({0}{1}{2}){3}({4}{5}{6})".format(one[1], s1, two[1], s2, three[1], s3, four[1]),
                                   "({0}{1}({2}{3}{4})){5}{6}".format(one[1], s1, two[1], s2, three[1], s3, four[1]),
                                   "{0}{1}(({2}{3}{4}){5}{6})".format(one[1], s1, two[1], s2, three[1], s3, four[1]),
                                   "{0}{1}({2}{3}({4}{5}{6}))".format(one[1], s1, two[1], s2, three[1], s3, four[1])]
                            # if str(one) + str(two) + str(three) + str(four) == "8383":
                            #     print(express)
                        for e in express:
                            try:
                                if round(eval(e), 6) == 24:
                                    e = del_parentheses(e)
                                    list2.append(e)
                                    self.Color.append([one[0], two[0], three[0], four[0]])
                                    flag = True
                            except ZeroDivisionError:
                                pass


        if flag:
            self.answer = list2
            self.num = len(list2)
            # return list3

        else:
            self.answer = ["无法得出24点"]
            self.Color.append(None)
            self.num = 0


#删除最外层括号
def del_parentheses(e):
    if e[0] == '(' and e[-1] == ')':
        try:
            eval(e[1:len(e)-1])
        except:
            pass
        else:
            if round(eval(e[1:len(e)-1]),6) == 24:
                return e[1:len(e)-1]
    return e



if __name__ == '__main__':
    ans = Aexp(1, 2, 4, 6)
    for i in ans.answer:
        print(i)
    # print(ans)
    pass
