资讯专栏INFORMATION COLUMN

[译][Tkinter 教程13] Mastermind 游戏

Jaden / 2852人阅读

摘要:已获原作者授权原系列地址游戏本章我们演示一个进阶例子我们用编写了游戏这个游戏也被称作或者或者是一个古老的益智解谜游戏由两名玩家参与早在世纪人们就在用铅笔和纸来玩这个游戏了在年发明的游戏正是受到这个游戏的启发和在基本理念上是一样的但被盒装出售

已获原作者授权. 原系列地址: 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 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()

译者注: 不打算翻译这篇文章中提到的那篇进阶教程了...
全系列:
[译][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] 对话框和消息框
[译][Tkinter 教程12] 布局管理 (Pack Place Grid)
[译][Tkinter 教程13] Mastermind 游戏
[译][Tkinter 教程14] menu 菜单
[译][Tkinter 教程15] event 事件绑定
译者水平有限, 如有疏漏, 欢迎指正.
已获得原作者授权. 原文地址: Mastermind in TK.

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/41506.html

相关文章

  • [][Tkinter 教程03] Button 控件

    摘要:已获原作者授权原系列地址简介控件是一种标准控件用来展现不同样式的按钮控件被用以和用户交互比如按钮被鼠标点击后某种操作被启动和控件类似按钮可以展示图片或者文字不同的是控件可以指定字体控件只能使用单一的字体上的文字可以多行显示可以将一个函数或方 已获原作者授权. 原系列地址: Python Tkinter 简介 Button 控件是一种标准 Tkinter 控件, 用来展现不同样式的按钮...

    googollee 评论0 收藏0
  • [][Tkinter 教程14] menu 菜单

    摘要:已获原作者授权原系列地址简介一提到这个词很多人首先想到的是餐馆里的菜单虽然餐馆菜单和计算机程序中的菜单看起来一点也不像但他们确实有很多共同点在餐馆中菜单列举了所有菜品和饮料在计算机程序中菜单通过图形界面展示了应用程序可用的命令和功能在用户界 已获原作者授权. 原系列地址: Python Tkinter 简介 一提到menu这个词, 很多人首先想到的是餐馆里的菜单. 虽然餐馆菜单和计算...

    joyqi 评论0 收藏0
  • [][Tkinter 教程04] Variable 类

    摘要:已获原作者授权原系列地址类有些控件比如控件控件等可以通过传入特定参数直接和一个程序变量绑定这些参数包括这种绑定是双向的如果该变量发生改变与该变量绑定的控件也会随之更新这些控制变量和一般的变量一样都是用来保存某个值的但一般的变量不能被传递给或 已获原作者授权. 原系列地址: Python Tkinter Variable 类 有些控件 (比如 Entry 控件, Radiobutton...

    zilu 评论0 收藏0
  • [][Tinkter 教程05] Radiobutton 控件

    摘要:已获原作者授权原系列地址单选按钮是一种可在多个预先定义的选项中选择出一项的控件单选按钮可显示文字或图片显示文字时只能使用预设字体该控件可以绑定一个函数或方法当单选按钮被选择时该函数或方法将被调用单选按钮这个名字来源于收音机上的调频按钮这些按 已获原作者授权. 原系列地址: Python Tkinter Radio Buttons 单选按钮是一种可在多个预先定义的选项中选择出一项的 T...

    shusen 评论0 收藏0
  • [][Tkinter 教程02] Message 控件

    摘要:已获原作者授权原系列地址控件控件用来展示一些文字短消息和控件有些类似但在展示文字方面比要灵活比如控件可以改变字体而控件只能使用一种字体它提供了一个换行对象以使文字可以断为多行它可以支持文字的自动换行及对齐这里要澄清一下前面提到的控件可以改变 已获原作者授权. 原系列地址: Python Tkinter Message 控件 Message 控件用来展示一些文字短消息. Message...

    JowayYoung 评论0 收藏0

发表评论

0条评论

Jaden

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<