[Tkinter 教程13] Mastermind 游戏

来源:互联网 发布:l800清零维修软件 编辑:程序博客网 时间:2024/06/05 20:25

原系列地址: Python Tkinter

Mastermind 游戏

本章我们演示一个进阶例子. 我们用 Tkinter 编写了 “Bulls and Cows” 游戏. 这个游戏也被称作 “Cows and Bulls” 或者 “Pigs and Bulls” 或者 “Bulls and Cleots”, 是一个古老的益智解谜游戏, 由两名玩家参与. 早在19世纪, 人们就在用铅笔和纸来玩这个游戏了. Mordecai Meirowitz 在 1970 年发明的 Mastermind 游戏正是受到这个游戏的启发. Mastermind 和 Bulls and Cows 在基本理念上是一样的, 但 Mastermind 被盒装出售, 其中还包含了一个解谜棋盘和一些标记解谜和反馈的标签. Mastermind 使用颜色作为谜题信息, 而 Bulls and Cows 则是用数字做谜题信息.
这个游戏的算法在我们的 Python 进阶教程中的 “Mastermind / Bulls and Cows” 一文内有详细阐释.

这里写图片描述

实现代码

from tkinter import *from tkinter.messagebox import *import randomfrom combinatorics import all_coloursdef inconsistent(p, guesses):   """ the function checks, if a permutation p, i.e. a list of colours like p = ['pink', 'yellow', 'green', 'red'] is consistentwith 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. consistentdef 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 Truedef 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 withthe 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_choicedef 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 += 1def 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()

译者注: 不打算翻译这篇文章中提到的那篇进阶教程了…

译者水平有限, 如有疏漏, 欢迎指正.
已获得原作者授权. 原文地址: Mastermind in TK.

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 数字画涂料干了怎么办 广告画颜料干了怎么办 宝宝断奶后瘦了怎么办 腿一个粗一个细怎么办 两条小腿不一样粗怎么办 两岁宝宝坐不了怎么办 q糖孩子吃多了怎么办 q糖孩子吃的太多怎么办 ps图层不能覆盖怎么办 孩子的字写的不好怎么办 孩子爱打人怎么办 6招 一岁宝宝爱打人怎么办 2岁小朋友爱打人怎么办 小朋友爱动手怎么办4岁 5,6岁爱动手打人怎么办 七个月宝宝大便干燥怎么办 两岁宝宝吐奶怎么办 2岁宝宝轻微蛀牙怎么办 2岁宝宝喝水都吐怎么办 2岁宝宝吐的厉害怎么办 两岁宝宝个子矮怎么办 宝宝吐了一天了怎么办 3岁宝宝吃饭呕吐怎么办 3岁宝宝突然呕吐怎么办 11个月婴儿呕吐怎么办 2岁宝宝呕吐是怎么办 2岁宝宝呕吐腹泻怎么办 七个月的宝宝拉肚子怎么办 两岁宝宝玩手机怎么办 5岁宝宝突然口吃怎么办 3岁宝宝突然口吃怎么办 2岁半宝宝结巴怎么办 卤门2岁未闭合怎么办 宝宝2岁说话结巴怎么办 4岁半的宝宝口臭怎么办 2岁宝宝口气重怎么办 2岁宝宝有口气怎么办 2岁宝宝头大怎么办 2周宝宝讲话结巴怎么办 宝贝烧到39度怎么办 4岁宝宝有点口吃怎么办