摘要:已獲原作者授權(quán)原系列地址游戲本章我們演示一個(gè)進(jìn)階例子我們用編寫了游戲這個(gè)游戲也被稱作或者或者是一個(gè)古老的益智解謎游戲由兩名玩家參與早在世紀(jì)人們就在用鉛筆和紙來玩這個(gè)游戲了在年發(fā)明的游戲正是受到這個(gè)游戲的啟發(fā)和在基本理念上是一樣的但被盒裝出售
已獲原作者授權(quán). 原系列地址: Python TkinterMastermind 游戲
本章我們演示一個(gè)進(jìn)階例子. 我們用 Tkinter 編寫了 "Bulls and Cows" 游戲. 這個(gè)游戲也被稱作 "Cows and Bulls" 或者 "Pigs and Bulls" 或者 "Bulls and Cleots", 是一個(gè)古老的益智解謎游戲, 由兩名玩家參與. 早在19世紀(jì), 人們就在用鉛筆和紙來玩這個(gè)游戲了. Mordecai Meirowitz 在 1970 年發(fā)明的 Mastermind 游戲正是受到這個(gè)游戲的啟發(fā). Mastermind 和 Bulls and Cows 在基本理念上是一樣的, 但 Mastermind 被盒裝出售, 其中還包含了一個(gè)解謎棋盤和一些標(biāo)記解謎和反饋的標(biāo)簽. Mastermind 使用顏色作為謎題信息, 而 Bulls and Cows 則是用數(shù)字做謎題信息.
這個(gè)游戲的算法在我們的 Python 進(jìn)階教程中的 "Mastermind / Bulls and Cows" 一文內(nèi)有詳細(xì)闡釋.
from tkinter import * from tkinter.messagebox import * import random from combinatorics import all_colours def inconsistent(p, guesses): """ the function checks, if a permutation p, i.e. a list of colours like p = ["pink", "yellow", "green", "red"] is consistent with the previous colours. Each previous colour permuation guess[0] compared (check()) with p has to return the same amount of blacks (rightly positioned colours) and whites (right colour at wrong position) as the corresponding evaluation (guess[1] in the list guesses) """ for guess in guesses: res = check(guess[0], p) (rightly_positioned, permutated) = guess[1] if res != [rightly_positioned, permutated]: return True # inconsistent return False # i.e. consistent def answer_ok(a): """ checking of an evaulation given by the human player makes sense. 3 blacks and 1 white make no sense for example. """ (rightly_positioned, permutated) = a if (rightly_positioned + permutated > number_of_positions) or (rightly_positioned + permutated < len(colours) - number_of_positions): return False if rightly_positioned == 3 and permutated == 1: return False return True def get_evaluation(): """ get evaluation from entry fields """ rightly_positioned = int(entryWidget_both.get()) permutated = int(entryWidget_only_colours.get()) return (rightly_positioned, permutated) def new_evaluation(current_colour_choices): """ This funtion gets an evaluation of the current guess, checks the consistency of this evaluation, adds the guess together with the evaluation to the list of guesses, shows the previous guesses and creates a ne guess """ rightly_positioned, permutated = get_evaluation() if rightly_positioned == number_of_positions: return(current_colour_choices, (rightly_positioned, permutated)) if not answer_ok((rightly_positioned, permutated)): print("Input Error: Sorry, the input makes no sense") return(current_colour_choices, (-1, permutated)) guesses.append((current_colour_choices, (rightly_positioned, permutated))) view_guesses() current_colour_choices = create_new_guess() show_current_guess(current_colour_choices) if not current_colour_choices: return(current_colour_choices, (-1, permutated)) return(current_colour_choices, (rightly_positioned, permutated)) def check(p1, p2): """ check() calcualtes the number of bulls (blacks) and cows (whites) of two permutations """ blacks = 0 whites = 0 for i in range(len(p1)): if p1[i] == p2[i]: blacks += 1 else: if p1[i] in p2: whites += 1 return [blacks, whites] def create_new_guess(): """ a new guess is created, which is consistent to the previous guesses """ next_choice = next(permutation_iterator) while inconsistent(next_choice, guesses): try: next_choice = next(permutation_iterator) except StopIteration: print("Error: Your answers were inconsistent!") return () return next_choice def new_evaluation_tk(): global current_colour_choices res = new_evaluation(current_colour_choices) current_colour_choices = res[0] def show_current_guess(new_guess): row = 1 Label(root, text=" New Guess: ").grid(row=row, column=0, columnspan=4) row +=1 col_count = 0 for c in new_guess: print(c) l = Label(root, text=" ", bg=c) l.grid(row=row,column=col_count, sticky=W, padx=2) col_count += 1 def view_guesses(): row = 3 Label(root, text="Old Guesses").grid(row=row, column=0, columnspan=4) Label(root, text="c&p").grid(row=row, padx=5, column=number_of_positions + 1) Label(root, text="p").grid(row=row, padx=5, column=number_of_positions + 2) # dummy label for distance: Label(root, text=" ").grid(row=row, column=number_of_positions + 3) row += 1 # vertical dummy label for distance: Label(root, text=" ").grid(row=row, column=0, columnspan=5) for guess in guesses: guessed_colours = guess[0] col_count = 0 row += 1 for c in guessed_colours: print(guessed_colours[col_count]) l = Label(root, text=" ", bg=guessed_colours[col_count]) l.grid(row=row,column=col_count, sticky=W, padx=2) col_count += 1 # evaluation: for i in (0,1): l = Label(root, text=str(guess[1][i])) l.grid(row=row,column=col_count + i + 1, padx=2) if __name__ == "__main__": colours = ["red","green","blue","yellow","orange","pink"] guesses = [] number_of_positions = 4 permutation_iterator = all_colours(colours, number_of_positions) current_colour_choices = next(permutation_iterator) new_guess = (current_colour_choices, (0,0) ) row_offset = 1 root = Tk() root.title("Mastermind") root["padx"] = 30 root["pady"] = 20 entryLabel = Label(root) entryLabel["text"] = "Completely Correct:" entryLabel.grid(row=row_offset, sticky=E, padx=5, column=number_of_positions + 4) entryWidget_both = Entry(root) entryWidget_both["width"] = 5 entryWidget_both.grid(row=row_offset, column=number_of_positions + 5) entryLabel = Label(root) entryLabel["text"] = "Wrong Position:" entryLabel.grid(row=row_offset+1, sticky=E, padx=5, column= number_of_positions + 4) entryWidget_only_colours = Entry(root) entryWidget_only_colours["width"] = 5 entryWidget_only_colours.grid(row=row_offset+1, column=number_of_positions + 5) submit_button = Button(root, text="Submit", command=new_evaluation_tk) submit_button.grid(row=4,column=number_of_positions + 4) quit_button = Button(root, text="Quit", command=root.quit) quit_button.grid(row=4,column=number_of_positions + 5) show_current_guess(current_colour_choices) root.mainloop()
譯者注: 不打算翻譯這篇文章中提到的那篇進(jìn)階教程了...
全系列:
[譯][Tkinter 教程01] 入門: Label 控件
[譯][Tkinter 教程02] Message 控件
[譯][Tkinter 教程03] Button 控件
[譯][Tkinter 教程04] Variable 類
[譯][Tinkter 教程05] Radiobutton 控件
[譯][Tkinter 教程06] Checkbox 控件
[譯][Tkinter 教程07] Entry 控件
[譯][Tkinter 教程08] Canvas 圖形繪制
[譯][Tkinter 教程09] Scale 控件
[譯][Tkinter 教程10] Text 控件
[譯][Tkinter 教程11] 對(duì)話框和消息框
[譯][Tkinter 教程12] 布局管理 (Pack Place Grid)
[譯][Tkinter 教程13] Mastermind 游戲
[譯][Tkinter 教程14] menu 菜單
[譯][Tkinter 教程15] event 事件綁定
譯者水平有限, 如有疏漏, 歡迎指正.
已獲得原作者授權(quán). 原文地址: Mastermind in TK.
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://m.specialneedsforspecialkids.com/yun/41506.html
摘要:已獲原作者授權(quán)原系列地址簡(jiǎn)介控件是一種標(biāo)準(zhǔn)控件用來展現(xiàn)不同樣式的按鈕控件被用以和用戶交互比如按鈕被鼠標(biāo)點(diǎn)擊后某種操作被啟動(dòng)和控件類似按鈕可以展示圖片或者文字不同的是控件可以指定字體控件只能使用單一的字體上的文字可以多行顯示可以將一個(gè)函數(shù)或方 已獲原作者授權(quán). 原系列地址: Python Tkinter 簡(jiǎn)介 Button 控件是一種標(biāo)準(zhǔn) Tkinter 控件, 用來展現(xiàn)不同樣式的按鈕...
摘要:已獲原作者授權(quán)原系列地址簡(jiǎn)介一提到這個(gè)詞很多人首先想到的是餐館里的菜單雖然餐館菜單和計(jì)算機(jī)程序中的菜單看起來一點(diǎn)也不像但他們確實(shí)有很多共同點(diǎn)在餐館中菜單列舉了所有菜品和飲料在計(jì)算機(jī)程序中菜單通過圖形界面展示了應(yīng)用程序可用的命令和功能在用戶界 已獲原作者授權(quán). 原系列地址: Python Tkinter 簡(jiǎn)介 一提到menu這個(gè)詞, 很多人首先想到的是餐館里的菜單. 雖然餐館菜單和計(jì)算...
摘要:已獲原作者授權(quán)原系列地址類有些控件比如控件控件等可以通過傳入特定參數(shù)直接和一個(gè)程序變量綁定這些參數(shù)包括這種綁定是雙向的如果該變量發(fā)生改變與該變量綁定的控件也會(huì)隨之更新這些控制變量和一般的變量一樣都是用來保存某個(gè)值的但一般的變量不能被傳遞給或 已獲原作者授權(quán). 原系列地址: Python Tkinter Variable 類 有些控件 (比如 Entry 控件, Radiobutton...
摘要:已獲原作者授權(quán)原系列地址單選按鈕是一種可在多個(gè)預(yù)先定義的選項(xiàng)中選擇出一項(xiàng)的控件單選按鈕可顯示文字或圖片顯示文字時(shí)只能使用預(yù)設(shè)字體該控件可以綁定一個(gè)函數(shù)或方法當(dāng)單選按鈕被選擇時(shí)該函數(shù)或方法將被調(diào)用單選按鈕這個(gè)名字來源于收音機(jī)上的調(diào)頻按鈕這些按 已獲原作者授權(quán). 原系列地址: Python Tkinter Radio Buttons 單選按鈕是一種可在多個(gè)預(yù)先定義的選項(xiàng)中選擇出一項(xiàng)的 T...
摘要:已獲原作者授權(quán)原系列地址控件控件用來展示一些文字短消息和控件有些類似但在展示文字方面比要靈活比如控件可以改變字體而控件只能使用一種字體它提供了一個(gè)換行對(duì)象以使文字可以斷為多行它可以支持文字的自動(dòng)換行及對(duì)齊這里要澄清一下前面提到的控件可以改變 已獲原作者授權(quán). 原系列地址: Python Tkinter Message 控件 Message 控件用來展示一些文字短消息. Message...
閱讀 1135·2023-04-26 00:12
閱讀 3275·2021-11-17 09:33
閱讀 1069·2021-09-04 16:45
閱讀 1196·2021-09-02 15:40
閱讀 2181·2019-08-30 15:56
閱讀 2969·2019-08-30 15:53
閱讀 3557·2019-08-30 11:23
閱讀 1939·2019-08-29 13:54