zoukankan      html  css  js  c++  java
  • 纸牌24点,Python 程序实现

    最近刚刚接触 python,又恰巧和室友玩纸牌,想到小时候玩的24点游戏,所以当做练习,写了一段代码。
    参考了一位叫GreatAnt网友的程序,但是他实现的只是一种计算结果的输出,为了看到所以计算可能的输出,我编写了下面一段代码,欢迎广大网友来此留言交流。

    #!/usr/bin/env python
    #encoding:utf8
    
    import itertools
    import random
    
    cardNum = []    # 存放随机牌组
    listSet = []    # 存放随机牌组对
    cardGroup = ()  # 调用牌组
    symbols = ["+","-","*","/"]
    cardOne = 0 ; cardTwo = 0 ; cardThr = 0 ;cardFor = 0    # 存放卡牌信息
    resultOne = 0 ; resultTwo = 0 ; resultThr = 0   # 存放运算计算结果
    cardValue = []  # 保存结果打印信息
    cardResult = []
    
    # 发牌器
    def cardFun():
        for i in range(4):
            cardNum.append(int(random.random() * 100 % 13) + 1)
        listSet = list(set(itertools.permutations(cardNum, 4)))
        return listSet         # 存放A(4,4)种排列方式的列表
    
    # 计算方法
    cardList = cardFun()
    def cardCompute():
        for i in range(len(cardList)):
            cardGroup = cardList[i]
            cardOne = cardGroup[0]
            cardTwo = cardGroup[1]
            cardThr = cardGroup[2]
            cardFor = cardGroup[3]
            flag = False
            # 下面的循环运算体系会有数学上逻辑上的报错,所以用try检测
            try:
                for s1 in symbols:
                    resultOne = 0
                    if s1 == "+":
                        resultOne = cardOne + cardTwo
                    elif s1 == "-":
                        resultOne = cardOne - cardTwo
                    elif s1 == "*":
                        resultOne = cardOne * cardTwo
                    elif s1 == "/":
                        resultOne = cardOne / cardTwo
                    for s2 in symbols:
                        resultTwo = 0
                        if s2 == "+":
                            resultTwo = resultOne + cardThr
                        elif s2 == "-":
                            resultTwo = resultOne - cardThr
                        elif s2 == "*":
                            resultTwo = resultOne * cardThr
                        elif s2 == "/":
                            resultTwo = resultOne / cardThr
                        for s3 in symbols:
                            resultThr =0 ; resultelse = 0
                            if s3 == "+":
                                resultThr = resultTwo + cardFor
                                resultelse = cardThr + cardFor
                            elif s3 == "-":
                                resultThr = resultTwo - cardFor
                                resultelse = cardThr - cardFor
                            elif s3 == "*":
                                resultThr = resultTwo * cardFor
                                resultelse = cardThr * cardFor
                            elif s3 == "/":
                                resultThr = resultTwo / cardFor
                                resultelse = cardThr / cardFor
    
                            # 判断最终结果是否为24
                            if resultThr == 24:
                                cardValue.append("((%s %s %s) %s %s ) %s %s = 24" % (cardOne,s1,cardTwo,s2,cardThr,s3,cardFor))
                                flag = True
                            # 括号与括号的运算
                            elif resultThr != 24 and 24 % resultOne == 0:
                                for s4 in symbols:
                                    resultThr = 0
                                    if s4 == "+":
                                        resultThr = resultOne + resultelse
                                    elif s4 == "-":
                                        resultThr = resultOne - resultelse
                                    elif s4 == "*":
                                        resultThr = resultOne * resultelse
                                    elif s4 == "/":
                                        resultThr = resultOne / resultelse
                                    if resultThr == 24:
                                        cardValue.append("(%s %s %s) %s (%s %s %s) = 24" % (cardOne,s1,cardTwo,s4,cardThr,s3,cardFor))
                                        flag = True
                                    if flag:
                                        break
                        # 如果得到结果,就退出3次运算的循环
                            if flag:
                                break
                        if flag:
                            break
                    if flag:
                        break
            except ZeroDivisionError:
                pass
    
        cardResult = set(cardValue)
        return cardResult
    
    # 执行主体
    if __name__ == "__main__":
        Compute = cardCompute()
        print("你手上的卡牌为:%s %s %s %s" % (cardList[0][0],cardList[0][1],cardList[0][2],cardList[0][3]))
        print("这组卡牌共有 %s 种算法" % (len(Compute)))
        print("---" * 10)
        for i in Compute:
            print(i)

    此程序的实现没什么算法上的设计,就是用穷举法简单粗暴的来解决,因为是初学,所以主要是练习数据类型的应用,欢迎指错。

  • 相关阅读:
    全国最全的省,市,县,电话号前缀,邮编数据
    数组的高级用法
    Maven Project configuration is not up-to-date with pom.xml错误解决方法
    HighCharts开发说明
    Java基础复习之二:运算符,键盘录入,流程控制语句,if语句,三元运算
    Java基础复习之一篇:关健字,标识符,注释,常量,进制转换,变量,数据类型,数据类型转换
    ehcache 缓存技术
    Write operations are not allowed in read-only mode
    浮点运算与boost.multiprecision
    关于OpenCASCADE数组序列的起始值
  • 原文地址:https://www.cnblogs.com/nimo97/p/9704767.html
Copyright © 2011-2022 走看看