《Mastermind》是一款两人密码破译游戏,其中一名玩家隐藏由颜色组成的代码,而另一名玩家则必须使用前一名玩家每回合提供的线索来猜测它。
在本教程中,我们将使用 Python 语言创建我们自己的 Mastermind 游戏。在我们的 Mastermind 版本中,计算机将随机选择一个密码,用户根据计算机给出的确定性线索尝试猜测它。
Python 中的 Mastermind 游戏 – 演示
废话不多说,让我们进入游戏的设计部分。
策划游戏设计
原始策划板如下所示。
电路板的屏蔽端隐藏了密码,而整个电路板则基于密码破译者的猜测。一旦用户识别出隐藏代码,游戏就会结束。
图中棋盘的右侧包含一系列白色和红色的钉子,用于表示与每次尝试相关的线索。
- 红色– 所选颜色之一位于密码中的正确位置
- 白色– 代码中存在所选颜色之一,但位置不正确。
终端中的板子的实现如下所示:
棋盘的顶端隐藏着密码,当玩家没有机会或破解密码时,密码就会显露出来。在那一刻之前,会显示“UNK”(表示“未知”) 。
棋盘的每个部分都表示玩家进行的一个回合。游戏支持六种颜色,红色,绿色,黄色,蓝色,黑色,橙色。最左边的面板表示基于每个回合的线索。“W”代表白色,“R”代表红色,按其原始含义使用
上图中,用户选择的四种颜色中,有三种是正确的,但根据密码,它们的位置都不是正确的,因此线索中有三个“W”。
# Function to print the mastermind board def print_mastermind_board(passcode, guess_codes, guess_flags): print ( "-----------------------------------------" ) print ( "\t MASTERMIND" ) print ( "-----------------------------------------" ) print ( " |" , end = "") for x in passcode: print ( "\t" + x[: 3 ], end = "") print () for i in reversed ( range ( len (guess_codes))): print ( "-----------------------------------------" ) print (guess_flags[i][ 0 ], guess_flags[i][ 1 ], "|" ) print (guess_flags[i][ 2 ], guess_flags[i][ 3 ], end = " |" ) for x in guess_codes[i]: print ( "\t" + x[: 3 ], end = "") print () print ( "-----------------------------------------" ) |
上面的代码片段负责在终端上显示主脑板。
数据结构 – 游戏变量
为了方便开发游戏逻辑,我们需要一些数据结构。
colors
– 游戏涉及的颜色列表colors_map
– 数字和颜色之间的映射passcode
– 密码show_passcode
– 显示给用户的密码,未知数列表guess_code
– 玩家做出的猜测列表guess_flags
– 提供给玩家的线索列表列表
# The Main function if __name__ = = '__main__' : # List of colors colors = [ "RED" , "GREEN" , "YELLOW" , "BLUE" , "BLACK" , "ORANGE" ] # Mapping of colors to numbers colors_map = { 1 : "RED" , 2 : "GREEN" , 3 : "YELLOW" , 4 : "BLUE" , 5 : "BLACK" , 6 : "ORANGE" } # Randomly selecting a passcode random.shuffle(colors) passcode = colors[: 4 ] # Number of chances for the player chances = 8 # The passcode to be shown to the user show_passcode = [ 'UNK' , 'UNK' , 'UNK' , 'UNK' ] # The codes guessed by the player each turn guess_codes = [[ '-' , '-' , '-' , '-' ] for x in range (chances)] # The clues provided to the player each turn guess_flags = [[ '-' , '-' , '-' , '-' ] for x in range (chances)] |
在处理我们实现 Mastermind 游戏的游戏逻辑时,这些数据结构中的每一个都很方便。
游戏循环
游戏开发中最关键的部分之一是游戏循环,它负责玩家移动和游戏变量更新的正常运行。
# The current turn turn = 0 # The GAME LOOP while turn < chances: |
游戏循环取决于机会数和当前回合。当玩家的机会耗尽时,它应该停止游戏。
游戏菜单
游戏菜单是游戏的一个简单方面,它帮助程序员通过指令或规则与玩家进行交互。
我们的游戏菜单如下所示:
# The GAME MENU print ( "-----------------------------------------" ) print ( "\t\tMenu" ) print ( "-----------------------------------------" ) print ( "Enter code using numbers." ) print ( "1 - RED, 2 - GREEN, 3 - YELLOW, 4 - BLUE, 5 - BLACK, 6 - ORANGE" ) print ( "Example: RED YELLOW ORANGE BLACK ---> 1 3 6 5" ) print ( "-----------------------------------------" ) print_mastermind_board(show_passcode, guess_codes, guess_flags) |
游戏菜单必须简单明了。
处理玩家输入
处理玩家输入涉及三个基本步骤:接受玩家输入,对其进行一些健全性检查,如果一切正常,则将其存储到我们的数据结构中。
接受玩家输入
正如游戏菜单中提到的,玩家需要输入四个数字,每个数字对应一种特定颜色,并用空格分隔。
我们的工作是将玩家输入解析为整数列表,以检索正确的颜色。
# Accepting the player input try : code = list ( map ( int , input ( "Enter your choice = " ).split())) except ValueError: clear() print ( "\tWrong choice!! Try again!!" ) continue |
注意:该
clear()
函数负责通过清除以前的输出来保持终端干净。它需要Python 的os
库。检查下面的完整代码以了解 的函数声明
clear()
。
应用健全性检查
接下来是对玩家输入进行一些健全性检查。
# Check if the number of colors nunbers are 4 if len (code) ! = 4 : clear() print ( "\tWrong choice!! Try again!!" ) continue # Check if each number entered corresponds to a number flag = 0 for x in code: if x > 6 or x < 1 : flag = 1 if flag = = 1 : clear() print ( "\tWrong choice!! Try again!!" ) continue |
存储玩家的动作
当我们知道玩家做出了有效的动作后,我们可以将其存储在游戏容器中。
# Storing the player moves for i in range ( 4 ): guess_codes[turn][i] = colors_map[code[i]] |
为每个动作设置线索
有两组标记要分配,如果颜色位于密码中的正确位置,则为“R”;如果颜色正确但位置错误,则为“W”。
# Process to apply clues according to the player input dummy_passcode = [x for x in passcode] pos = 0 # Loop to set up clues for the player move for x in code: if colors_map[x] in dummy_passcode: if code.index(x) = = passcode.index(colors_map[x]): guess_flags[turn][pos] = 'R' else : guess_flags[turn][pos] = 'W' pos + = 1 dummy_passcode.remove(colors_map[x]) random.shuffle(guess_flags[turn]) |
要记住的一件小事是重新排列标志,因为它可能会给出与颜色位置相关的提示。
检查获胜条件
我们所要做的就是用隐藏代码检查最新的输入。
# Check for win condition if guess_codes[turn] = = passcode: clear() print_mastermind_board(passcode, guess_codes, guess_flags) print ( "Congratulations!! YOU WIN!!!!" ) break |
一旦玩家输入正确的代码,我们就会显示获胜消息并结束游戏。
更新回合数
一项小但非常重要的任务是在每个成功的玩家移动后更新回合数。
# Update turn turn + = 1 clear() |
最后但并非最不重要的是处理丢失情况。
检查丢失情况
当玩家用尽机会次数时,玩家就输了。发生这种情况时,我们需要显示适当的消息。
# Check for loss condiiton if turn = = chances: clear() print_mastermind_board(passcode, guess_codes, guess_flags) print ( "YOU LOSE!!! Better luck next time!!!" ) |
使用Python语言创建mastermind的说明就结束了。
完整代码
import random import os def clear(): os.system( "clear" ) # Function to print the mastermind board def print_mastermind_board(passcode, guess_codes, guess_flags): print ( "-----------------------------------------" ) print ( "\t MASTERMIND" ) print ( "-----------------------------------------" ) print ( " |" , end = "") for x in passcode: print ( "\t" + x[: 3 ], end = "") print () for i in reversed ( range ( len (guess_codes))): print ( "-----------------------------------------" ) print (guess_flags[i][ 0 ], guess_flags[i][ 1 ], "|" ) print (guess_flags[i][ 2 ], guess_flags[i][ 3 ], end = " |" ) for x in guess_codes[i]: print ( "\t" + x[: 3 ], end = "") print () print ( "-----------------------------------------" ) # The Main function if __name__ = = '__main__' : # List of colors colors = [ "RED" , "GREEN" , "YELLOW" , "BLUE" , "BLACK" , "ORANGE" ] # Mapping of colors to numbers colors_map = { 1 : "RED" , 2 : "GREEN" , 3 : "YELLOW" , 4 : "BLUE" , 5 : "BLACK" , 6 : "ORANGE" } # Randomly selecting a passcode random.shuffle(colors) passcode = colors[: 4 ] # Number of chances for the player chances = 8 # The passcode to be shown to the user show_passcode = [ 'UNK' , 'UNK' , 'UNK' , 'UNK' ] # The codes guessed by the player each turn guess_codes = [[ '-' , '-' , '-' , '-' ] for x in range (chances)] # The clues provided to the player each turn guess_flags = [[ '-' , '-' , '-' , '-' ] for x in range (chances)] clear() # The current turn turn = 0 # The GAME LOOP while turn < chances: print ( "-----------------------------------------" ) print ( "\t\tMenu" ) print ( "-----------------------------------------" ) print ( "Enter code using numbers." ) print ( "1 - RED, 2 - GREEN, 3 - YELLOW, 4 - BLUE, 5 - BLACK, 6 - ORANGE" ) print ( "Example: RED YELLOW ORANGE BLACK ---> 1 3 6 5" ) print ( "-----------------------------------------" ) print_mastermind_board(show_passcode, guess_codes, guess_flags) # Accepting the player input try : code = list ( map ( int , input ( "Enter your choice = " ).split())) except ValueError: clear() print ( "\tWrong choice!! Try again!!" ) continue # Check if the number of colors nunbers are 4 if len (code) ! = 4 : clear() print ( "\tWrong choice!! Try again!!" ) continue # Check if each number entered corresponds to a number flag = 0 for x in code: if x > 6 or x < 1 : flag = 1 if flag = = 1 : clear() print ( "\tWrong choice!! Try again!!" ) continue # Storing the player input for i in range ( 4 ): guess_codes[turn][i] = colors_map[code[i]] # Process to apply clues according to the player input dummy_passcode = [x for x in passcode] pos = 0 # Loop to set up clues for the player move for x in code: if colors_map[x] in dummy_passcode: if code.index(x) = = passcode.index(colors_map[x]): guess_flags[turn][pos] = 'R' else : guess_flags[turn][pos] = 'W' pos + = 1 dummy_passcode.remove(colors_map[x]) random.shuffle(guess_flags[turn]) # Check for win condition if guess_codes[turn] = = passcode: clear() print_mastermind_board(passcode, guess_codes, guess_flags) print ( "Congratulations!! YOU WIN!!!!" ) break # Update turn turn + = 1 clear() # Check for loss condiiton if turn = = chances: clear() print_mastermind_board(passcode, guess_codes, guess_flags) print ( "YOU LOSE!!! Better luck next time!!!" ) |
结论
对于任何新手 Python 程序员来说,创建我们自己的游戏的任务一开始可能看起来令人畏惧。我们希望本文简化了某些 Python 概念,并使读者认为该任务是可以实现的。
如有任何建议或疑问,请随时在下面发表评论。感谢您的阅读。