纸牌24点,Python程序实现
来源:互联网 发布:淘宝店如何开通直通车 编辑:程序博客网 时间:2024/05/18 23:55
最近刚刚接触python,又恰巧和室友玩纸牌,想到小时候玩的24点游戏,所以当做练习,写了一段代码。
参考了一位叫GreatAnt网友的程序,但是他实现的只是一种计算结果的输出,为了看到所以计算可能的输出,我编写了下面一段代码,欢迎广大网友来此留言交流。
#!/usr/bin/env python#encoding:utf8import itertoolsimport randomcardNum = [] # 存放随机牌组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)
此程序的实现没什么算法上的设计,就是用穷举法简单粗暴的来解决,因为是初学,所以主要是练习数据类型的应用,欢迎指错。
阅读全文
0 0
- 纸牌24点,Python程序实现
- Java实现24点纸牌游戏
- 24点纸牌游戏
- 24点纸牌游戏计算
- 24点纸牌游戏计算器
- 24点纸牌益智游戏
- 算24点纸牌游戏【破代码】
- C语言-纸牌计算24点小游戏
- 24点纸牌游戏 c语言
- java ----实现简单的21点纸牌游戏
- 24 点 Python 的实现
- Python实现24点游戏
- 队、栈实现纸牌游戏—C—python
- 纸牌发牌程序,数据结构
- 均分纸牌pascal程序
- python 的算24点程序
- python实现的某网页模拟点赞程序
- C++21点纸牌游戏
- Python3爬取妹子图——爬虫&下载网络图片
- 设计模式(8)——组合 Composite
- css总结
- 习题5.8
- 在eclipse中设置编码自动补全功能
- 纸牌24点,Python程序实现
- 20171103
- 回流与重绘
- 菱形
- eclipse未正常退出,下次启动Initializing Java Tooling:(1%)
- Java学习——循环结构
- KNN实战——约会网站配对效果判定
- 关于类的构造函数
- 设计模式(9)——装饰 Decorator