




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
實(shí)驗(yàn)流程及實(shí)驗(yàn)效果(1)定義四個(gè)全局變量(2)定義一個(gè)列表存放這四個(gè)變量
(3)定義游戲答案中的運(yùn)算符號(hào)(4)定義便用戶更改四位數(shù)字重新開始游戲的函數(shù)
(5)打印出游戲的規(guī)則:
“str1="游戲說明:\n在這個(gè)游戲中,系統(tǒng)隨機(jī)發(fā)四張牌,玩家需運(yùn)用加減乘除四則運(yùn)算嘗試構(gòu)建表達(dá)式,使得運(yùn)算結(jié)果為24,每個(gè)數(shù)字均需用到,點(diǎn)擊“開始”發(fā)牌,點(diǎn)擊“完成”完成游戲,點(diǎn)擊“提示答案”查看一種答案,點(diǎn)擊“重新開始”重新發(fā)牌"”(6)定義顯示框并定義其屬性(7)定義清空函數(shù)表達(dá)式
(8)定義隨機(jī)獲取四個(gè)數(shù)的函數(shù)(9)定義將新字符添加進(jìn)表達(dá)式的函數(shù)
(10)定義制造隨機(jī)運(yùn)算法則的函數(shù),為下面函數(shù)做準(zhǔn)備(11)排除分母為0的情況,輸出100,使該式的值不可能為24
(12)定義函數(shù)計(jì)算對(duì)于一個(gè)排好序的列表中的四個(gè)數(shù)是否有可能出現(xiàn)24,為下面hard函數(shù)判斷可能性做準(zhǔn)備(13)根據(jù)不同方式進(jìn)行運(yùn)算,符合條件則將對(duì)應(yīng)的判斷標(biāo)志改為1,否則記為0(14)定義函數(shù)輸出供用戶參考的正確答案(15)將所有可能的方案列出(16)定義一個(gè)列表存放對(duì)應(yīng)的數(shù)字排序是否可行的標(biāo)志(17)定義變量用于判斷確認(rèn)是否有解決方案(18)繪制窗口(19)給窗口命名(20)定義表達(dá)式為交互式對(duì)象(21)定義顯示框并定義其屬性(22)設(shè)置窗口背景顏色、長(zhǎng)、寬(23)將Label的內(nèi)容與display關(guān)聯(lián)(24)布置按鈕(25)給出AC鍵大小、位置以及觸發(fā)的操作
(26)給出(鍵大小、位置以及觸發(fā)的操作(27)給出)鍵大小、位置以及觸發(fā)的操作(28)給出-鍵大小、位置以及觸發(fā)的操作(29)完成'(30)提示答案重新開始
顯示界面2.代碼#-*-coding:utf-8-*-
#pleaseinputyourcodehere.
fromtkinterimport*
fromrandomimportrandint
num1=randint(1,10)
num2=randint(1,10)
num3=randint(1,10)
num4=randint(1,10)
list=[]
list.append(num1)
list.append(num2)
list.append(num3)
list.append(num4)
operator=[]
foriinrange(0,3):
operator.append('no')
defchange():
globalnum1
globalnum2
globalnum3
globalnum3
num1=randint(1,10)
num2=randint(1,10)
num3=randint(1,10)
num4=randint(1,10)
globallist
list[0]=num1
list[1]=num2
list[2]=num3
list[3]=num4
get()
str1="游戲說明:\n在這個(gè)游戲中,系統(tǒng)隨機(jī)發(fā)四張牌,玩家需運(yùn)用加減乘除四則運(yùn)算嘗試構(gòu)建表達(dá)式,使得運(yùn)算結(jié)果為24,每個(gè)數(shù)字均需用到,點(diǎn)擊“開始”發(fā)牌,點(diǎn)擊“完成”完成游戲,點(diǎn)擊“提示答案”查看一種答案,點(diǎn)擊“重新開始”重新發(fā)牌"
root1=Tk()
root1.title("游戲說明")
frametext=Frame(root1,width=400,height=200,bg='green')
pagate(False)
frametext.pack(side=TOP)
t=Text(frametext)
t.pack(expand='yes',fill='both',pady=4)
t.insert(INSERT,str1)
defclear():
display.set("")
defget():
display.set(list)
defnExp(num):
exp=display.get()+num
display.set(exp)
deffunc(a,b,op,s):
if(op==1):
operator[s]='+'
return(a+b)
if(op==2):
operator[s]='-'
return(a-b)
if(op==3):
operator[s]='*'
return(a*b)
if(op==4):
#排除分母為0的情況,輸出100,使該式的值不可能為24
if(b!=0):
operator[s]='/'
return(a/b)
else:return100
defcalculate(list):
get=0
k=1
while(get==0andk<=4):
i=1
while(get==0andi<=4):
j=1
while(get==0andj<=4):
if(func(func(list[0],list[1],i,0),func(list[2],list[3],j,2),k,1)==24):
get=1
return1
j=j+1
i=i+1
k=k+1
if(get==0):return0
defhard():
mylist=[]
ele=[]
elep=[]
fori1inrange(0,4):
fori2inrange(0,4):
fori3inrange(0,4):
fori4inrange(0,4):
ele.append([i1,i2,i3,i4])
foriinrange(0,256):
forminele[i]:
forninele[i]:
if(m==n):elep.append(0)
else:elep.append(1)
if(elep[i]!=0):
mylist.append([list[ele[i][0]],list[ele[i][1]],list[ele[i][2]],list[ele[i][3]]])
possibility=[]
solution=0
i=0
while(i<256andsolution==0):
possibility.append('no')
possibility[i]=calculate(mylist[i])
if(possibility[i]==1):
display.set("提示答案:("+str(mylist[i][0])+str(operator[0])+str(mylist[i][1])+")"+operator[1]+"("+str(mylist[i][2])+operator[2]+str(mylist[i][3])+")")
solution=1
i=i+1
if(solution!=1):
display.set("無解")
defeasy():
exp=display.get()
res=eval(exp)
ifres==24:
foriinrange(len(list)):
ifstr(list[i])inexp:
display.set("True")
else:
display.set("UnvalidValue")
else:
display.set("False")
root=Tk()
root.title('24點(diǎn)游戲')
display=StringVar()
LabelA=Label(root,relief='sunken',borderwidth=4,anchor=SE)
LabelA.configure(background='white',height=4,width=50)
LabelA['textvariable']=displayLabelA.grid(row=0,column=0,columnspan=4)
Button(root,text='AC',width=10,height=3,command=clear).grid(row=1,rowspan=2)
Button(root,text='(',width=10,command=lambda:nExp('(')).grid(row=1,column=1)
Button(root,text=')',width=10,command=lambda:nExp(')')).grid(row=1,column=2)
Button(root,text='-',width=10,height=3,command=lambda:nExp('-')).grid(row=1,rowspan=2,column=3)
Button(root,text='/',width=10,command=lambda:nExp('/')).grid(row=2,column=1)
Button(root,text='*',width=10,command=lambda:nExp('*')).grid(row=2,column=2)
Button(root,text='7',width=10,command=lambda:nExp('7')).grid(row=3)
Button(root,text='8',width=10,command=lambda:nExp('8')).grid(row=3,column=1)
Button(root,text='9',width=10,command=lambda:nExp('9')).grid(row=3,column=2)
Button(root,text='+',width=10,height=3,command=lambda:nExp('+')).grid(row=3,rowspan=2,column=3)
Button(root,text='4',width=10,command=lambda:nExp('4')).grid(row=4,column=0)
Button(root,text='5',width=10,command=lambda:nExp('5')).grid(row=4,column=1)
Button(root,text='6',width=10,command=lambda:nExp('6')).grid(row=4,column=2)
Button(root,text='1',width=10,command=lambda:nExp('1')).grid(row=5,column=0)
Button(root,text='2',width=10,command=lambda:nExp('2')).grid(row=5,column=1)
Button(root,text='3',width=10,command=lambda:nExp('3')).grid(row=5,column=2)
Button(root,text='0',width=10,command=lambda:nExp('0')).grid(row=6,column=0,columnspan=2)
Button(root,text='.',width=10,command=lambda:nExp('.')).grid(row=6,column=2)
Button(root,text=
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年度法律服務(wù)團(tuán)隊(duì)聘用合同范本下載
- 2025年度音樂噴泉與音響系統(tǒng)集成項(xiàng)目合同
- 知識(shí)產(chǎn)權(quán)教育實(shí)踐活動(dòng)計(jì)劃
- 科技創(chuàng)新應(yīng)用推廣工作方案計(jì)劃
- 學(xué)期后勤保障與支持計(jì)劃
- 2025年物位儀合作協(xié)議書
- 培養(yǎng)小學(xué)生的公民道德責(zé)任計(jì)劃
- 制定年度客戶滿意度提升計(jì)劃
- 如何構(gòu)建長(zhǎng)效的職業(yè)發(fā)展體系計(jì)劃
- 校園美術(shù)文化建設(shè)方案計(jì)劃
- 2025年部編版道德與法治五年級(jí)下冊(cè)全冊(cè)教案(含教學(xué)計(jì)劃)
- 2025中國(guó)水利水電第十二工程局限公司招聘400人高頻重點(diǎn)提升(共500題)附帶答案詳解
- 《辣椒主要病蟲害》課件
- 2024年煤礦安全生產(chǎn)知識(shí)培訓(xùn)考試必答題庫及答案(共190題)
- 《軟件開發(fā)質(zhì)量管理研究的國(guó)內(nèi)外文獻(xiàn)綜述》7100字
- 《法律援助》課件
- 碳中和技術(shù)概論 課件 第1-3章 碳中和概述、太陽能、風(fēng)能
- 小兒肺炎治療與護(hù)理
- GB/T 36547-2024電化學(xué)儲(chǔ)能電站接入電網(wǎng)技術(shù)規(guī)定
- 育嬰員初級(jí)培訓(xùn)
- 學(xué)校物業(yè)管理投標(biāo)書范本
評(píng)論
0/150
提交評(píng)論