如何用python攻克24点游戏

  24点游戏玩法为:从一副扑克中抽取4张牌,对4张牌使用加减乘除中的任何方法,使计算结果为24。

   基本思路是枚举计算顺序,首先我们需要四个数字,whatever,搞一个随机生成

new_cards = [rand_card() for _ in range(4)] 
print('我抽到的牌是:{}'.format(new_cards))


结果:
我抽到的牌是:['2', '7', '6', '2']

  好,那么此刻我们就得到了一串数字,接下来我们知道根据游戏规则,四个数字总共有4*3*2*1=24种排列,然后['+','-','*','/']这四种符号可以重复,总共有4*4*4=64种组合,先用代码实现如下:

from itertools import product
from itertools import permutations 
operators = ['+','-','*','/']
for p in product(operators,repeat = 3):  #product函数可以算笛卡尔乘积,repeat=3 相当于里面放三个operators
    print(p)

cards= ['2', '7', '6', '2']
for i in permutations(cards):  #permutations函数可以对列表进行排列
    print(i)

===结果:
('+', '+', '+')
('+', '+', '-')
('+', '+', '*')
('+', '+', '/')
('+', '-', '+')
('+', '-', '-')
('+', '-', '*')
('+', '-', '/')
('+', '*', '+')
('+', '*', '-')
('+', '*', '*')
('+', '*', '/')
('+', '/', '+')
('+', '/', '-')
....

('2', '7', '6', '2')
('2', '7', '2', '6')
('2', '6', '7', '2')
('2', '6', '2', '7')
('2', '2', '7', '6')
('2', '2', '6', '7')
('7', '2', '6', '2')
('7', '2', '2', '6')
('7', '6', '2', '2')
('7', '6', '2', '2')
('7', '2', '2', '6')
('7', '2', '6', '2')
('6', '2', '7', '2')
('6', '2', '2', '7')
('6', '7', '2', '2')
('6', '7', '2', '2')
('6', '2', '2', '7')
('6', '2', '7', '2')
.....

先不加括弧的情况下,试试将这两组数据合并

def get_all_operation_combine(cards):
    c1,c2,c3,c4 = cards
    operators = ['+','-','*','/']
    expressions = []
    for p in product(operators,repeat = len(cards)-1):
        op1,op2,op3 = p
        expressions.append('{}{}{}{}{}{}{}'.format(c1,op1,c2,op2,c3,op3,c4))
    return expressions 

def get_all_operation_combine_with_number_exchange(cards):
    all_result = []
    for p in permutations(cards):
        all_result += get_all_operation_combine(p)
    return all_result

get_all_operation_combine_with_number_exchange(cards)

结果:
['2+7+6+2',
 '2+7+6-2',
 '2+7+6*2',
 '2+7+6/2',
 '2+7-6+2',
 '2+7-6-2',
 '2+7-6*2',
 '2+7-6/2',
 '2+7*6+2',
 '2+7*6-2',
 '2+7*6*2',
 '2+7*6/2',
 '2+7/6+2',
 '2+7/6-2',
 '2+7/6*2',
 '2+7/6/2',
 '2-7+6+2',
 '2-7+6-2',
 '2-7+6*2',
 '2-7+6/2',
....

现在 我们就可以通过 eval函数对这些字符串进行计算,得到一个简单的结果:

def get_answer(cards):
    target =24
    for exp in get_all_operation_combine_with_number_exchange(cards):
        if eval(exp)== target:
            print(exp)
cards= [7, 13, 14, 2]
get_answer(cards)

结果:
13/7*14-2
13*14/7-2
13*2-14/7
14/7*13-2
14*13/7-2
2*13-14/7

  以上已把24点计算问题走了一遍,最后也是相对最难的一个环节,就是加括弧,那么如何加入括弧呢?代码如下:

def add_brace(numbers):
    if len(numbers)< 2:return [numbers]
    if len(numbers) ==2 :return [['('+str(numbers[0])]+numbers[1:-1]+[str(numbers[1])+')']]
    results =[]
    
    for j in range(1,len(numbers)):
        prefixs = add_brace(numbers[:j])
        tails = add_brace(numbers[j:])
        for p in prefixs:
            for t in tails:
                with_around_brach = ['(' + p[0]] +p[1:] +t[:-1] +[t[-1] +')']
                results.append(with_around_brach)
    return results
#add_brace1("1 2 3 4".split())
add_brace(['1','2','3','4'])

结果:
[['(1', '(2', '(3', '4)))'],
 ['(1', '((2', '3)', '4))'],
 ['((1', '2)', '(3', '4))'],
 ['((1', '(2', '3))', '4)'],
 ['(((1', '2)', '3)', '4)']]

这里可以看到四个数字,在不改变顺序的情况下,总共有五种加括弧的方式,那么这个加括弧的函数也写好了,最后就是将以上的代码组合起来,合成一个全新的计算24点的程序。

计算顺序:所有数字组合-->每个组合五种括弧方式-->加入64种符合组合-->合成并 利用eval函数计算24点

代码如下:

def finally_count(cards):
    target =24
    for i in permutations(cards):
        for j in product(operators,repeat = len(cards)-1):
            add_brace_number = add_brace(list(i))
            for t in add_brace_number:
                try:
                    cc =join_operators_number(j,t)
                    if eval(cc)==target:print(cc)
                except ZeroDivisionError:
                    continue
                    
                
new_cards = [rand_card() for _ in range(4)]
print('我抽到的牌是:{}'.format(new_cards))          
finally_count(new_cards)

结果:
我抽到的牌是:['11', '3', '1', '6']
(((11+1)/3)*6)
((11+1)/(3/6))
(((11-1)*3)-6)
((11+1)*(6/3))
(((11+1)*6)/3)
((3*(11-1))-6)
(((1+11)/3)*6)
((1+11)/(3/6))
((1+11)*(6/3))
(((1+11)*6)/3)
(6*((11+1)/3))
((6*(11+1))/3)
((6/3)*(11+1))
(6/(3/(11+1)))
((6/3)*(1+11))
(6/(3/(1+11)))
(6*((1+11)/3))
((6*(1+11))/3)

  以上就是24点游戏的python求解方式

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值