用python 代码实现24点游戏的解决

2021年8月29日 337点热度 0人点赞

24点游戏是一个简单但有趣的游戏,玩家利用给出来的四个整数,通过加减乘除四则运算找出能够算出24的表达式。24点游戏常用来训练计算能力以及逻辑能力等,因此写出了以下代码

# 24点游戏基本思路
# 1.输入4个整数
# 2.推算出所有可能的组成方式
# 3.一个一个验证,得出答案


# 输入4个整数
int1 = int(input('请输入第一个整数:'))
int2 = int(input('请输入第二个整数:'))
int3 = int(input('请输入第三个整数:'))
int4 = int(input('请输入第四个整数:'))
cv = ''


# 计算出每一种情况的计算结果
def every_method(a, b, c, d):
    global cv
    method = ['+', '-', '*', '/']
    nums = [a, b, c, d]
    for a1 in nums:
        for x1 in method:
            for b1 in nums:
                for y1 in method:
                    for c1 in nums:
                        for z1 in method:
                            for d1 in nums:
                                if not same(a1, b1, c1, d1):
                                    continue
                                dcf = [str(a1), str(x1), str(b1), str(y1), str(c1), str(z1), str(d1)]
                                text__ = list(dcf)
                                for t in range(0, len(text__) + 1):
                                    for u in range(t + 1, len(text__) + 1):
                                        text__ = list(dcf)
                                        text__.insert(t, '(')
                                        text__.insert(u-1, ')')

                                        cv = ''
                                        for h in text__:
                                            cv = cv + h
                                        cv = del_(cv)
                                        if check(cv):
                                            print('正在尝试{}中……'.format(cv))
                                            if not fu_hao(cv):
                                                continue
                                            if (cul(cv)) == 24:

                                                print('符合条件的算式为' + cv + '=24')
                                                return True
                                if y1 == '/' and eval(str(c1) + str(z1) + str(d1)) == 0:
                                    pass

                                else:
                                    if eval('(' + str(a1) + str(x1) + str(b1) + ')' + str(y1) + '(' + str(c1) + str(z1) + str(d1) + ')') == 24:
                                        print('符合条件的算式为:(' + str(a1) + str(x1) + str(b1) + ')' + str(y1) + '(' + str(c1) + str(z1) + str(d1) + ')=24')
                                        return True
    print('找不到这样的组合!')
    return False


# 根据输入的算式文本计算结果
def cul(text):
    return eval(str(text))


def del_(text):     # 删除单个数字前后的括号
    text_list = list(text)
    for o in range(0, len(text_list) - 2):
        if (text_list[o] == '(' or text_list[o] == ')') and (text_list[o+2] == ')' or text_list[o+2] == '('):
            text_list.remove('(')
            text_list.remove(')')
    text1 = ''.join(text_list)
    return text1


# 检查是否所有数字都用上以及各数字个数都准确
def same(h, i, j, k):
    nums = [h, i, j, k]
    if int1 in nums:
        if int2 in nums:
            if int3 in nums:
                if int4 in nums:
                    if int1 + int2 + int3 + int4 == h + i + j + k:
                        return True
    return False


def fu_hao(wen_ben):     # 对一些基础数学问题进行跳过(如除数不能为零)
    cb = ''
    if '(' in wen_ben and ')' in wen_ben:
        ind1 = wen_ben.index('(')
        ind2 = wen_ben.index(')')
        ss1 = wen_ben[:ind2]
        ss2 = wen_ben[ind1 + 1:]
        for char in ss1:
            if char in ss2:
                cb += char
        if cb[0] in ['*', '/']:
            return False
        if eval(cb) != 0 and wen_ben[ind1 - 1] != '/':
            return True
    return False


def check(list_):   # 跳过格式错误的算式
    met = ['+', '-', '*', '/']
    in_nums = [int1, int2, int3, int4]
    for n in range(0, len(list_) - 1):
        if list_[n] == '(' and (list_[n+1] == '+' or list_[n+1] == '-' or list_[n+1] == '*' or list_[n+1] == '/'):
            return False
        elif (list_[n] == '+' or list_[n] == '-' or list_[n] == '*' or list_[n] == '/') and list_[n+1] == ')':
            return False
        elif (list_[n] == '(' and list_[n+1] == ')') or (list_[n] == ')' and list_[n+1] == '('):
            return False
        elif list_[n] in met and list_[n+1] in met:
            return False
        elif list_[n] in in_nums and (list_[n+1] == '(' or list_[n+1] == ')') and list_[n+2] in in_nums:
            return False
    return True


every_method(int1, int2, int3, int4)
print('计算结束!')

以上代码基本能够满足24点游戏的需求,对验证24点起到了帮助作用

Wantz

这个人很懒,什么都没留下