欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python實(shí)現(xiàn)數(shù)字華容道

 更新時(shí)間:2021年04月08日 08:30:41   作者:#Qss#  
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)數(shù)字華容道,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

制作Python數(shù)字華容道(可選擇關(guān)卡),供大家參考,具體內(nèi)容如下

由于比賽需要,我這邊制作了一份數(shù)字華容道,內(nèi)含有3,4,5階的數(shù)字華容道,開(kāi)頭在殼窗口內(nèi)選擇,運(yùn)用了隨機(jī)數(shù)模塊(random)和圖形化用戶(hù)界面(tkinter)

下面是程序完整代碼

# coding:utf-8 #

"""
#============================================================
作者:@Qss
2021年3月20日起草
2021年3月21日完工
2021年3月23日一次優(yōu)化完成
2021年3月31日完成二次優(yōu)化(關(guān)卡設(shè)計(jì))
二次優(yōu)化待解決漏洞:設(shè)計(jì)關(guān)卡后窗口不能自動(dòng)顯示,需手動(dòng)切換
2021年4月1日三次優(yōu)化完成,成功解決二次優(yōu)化bug
#============================================================
"""
from random import *    #導(dǎo)入隨機(jī)數(shù)模塊
from tkinter import *   #導(dǎo)入圖形化用戶(hù)界面模塊
step_number = 0     #設(shè)置步數(shù)的變量,初始值為0
difficulty = int(input('請(qǐng)輸入數(shù)字華容道列數(shù)(3/4/5):'))
def Button_Click_1(x,y):      #按鈕點(diǎn)擊事件函數(shù)
        """聲明空白按鈕行列號(hào)和步數(shù)的變量為全局變量"""
        global row_of_space  
        global col_of_space    
        global step_number
        """判斷判斷點(diǎn)擊按鈕旁是否為空白按鈕,是則交換位置"""
        if abs(x-row_of_space) + abs(y-col_of_space) == 1:  
            step_number += 1    #將步數(shù)賦值
            label_step_number['text'] = '步數(shù):' + str(step_number)  #將步數(shù)變量導(dǎo)入label控件
            """交換按鈕位置"""
            buttons[row_of_space,col_of_space]['text'] = buttons[x,y]['text']
            buttons[x,y]['text']  = ' '  
            row_of_space = x            
            col_of_space = y
            n = 0   
            for row in range(3):
                for col in range(3):
                    """對(duì)比所有按鈕序列是否正確,不正確則跳出函數(shù)"""
                    if buttons[row,col]['text'] != numbers[n]:  
                        return
                    n += 1
            """所有按鈕判斷完畢贏得游戲勝利"""
            label_welcomes['text'] = '你贏了'

def Button_Click_2(x,y):      #按鈕點(diǎn)擊事件函數(shù)
        """聲明空白按鈕行列號(hào)和步數(shù)的變量為全局變量"""
        global row_of_space  
        global col_of_space    
        global step_number
        """判斷判斷點(diǎn)擊按鈕旁是否為空白按鈕,是則交換位置"""
        if abs(x-row_of_space) + abs(y-col_of_space) == 1:  
            step_number += 1    #將步數(shù)賦值
            label_step_number['text'] = '步數(shù):' + str(step_number)  #將步數(shù)變量導(dǎo)入label控件
            """交換按鈕位置"""
            buttons[row_of_space,col_of_space]['text'] = buttons[x,y]['text']
            buttons[x,y]['text']  = ' '  
            row_of_space = x            
            col_of_space = y
            n = 0   
            for row in range(4):
                for col in range(4):
                    """對(duì)比所有按鈕序列是否正確,不正確則跳出函數(shù)"""
                    if buttons[row,col]['text'] != numbers[n]:  
                        return
                    n += 1
            """所有按鈕判斷完畢贏得游戲勝利"""
            label_welcomes['text'] = '你贏了'

def Button_Click_3(x,y):      #按鈕點(diǎn)擊事件函數(shù)
        """聲明空白按鈕行列號(hào)和步數(shù)的變量為全局變量"""
        global row_of_space  
        global col_of_space    
        global step_number
        """判斷判斷點(diǎn)擊按鈕旁是否為空白按鈕,是則交換位置"""
        if abs(x-row_of_space) + abs(y-col_of_space) == 1:  
            step_number += 1    #將步數(shù)賦值
            label_step_number['text'] = '步數(shù):' + str(step_number)  #將步數(shù)變量導(dǎo)入label控件
            """交換按鈕位置"""
            buttons[row_of_space,col_of_space]['text'] = buttons[x,y]['text']
            buttons[x,y]['text']  = ' '  
            row_of_space = x            
            col_of_space = y
            n = 0   
            for row in range(5):
                for col in range(5):
                    """對(duì)比所有按鈕序列是否正確,不正確則跳出函數(shù)"""
                    if buttons[row,col]['text'] != numbers[n]:  
                        return
                    n += 1
            """所有按鈕判斷完畢贏得游戲勝利"""
            label_welcomes['text'] = '你贏了'

"""創(chuàng)建華容道游戲窗口"""
root = Tk()                      #創(chuàng)建圖形化用戶(hù)界面實(shí)例
root.title('數(shù)字華容道')         #設(shè)置窗口標(biāo)題
root.geometry("400x400")    #將窗口大小設(shè)為高400寬400
root.configure(bg = 'black') #將窗口背景設(shè)為黑色
root.resizable(width = False,height = False)    #設(shè)置窗口為不可拉伸
"""設(shè)置歡迎語(yǔ)的label控件"""
label_welcomes = Label(root,text = '歡迎來(lái)到數(shù)字華容道',bg = 'black',fg = 'red',font = ("Arial",13))    #設(shè)置label控件的屬性
label_welcomes.place(x = 20,y = 10,width = 250,height = 40)  #設(shè)置label控件位置
"""設(shè)置顯示操作方式的label控件"""
label_operation = Label(root,text = '單擊數(shù)字',bg = 'black',fg = 'white',font = ("Arial",10))
label_operation.place(x = 3,y = 40,width = 50,height = 30)
label_operation_2 = Label(root,text = '移動(dòng)方塊',bg = 'black',fg = 'white',font = ("Arial",10))
label_operation_2.place(x = 3,y = 60,width = 50,height = 30)
"""設(shè)置顯示步數(shù)的label控件"""
label_step_number = Label(root,text = '步數(shù):' + str(step_number),bg = 'black',fg = 'yellow',font = ("Arial",10))
label_step_number.place(x = 3,y = 20,width = 50,height = 30)

if difficulty == 3:
    root.attributes("-topmost", True)
    row_of_space = 0    #存放空白按鈕的行號(hào) 
    col_of_space = 0    #存放空白按鈕的列號(hào)
    buttons = {}      #存放數(shù)字按鈕的數(shù)組
    numbers = ['1','2','3','4','5','6','7','8',' '] #所有數(shù)字文本的列表 
    shuffle(numbers)     #打亂數(shù)字列表中的數(shù)字順序
    """制造數(shù)字華容道方陣"""
    for row in range(3): 
        for col in range(3):
            """創(chuàng)建數(shù)字按鈕,并將行列號(hào)傳入該按鈕的點(diǎn)擊事件函數(shù)"""
            button = Button(root,command = lambda x = row,y = col:Button_Click_1(x,y),bg = 'black',fg = 'green',font = ("Arial",35))
            buttons[row,col] = button   #將按鈕導(dǎo)入數(shù)組
            button['text'] = numbers.pop()    #設(shè)置按鈕上的文本
            button.place(x = 60 + col * 60,y = 60 + row * 60,width = 50,height = 50)    #設(shè)置數(shù)字按鈕大小
            if button['text'] == ' ':        #判斷是否為空白按鈕,如果是,則記錄到空白按鈕行列號(hào)變量
                row_of_space = row
                col_of_space = col
    numbers = ['1','2','3','4','5','6','7','8',' ']   #還原數(shù)字列表 
    root.mainloop() #顯示窗口
elif difficulty == 4:
    root.attributes("-topmost", True)
    row_of_space = 0    #存放空白按鈕的行號(hào) 
    col_of_space = 0    #存放空白按鈕的列號(hào)
    buttons = {}      #存放數(shù)字按鈕的數(shù)組
    numbers = ['1','2','3','4','5','6','7','8','9','10','11','12','13','14','15',' '] #所有數(shù)字文本的列表 
    shuffle(numbers)     #打亂數(shù)字列表中的數(shù)字順序
    """制造數(shù)字華容道方陣"""
    for row in range(4): 
        for col in range(4):
            """創(chuàng)建數(shù)字按鈕,并將行列號(hào)傳入該按鈕的點(diǎn)擊事件函數(shù)"""
            button = Button(root,command = lambda x = row,y = col:Button_Click_2(x,y),bg = 'black',fg = 'green',font = ("Arial",35))
            buttons[row,col] = button   #將按鈕導(dǎo)入數(shù)組
            button['text'] = numbers.pop()    #設(shè)置按鈕上的文本
            button.place(x = 60 + col * 60,y = 60 + row * 60,width = 50,height = 50)    #設(shè)置數(shù)字按鈕大小
            if button['text'] == ' ':        #判斷是否為空白按鈕,如果是,則記錄到空白按鈕行列號(hào)變量
                row_of_space = row
                col_of_space = col
    numbers = ['1','2','3','4','5','6','7','8','9','10','11','12','13','14','15',' ']   #還原數(shù)字列表 
    root.mainloop() #顯示窗口
elif difficulty == 5:
    root.attributes("-topmost", True)
    row_of_space = 0    #存放空白按鈕的行號(hào) 
    col_of_space = 0    #存放空白按鈕的列號(hào)
    buttons = {}      #存放數(shù)字按鈕的數(shù)組
    numbers = ['1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24',' '] #所有數(shù)字文本的列表 
    shuffle(numbers)     #打亂數(shù)字列表中的數(shù)字順序
    for row in range(5): 
        for col in range(5):
            """創(chuàng)建數(shù)字按鈕,并將行列號(hào)傳入該按鈕的點(diǎn)擊事件函數(shù)"""
            button = Button(root,command = lambda x = row,y = col:Button_Click_3(x,y),bg = 'black',fg = 'green',font = ("Arial",35))
            buttons[row,col] = button   #將按鈕導(dǎo)入數(shù)組
            button['text'] = numbers.pop()    #設(shè)置按鈕上的文本
            button.place(x = 60 + col * 60,y = 60 + row * 60,width = 50,height = 50)    #設(shè)置數(shù)字按鈕大小
            if button['text'] == ' ':        #判斷是否為空白按鈕,如果是,則記錄到空白按鈕行列號(hào)變量
                row_of_space = row
                col_of_space = col
    numbers = ['1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24',' ']   #還原數(shù)字列表 
    root.mainloop() #顯示窗口
else:
    print('未完成此類(lèi)關(guān)卡')

下面是程序運(yùn)行結(jié)果

三階華容道加勝利

四階華容道加勝利

五階就不傳了,都一樣

接下來(lái)說(shuō)一下代碼原理

首先看下這一段

def Button_Click_1(x,y):      #按鈕點(diǎn)擊事件函數(shù)
        """聲明空白按鈕行列號(hào)和步數(shù)的變量為全局變量"""
        global row_of_space  
        global col_of_space    
        global step_number
        """判斷判斷點(diǎn)擊按鈕旁是否為空白按鈕,是則交換位置"""
        if abs(x-row_of_space) + abs(y-col_of_space) == 1:  
            step_number += 1    #將步數(shù)賦值
            label_step_number['text'] = '步數(shù):' + str(step_number)  #將步數(shù)變量導(dǎo)入label控件
            """交換按鈕位置"""
            buttons[row_of_space,col_of_space]['text'] = buttons[x,y]['text']
            buttons[x,y]['text']  = ' '  
            row_of_space = x            
            col_of_space = y
            n = 0   
            for row in range(3):
                for col in range(3):
                    """對(duì)比所有按鈕序列是否正確,不正確則跳出函數(shù)"""
                    if buttons[row,col]['text'] != numbers[n]:  
                        return
                    n += 1
            """所有按鈕判斷完畢贏得游戲勝利"""
            label_welcomes['text'] = '你贏了'

這段是自定義了三個(gè)按鈕的點(diǎn)擊事件函數(shù),不過(guò)三個(gè)都差不多,就是range后面的數(shù)字換了(應(yīng)對(duì)不同的階級(jí))。這個(gè)函數(shù)在注釋上已經(jīng)寫(xiě)明了,是判斷點(diǎn)擊按鈕旁是否有空白按鈕的,有則交換位置。后面運(yùn)用循環(huán)嵌套對(duì)比序列中的數(shù)字和矩陣上的是否一一對(duì)應(yīng),不對(duì)應(yīng)就跳出循環(huán),對(duì)應(yīng)則判定為贏得游戲。

"""創(chuàng)建華容道游戲窗口"""
root = Tk()                      #創(chuàng)建圖形化用戶(hù)界面實(shí)例
root.title('數(shù)字華容道')         #設(shè)置窗口標(biāo)題
root.geometry("400x400")    #將窗口大小設(shè)為高300寬300
root.configure(bg = 'black') #將窗口背景設(shè)為黑色
root.resizable(width = False,height = False)    #設(shè)置窗口為不可拉伸
"""設(shè)置歡迎語(yǔ)的label控件"""
label_welcomes = Label(root,text = '歡迎來(lái)到數(shù)字華容道',bg = 'black',fg = 'red',font = ("Arial",13))    #設(shè)置label控件的屬性
label_welcomes.place(x = 20,y = 10,width = 250,height = 40)  #設(shè)置label控件位置
"""設(shè)置顯示操作方式的label控件"""
label_operation = Label(root,text = '單擊數(shù)字',bg = 'black',fg = 'white',font = ("Arial",10))
label_operation.place(x = 3,y = 40,width = 50,height = 30)
label_operation_2 = Label(root,text = '移動(dòng)方塊',bg = 'black',fg = 'white',font = ("Arial",10))
label_operation_2.place(x = 3,y = 60,width = 50,height = 30)
"""設(shè)置顯示步數(shù)的label控件"""
label_step_number = Label(root,text = '步數(shù):' + str(step_number),bg = 'black',fg = 'yellow',font = ("Arial",10))
label_step_number.place(x = 3,y = 20,width = 50,height = 30)

這一段創(chuàng)建了圖形化用戶(hù)界面,具體每行代碼做什么用的注釋上我都標(biāo)清楚了。

接著說(shuō)一下,由于時(shí)間有限,只做了在殼窗口內(nèi)選擇幾階級(jí)數(shù)字華容道的版本。是用輸入數(shù)字來(lái)判定的。其他的都是些簡(jiǎn)單玩意,比如說(shuō)按鈕創(chuàng)建,調(diào)用函數(shù)和循環(huán)嵌套,代碼的注釋上都寫(xiě)了一些大概意思,有點(diǎn)tkinter基礎(chǔ)的應(yīng)該都能看懂,不會(huì)的可以問(wèn)我。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 淺談Django中的QueryDict元素為數(shù)組的坑

    淺談Django中的QueryDict元素為數(shù)組的坑

    這篇文章主要介紹了淺談Django中的QueryDict元素為數(shù)組的坑,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-03-03
  • python進(jìn)程管理工具supervisor的安裝與使用教程

    python進(jìn)程管理工具supervisor的安裝與使用教程

    supervisor是用python寫(xiě)的一個(gè)進(jìn)程管理工具,用來(lái)啟動(dòng),重啟,關(guān)閉進(jìn)程。下面這篇文章主要給大家介紹了關(guān)于python實(shí)現(xiàn)的進(jìn)程管理工具supervisor的安裝與使用的相關(guān)資料,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。
    2017-09-09
  • Python 爬取攜程所有機(jī)票的實(shí)例代碼

    Python 爬取攜程所有機(jī)票的實(shí)例代碼

    這篇文章主要介紹了Python 爬取攜程所有機(jī)票功能,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2018-06-06
  • Windows 下更改 jupyterlab 默認(rèn)啟動(dòng)位置的教程詳解

    Windows 下更改 jupyterlab 默認(rèn)啟動(dòng)位置的教程詳解

    這篇文章主要介紹了Windows 下更改 jupyterlab 默認(rèn)啟動(dòng)位置,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-05-05
  • Python求兩個(gè)字符串最長(zhǎng)公共子序列代碼實(shí)例

    Python求兩個(gè)字符串最長(zhǎng)公共子序列代碼實(shí)例

    這篇文章主要介紹了Python求兩個(gè)字符串最長(zhǎng)公共子序列代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-03-03
  • python小技巧——將變量保存在本地及讀取

    python小技巧——將變量保存在本地及讀取

    這篇文章主要介紹了python小技巧——如何將變量保存在本地及讀取,幫助大家更好的理解和使用Java,感興趣的朋友可以了解下
    2020-11-11
  • Python實(shí)現(xiàn)點(diǎn)云投影到平面顯示

    Python實(shí)現(xiàn)點(diǎn)云投影到平面顯示

    今天小編就為大家分享一篇Python實(shí)現(xiàn)點(diǎn)云投影到平面顯示,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-01-01
  • 舉例講解Python程序與系統(tǒng)shell交互的方式

    舉例講解Python程序與系統(tǒng)shell交互的方式

    這篇文章主要介紹了Python程序與系統(tǒng)shell交互的方式,舉了一個(gè)非常簡(jiǎn)單的hello world的例子,需要的朋友可以參考下
    2015-04-04
  • 如何在Python函數(shù)執(zhí)行前后增加額外的行為

    如何在Python函數(shù)執(zhí)行前后增加額外的行為

    有的時(shí)候會(huì)需要在函數(shù)前后添點(diǎn)額外的功能(比如過(guò)濾、計(jì)時(shí)等)時(shí),以前總是首先想到裝飾器。最近學(xué)習(xí)了Python的上下文管理器,所以本文就給大家介紹了如何在Python函數(shù)執(zhí)行前后增加額外的行為,有需要的朋友們可以參考借鑒,下面來(lái)一起看看吧。
    2016-10-10
  • Python運(yùn)行第一個(gè)PySide2的窗體程序

    Python運(yùn)行第一個(gè)PySide2的窗體程序

    本文主要介紹了Python運(yùn)行第一個(gè)PySide2的窗體程序,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-07-07

最新評(píng)論