24点游戏(自动生成随机数)

24点游戏是经典的纸牌益智游戏。

常见游戏规则:

   从扑克中每次取出4张牌。使用加减乘除,第一个能得出24者为赢。(其中,J代表11,Q代表12,K代表13,A代表1),按照要求编程解决24点游戏。

随机产生四个数字用python中的函数进行全排列

随机数的生成:

对于python随机数的生成,通过导入包来实现,即import  random

 

本程序中因为要生成4个(1——13)的整形数字,采用randint

对四个随机数的全排列:

对于python可以通过python的包itertools所以不用自己写递归函数实现

itertools.permutations

 

通俗地讲,就是返回可迭代对象的所有数学全排列方式。

扫描二维码关注公众号,回复: 5871758 查看本文章
listCardSet = list(set(itertools.permutations(listCardNum, 4)))#全排列4个数
return  listCardSet

 

例如:8,13,3,5产生的全排列

(8, 13, 3, 5), (13, 5, 8, 3), (5, 3, 8, 13), (13, 3, 8, 5), (3, 5, 8, 13), (3, 13, 5, 8),

 (3, 5, 13, 8), (5, 3, 13, 8), (8, 13, 5, 3), (8, 5, 13, 3), (8, 3, 5, 13), (5, 8, 3, 13),

 (13, 8, 3, 5), (13, 3, 5, 8), (8, 3, 13, 5), (5, 8, 13, 3), (3, 8, 13, 5), (5, 13, 3, 8),

 (13, 8, 5, 3), (5, 13, 8, 3), (8, 5, 3, 13), (3, 13, 8, 5), (3, 8, 5, 13), (13, 5, 3, 8)

显示:

if __name__ == "__main__":
    print("************************欢迎进入24点游戏************************")
    print("该系统会随机产生四张牌,并帮助您算出可以实现24点的所有算法")

 

代码分析:

I.需要的包:

import itertools
import random

II.随机产生数字和全排列:

def card_rand():
    for i in range(4):
        list_random.append(random.randint(1, 13))                    #list.append(obj)
    list_quanpai = list(set(itertools.permutations(list_random, 4)))#全排列4个数
    return  list_quanpai
listCard = card_rand()  # 排列组合结果
#print(listCard)#输出所有排列

III. 计算函数:
 

def card_calculate():
    for i in range(len(listCard)):
        list1_Card = listCard[i]

        cardOne = list1_Card[0]
        cardTwo = list1_Card[1]
        cardThree = list1_Card[2]
        cardFour = list1_Card[3]  # 得到四张牌的点数
        flag = False  # 标志位
        try:
            for o1 in listOperator:
                resultOne = 0  # 前两个数的运算结果
                if o1 == "+":
                    resultOne = cardOne+cardTwo
                elif o1 == "-":
                    resultOne = cardOne-cardTwo
                elif o1 == "*":
                    resultOne = cardOne*cardTwo
                elif o1 == "/":
                    resultOne = cardOne/cardTwo
                for o2 in listOperator:
                    resultTwo = 0  # 前三个数的运算结果
                    if o2 == "+":
                        resultTwo = resultOne + cardThree
                    elif o2 == "-":
                        resultTwo = resultOne - cardThree
                    elif o2 == "*":
                        resultTwo = resultOne * cardThree
                    elif o2 == "/":
                        resultTwo = resultOne / cardThree
                    for o3 in listOperator:
                        resultThree = 0  # 四个数的运算结果
                        resultFinal = 0  # 最后两个数的运算结果
                        if o3 == "+":
                            resultThree = resultTwo + cardFour
                            resultFinal = cardThree + cardFour
                        elif o3 == "-":
                            resultThree = resultTwo - cardFour
                            resultFinal = cardThree - cardFour
                        elif o3 == "*":
                            resultThree = resultTwo * cardFour
                            resultFinal = cardThree * cardFour
                        elif o3 == "/":
                            resultThree = resultTwo / cardFour
                            resultFinal = cardThree / cardFour

 如需源码请点击:

猜你喜欢

转载自blog.csdn.net/qq_41233643/article/details/89279550