导航:首页 > 编程语言 > python24点游戏程序

python24点游戏程序

发布时间:2022-08-21 04:08:33

❶ 求大神!!python程序设计 一个24点纸牌游戏。如图所示,最好充分利用提示!

你好:
最好是自己写一点;
不会的部分,再拿出来询问!

❷ python想做一个24点游戏,请大神帮我做一个。真的感谢。做出来的。我可以给你钱。需要程序 方

#!python
#vim:fileencoding=utf-8
#=weibo.com/niio=
#2016-04-22
#Theme:Python24点智商矫正工具
#Memo:python2.7/开箱即用/玩得开薰XD
######################

importrandom
defbracket_exp(op,a,b):
return(op,a,b)

defremove_op(ops,op):
tmp=list(ops)
tmp.remove(op)
returntmp

defremove_nums(nums,number_combine):
tmp=list(nums)
foriinnumber_combine:
tmp.remove(i)
returntmp

defappend(res_nums,exp):
tmp=list(res_nums)
tmp.append(exp)
returntmp


defevalexp(e):
ifnotisinstance(e,tuple):
returnint(e)
else:
e1=evalexp(e[1])
e2=evalexp(e[2])
ife1==Noneore2==None:
returnNone
ife[0]=='+':
returne1+e2
elife[0]=='-':
returne1-e2
elife[0]=='*':
returne1*e2
elife[0]=='/':
ife2!=0ande1%e2==0:
returne1/e2
else:
returnNone
else:
None

deffmtexp(e):
ifnotisinstance(e,tuple):
returne
else:
e1=fmtexp(e[1])
e2=fmtexp(e[2])
ife[0]=='+':
return'(%s+%s)'%(e1,e2)
elife[0]=='-':
return'(%s-%s)'%(e1,e2)
elife[0]=='*':
return'(%s*%s)'%(e1,e2)
elife[0]=='/':
return'(%s/%s)'%(e1,e2)
else:
None

#r=evalexp(('+','1',('*','4',('-','6','8'))))
#r=fmtexp(('+','1',('*','4',('-','6','8'))))

defiter_all_exp(ops,nums,target):
iflen(nums)==1:
r=evalexp(nums[0])
ifrandabs(r-target)<0.001:
printfmtexp(nums[0]),'='+str(target)
return
fornumber_combineincombinations(nums,2):
foropinops:
res_ops=remove_op(ops,op)
res_nums=remove_nums(nums,number_combine)

exp=bracket_exp(op,number_combine[0],number_combine[1])
iter_all_exp(res_ops,append(res_nums,exp),target)

ifop=='-'orop=='/':
exp=bracket_exp(op,number_combine[1],number_combine[0])
iter_all_exp(res_ops,append(res_nums,exp),target)
print"正在出题"
q=random.sample([1,2,3,4,5,6,7,8,9],4)

printstr(q[0])+"_"+str(q[1])+"_"+str(q[2])+"_"+str(q[3])+"=24"

show=raw_input("题目不一定有解喔!,回车看答案")
ifshow=="":
iter_all_exp(["+","-","/","*"],[q[0],q[1],q[2],q[3]],24)
else:
print""

❸ 24点纸牌游戏的开发 python 各路大神我需要帮助

很久之前自己写的了,用的就是高级一点的穷举,还挺快的。
附带一个gui
求给分啊
两个文件,cui负责算数gui是界面,亲测可运行的

**************************************cui_24point.py
__author__ = 'Administrator'

add = lambda a,b:a+b
minus = lambda a,b:a-b
mul = lambda a,b:a*b
div = lambda a,b:a/b

caldict = {add:"+", minus:"-", mul:"*", div:"/"}

sortlist = [[a,b,c,d] for a in range(4) for b in range(4) for c in range(4) for d in range(4) if a != b != c != d and a != c and b != d and a != d]

class leaf:
def __init__(self, numlist, layer1, layer2, layer3, ans):
self.numlist = map(lambda a:float(a), numlist)
self.layer1 = layer1
self.layer2 = layer2
self.layer3 = layer3
self.ans = ans
def __str__(self):
return "((%s%s%s)%s%s)%s%s=%s" %(self.numlist[0], caldict[self.layer1], self.numlist[1], caldict[self.layer2], self.numlist[2], caldict[self.layer3], self.numlist[3] ,self.ans)

def tree(numlist):
layer1 = "Null"
layer2 = "Null"
layer3 = "Null"
for c1 in [add, minus, mul, div]:
layer1 = c1
for c2 in [add, minus, mul, div]:
layer2 = c2
for c3 in [add, minus, mul, div]:
layer3 = c3
if c3(c2(c1(numlist[0], numlist[1]), numlist[2]), numlist[3]) == 24:
return leaf(numlist, layer1, layer2, layer3, 24)

class Turn():
def __init__(self, numlist):
if len(numlist) == 4:
self.numlist = numlist
def calculate(self):
anslist = []
for i in sortlist:
templist = [self.numlist[i[0]], self.numlist[i[1]], self.numlist[i[2]], self.numlist[i[3]]]
anslist.append(tree(templist))
return anslist

def calc(templist):
ans = Turn(templist).calculate()
ans = [i for i in ans if i != None]
for i in ans:
print i
return ans

if __name__ == "__main__":
templist = []
for i in range(4):
templist.append(int(raw_input("Input No.%s number \n" %i)))
calc(templist)

*****************************************gui.py
# -*- coding: -UTF-8 -*-
__author__ = 'Administrator'

from Tkinter import *
import cui_24point

root = Tk()
root.geometry("800x400+400+400")
root.title("24点计算程式")

numlist = []
ansVar = StringVar()

def initvariable():
for i in range(4):
numlist.append(StringVar())

def drawframe():
entryframe = Frame(root, width = 300, height = 100)
for i in range(4):
Label(entryframe, text = str(i + 1)).grid(row = 1, column = i)
Entry(entryframe, textvariable = numlist[i]).grid(row = 2, column = i)

entryframe.pack()

ansframe = Frame(root, width = 100, height = 66)
anslistbox = Listbox(ansframe, width = 50, listvariable = ansVar)
ansscrollbar = Scrollbar(ansframe, command = anslistbox.yview)
anslistbox.configure(yscrollcommand = ansscrollbar.set)

anslistbox.pack(side = RIGHT)
ansscrollbar.pack(side = LEFT, fill = Y)
ansframe.pack()

def calculate():
templist = map(lambda i:float(i.get()), numlist)
for i in cui_24point.calc(templist):
anslistbox.insert(END, i)
anslistbox.insert(END, "********************")

Button(text = "计算", command = calculate).pack()

initvariable()
drawframe()

❹ python 24点游戏开发,求大神!!!!!!

这个就是一个排列组合 数字的排列组合,字母的排列组合 你给我留联系方式,我邮箱给你 网络云自己下

❺ 谁能用Python编一个24点小游戏吗谢谢!

记得采纳

❻ Python编24点游戏

#!/usr/bin/python
#coding:utf8
'''
原始方法编24点游戏
'''

importitertools
importrandom

numlist=[random.randint(1,13)foriinrange(4)] #随机4个数
printnumlist

nlist=[]
[nlist.append(nl)fornlinlist(itertools.permutations(numlist))ifnlnotinnlist] #4个数排列组合,并在有重复数字时对组合去重
#printnlist

option=['+','-','*','/']
olist=list(itertools.proct(option,repeat=3)) #操作符重复组合3位
#printolist

retlist=[[str(nl[0])+'*1.0']+[ol[0]]+[str(nl[1])+'*1.0']+[ol[1]]+[str(nl[2])+'*1.0']+[ol[2]]+[str(nl[3])+'*1.0']fornlinnlistforolinolist] #拼凑4个数和3个操作符
#printretlist

#括号的位置
#(0,3)(0,5)
#(2,5)(2,7)
#(4,7)
#(0,3)和(4,7)
lastlist=[]
forretinretlist:
if('*'notinretand'/'notinret)or('*'inretand'/'inret):
lastlist.append(''.join(ret))
else:
lastlist.append(''.join(['(']+ret[:3]+[')']+ret[3:]))
lastlist.append(''.join(['(']+ret[:5]+[')']+ret[5:]))
lastlist.append(''.join(ret[:2]+['(']+ret[2:5]+[')']+ret[5:]))
lastlist.append(''.join(ret[:2]+['(']+ret[2:7]+[')']))
lastlist.append(''.join(ret[:4]+['(']+ret[4:7]+[')']))
lastlist.append(''.join(['(']+ret[:3]+[')']+ret[3:4]+['(']+ret[4:7]+[')']))
#printlastlist

i=0
forllinlastlist:
try:
ifeval(ll)==24:#计算,每位数*1.0,是防止出现12/5=2的情况
printll.replace('*1.0','')+'=24'#去掉'*1.0'
else:
i+=1
exceptZeroDivisionError,e:
#print'除于0错误:'+ll.replace('*1.0','')#表达式中有除于0的
i+=1
continue

ifi==len(lastlist):
print'nooutput!'

❼ 求python写的24点游戏,带图形用户界面最好

写了个极其粗鲁暴力的草稿版穷举法,生成表达式。当eval执行后的结果是24的话,判定成功。

目前可见的缺陷是,对于相似结果,没想到好办法来过滤。求高手指教。


代码如下,

#coding:utf-8

'''
计算24点
tested@Windows7Pro64-bitENU
'''


try:
importTkinterasTK
except:
importtkinterasTK

fromrandomimportrandint


#GUIEVENT
defEntryUpdate(number_list):
'DisplaythenumberlistinEntry'
inputbox.delete('0',TK.END)
inputbox.insert('0',','.join([str(i)foriinnumber_list]))

defScrolledTextClear():
'ClearcontentinST'
outputbox.delete('1.0',TK.END)

defScrolledTextAppend(message):
'AppendcontentinST'
outputbox.insert(TK.END,message)

#defSetTriggerState(status=1):
#'Setbuttonstatus'
#trigger.config(state='disabled')ifstatus==0elsetrigger.config(state='active')

defMessageProcessor():
'Dealwithmessagequeue'
iflen(MessageQueue)>0:
ret=str(MessageQueue.pop(0))
ScrolledTextAppend('{} '.format(ret))
root.after(30,MessageProcessor)

#CalculatorEvent
defDivisionVerification(number1,number2):
try:
ifnumber1%number2==0:
return0
except:
try:
ifnumber2%number1==0:
return1
except:
return2

defExpression(number1,number2,operator):
'Returntheexpression(s)'
ifoperator=='1':
return['({0}{2}{1})'.format(number1,number2,operator_dict[operator]),
'({1}{2}{0})'.format(number1,number2,operator_dict[operator])]
elifoperator=='3':
ret=DivisionVerification(number1,number2)
ifret==0:
return['({0}{2}{1})'.format(number1,number2,operator_dict[operator])]
ifret==1:
return['({1}{2}{0})'.format(number1,number2,operator_dict[operator])]
ifret==2:
returnNone
else:
return['({0}{2}{1})'.format(number1,number2,operator_dict[operator])]

defJudgement(operation_expression):
'`target`.'
try:
returnTrueifeval(ur'{}'.format(operation_expression))==targetelseFalse
except:
returnFalse

defEnumerateExpression(number_list):
'''
[A,B,C,D]

1.CalculateAandB->[R,C,D]
2.Threescenarios:
type1:
R,CandD
R,DandC
type2:
RandC,D
'''

strategy=[[[(0,1),2,3],
[(0,1),3,2],
[(0,2),1,3],
[(0,2),3,1],
[(0,3),1,2],
[(0,3),2,1],
[(1,2),0,3],
[(1,2),3,0],
[(2,3),0,1],
[(2,3),1,0]],
[[(0,1),(2,3)],
[(0,2),(1,3)],
[(0,3),(1,2)]]]

#type1
foriteminstrategy[0]:
ds=[number_list[item[0][0]],number_list[item[0][1]],number_list[item[1]],number_list[item[2]]]
foriinrange(4):
try:
forsub_exp_phase_oneinExpression(ds[0],ds[1],str(i)):
forjinrange(4):
try:
forsub_exp_phase_twoinExpression(sub_exp_phase_one,ds[2],str(j)):
forkinrange(4):
try:
forsub_exp_phase_threeinExpression(sub_exp_phase_two,ds[3],str(k)):
ifJudgement(sub_exp_phase_three)isTrue:
MessageQueue.append(sub_exp_phase_three)
except:
continue
except:
continue
except:
continue

#type2
foriteminstrategy[1]:
ds=[number_list[item[0][0]],number_list[item[0][1]]],[number_list[item[1][0]],number_list[item[1][1]]]
foriinrange(4):
try:
forsub_exp_leftinExpression(ds[0][0],ds[0][1],str(i)):
forjinrange(4):
try:
forsub_exp_rightinExpression(ds[1][0],ds[1][1],str(j)):
forkinrange(4):
try:
forsub_exp_finalinExpression(sub_exp_left,sub_exp_right,str(k)):
ifJudgement(sub_exp_final)isTrue:
MessageQueue.append(sub_exp_final)
except:
continue
except:
continue
except:
continue

defExecutor():
#preparedata
input_data=[]
foriinrange(4):
input_data.append(randint(minimum_number,maximum_number))
EntryUpdate(input_data)

#mainfunction
ScrolledTextClear()
EnumerateExpression(input_data)
iflen(MessageQueue)>0:
MessageQueue.append('{} Done'.format('-'*32))
else:
MessageQueue.append(str(input_data))
MessageQueue.append('Nothing!')

#settarget
target=24
minimum_number=1
maximum_number=13
operator_dict={
'0':'+',
'1':'-',
'2':'*',
'3':'/',}

#Generatewindow
root=TK.Tk()
root.attributes('-topmost',1)
root.attributes('-alpha',0.85)
root.geometry('+{}+{}'.format(root.winfo_screenwidth()/8,root.winfo_screenheight()/8))
root.title('Generator->24')

#Addoutputbox,
outputbox=ST(root,
width=35,height=20,
font=('CourierNew',13),
fg='yellow',bg='black')
outputbox.pack(expand=1,fill='both')

#Addinputbox,
inputbox=TK.Entry(root,bd=2,bg='yellow')
inputbox.pack(side=TK.LEFT,expand=1,fill='x')

#Addbutton,
trigger=TK.Button(root,
text='Go!',
command=Executor)
trigger.pack(side=TK.RIGHT,expand=1,fill='x')

#.
MessageQueue=[]
root.after(30,MessageProcessor)

#Displaywindow
TK.mainloop()

❽ 求python写的24点游戏,带GUI界面

#!/usr/bin/python
#coding:utf8
'''
介绍:24点游戏
'''

importitertools,random

numlist=[random.randint(1,13)foriinrange(4)]#随机4个数
printnumlist

nlist=[]
[nlist.append(nl)fornlinlist(itertools.permutations(numlist))ifnlnotinnlist] #4个数排列组合,并在有重复数字时对组合去重
#printnlist

option=['+','-','*','/']
olist=list(itertools.proct(option,repeat=3)) #操作符重复组合3位
#printolist

retlist=[str(nl[0])+'*1.0'+ol[0]+str(nl[1])+'*1.0'+ol[1]+str(nl[2])+'*1.0'+ol[2]+str(nl[3])+'*1.0'fornlinnlistforolinolist] #拼凑4个数和3个操作符
#printretlist

forrlinretlist:
ifeval(rl)==24: #计算,每位数*1.0,是防止出现12/5=2的情况
printrl.replace('*1.0','')+'=24' #去掉'*1.0'

运行结果:
[2,11,6,10]
11+6/2+10=24
11+10+6/2=24
6/2+11+10=24
6/2+10+11=24
10+11+6/2=24
10+6/2+11=24
[Finishedin0.3s]

❾ Python编24点游戏,重新开始怎么编

从图片上看,就是重新生成4个数据。不知道这个程序对应的卡牌是怎么选择的。。。猜测可以用random.randint来生成1到13之间的某个数字,然后贴对应的图片。

阅读全文

与python24点游戏程序相关的资料

热点内容
爱上北斗星男友在哪个app上看 浏览:413
主力散户派发源码 浏览:663
linux如何修复服务器时间 浏览:55
荣县优途网约车app叫什么 浏览:472
百姓网app截图是什么意思 浏览:222
php如何嵌入html 浏览:809
解压专家怎么传输 浏览:743
如何共享服务器的网络连接 浏览:132
程序员简易表白代码 浏览:166
什么是无线加密狗 浏览:62
国家反诈中心app为什么会弹出 浏览:67
cad压缩图打印 浏览:102
网页打开速度与服务器有什么关系 浏览:863
android开发技术文档 浏览:65
32单片机写程序 浏览:49
三星双清无命令 浏览:837
汉寿小程序源码 浏览:344
易助erp云服务器 浏览:532
修改本地账户管理员文件夹 浏览:419
python爬虫工程师招聘 浏览:285