Logo
Section 1 / 2

项目:构建一个 Hangman 游戏

学习如何使用AI助手通过过程式编程技术开发一个猜字游戏(Hangman),以提升我们的编程技能和对游戏逻辑的理解。

导入

项目:构建一个 Hangman 游戏

学习如何使用AI助手通过过程式编程技术开发一个猜字游戏(Hangman),以提升我们的编程技能和对游戏逻辑的理解。

...
1 / 17

由于错误猜测,绞刑架逐步构建

...

> 你需要作为我的编程助手和头脑风暴伙伴。帮助我生成想法、建议最佳实践,并引导我的思维过程,使其与我的项目目标保持一致。我会保留对项目的完全控制权,因此除非我明确要求,否则请不要提供任何代码。 > > 每个建议都应清晰、简洁,并以选项或想法的形式呈现,而不是最终解决方案,以帮助我提升编码技能和解决问题的能力。确保我在继续之前充分理解每个部分,并记住,每一行代码在编写前都需要我批准。

...

> 我已准备就绪!你想从哪个项目开始,或者你正在考虑哪些想法?

...

> 太好了!我们将尝试使用 Python 制作一个猜词游戏(Hangman)。我们可以探索哪些选项?

...
None

> **对于 Python 中的猜词游戏,我们可以考虑以下几个选项:** > > * **游戏结构:** > - 选项一:一个基本循环,持续运行直到玩家获胜或失败。 > - 选项二:模块化方法,包含用于选择单词、检查猜测和更新显示的函数,以便更好地组织代码。 > > * **单词选择:** > - 选项一:使用静态单词列表。 > - 选项二:从外部文件或 API 加载单词,以增加多样性。 > > * **显示猜词状态:** > - 选项一:使用简单的 ASCII 艺术字符画。 > - 选项二:使用 turtle 或 pygame 等库添加图形元素,实现可视化扩展。 > > * **输入与验证:** > - 选项一:基本输入验证。 > - 选项二:高级输入处理,允许整词猜测并处理边界情况。 > > * **可重玩性:** > - 选项一:询问玩家是否想再次游戏。 > - 选项二:添加计分系统,记录多局游戏中的胜负情况。 > > 你想先探索哪个选项?我们可以根据你的游戏目标进行调整。

...

> 这些都是不错的选项。我们为本项目选择以下方案: >1. 游戏结构:采用模块化方法。 >2. 单词选择:使用静态单词列表。 >3. 显示猜词状态:使用简单文本显示。 >4. 输入与验证:基本输入验证。 >5. 可重玩性:询问玩家是否想重新开始游戏。 >请为我写出模块化设计的大致框架?之后我们将逐个分析每个函数所需的代码。

...

> **猜词游戏的模块化分解** > 1. select_word() > 从预定义列表中随机选择一个单词。 > *输入:* 无 | *输出:* 随机单词(字符串)。 > 2. display_word(word, correct_guesses) > 显示当前单词,未猜出的字母用下划线表示。 > *输入:* 单词(字符串),正确猜测的字母列表。 | *输出:* 部分揭示的单词。 > 3. get_player_guess(guessed_letters) > 验证并返回玩家的猜测字母。 > *输入:* 已猜测字母列表。 | *输出:* 有效的猜测字母。 > 4. update_game_state(word, guess, correct_guesses, incorrect_guesses) > 更新正确/错误猜测,并检查游戏状态。 > *输入:* 单词、猜测字母、正确和错误猜测列表。 | *输出:* 更新后的猜测列表。 > 5. display_hangman(incorrect_guesses) > 以 ASCII 艺术形式显示当前绞刑架状态。 > *输入:* 错误猜测次数。 | *输出:* 无(打印绞刑架图案)。 > 6. check_game_over(word, correct_guesses, incorrect_guesses) > 判断游戏是否结束(胜利或失败)。 > *输入:* 单词、正确和错误猜测列表。 | *输出:* 游戏结束及胜利状态(True/False)。 > 7. play_game() > 协调游戏流程,调用其他函数。 > *输入:* 无 | *输出:* 无(运行游戏)。 > 8. ask_for_replay() > 提示玩家是否重玩。 > *输入:* 无 | *输出:* 是否重玩(True/False)。 > > --- > **游戏流程:** > - play_game() 启动并选择一个单词。 > - 循环执行:显示单词、获取猜测、更新游戏状态、显示绞刑架、检查游戏是否结束。 > - 游戏结束后,ask_for_replay() 决定是否重新开始或退出。 > > 你想从这些函数中的哪一个开始?我们可以逐步细化每个部分。

...

> 谢谢你提供的框架。让我们先从 play_game 函数入手,并在其中调用 select_word 函数。

...
None

> 很好的选择!让我们从 play_game() 函数开始,并加入对 select_word() 的调用。 > > **play_game() 的初步设计** > 此函数将控制游戏的主要流程,因此我们需要它: > > 1. 在游戏开始时选择一个单词。 > 2. 初始化游戏状态,例如创建正确和错误猜测的列表。 > 3. 循环执行,直到游戏胜利或失败。 > 4. 最终调用其他辅助函数(如 display_word()、get_player_guess() 等)来处理不同环节。 > > 目前,我们先专注于: > - 使用 select_word() 选择单词。 > - 建立基本结构,并为后续步骤预留占位符。 > > --- > > **play_game() 的基本结构** > > ```python > def play_game(): > # 步骤 1:为游戏选择一个单词 > word = select_word() > > # 步骤 2:初始化游戏状态 > correct_guesses = [] > incorrect_guesses = [] > > # 其他步骤的占位符,如显示单词、接收输入等。 > # 我们将在后续步骤中添加实际功能 > > print(f"Selected word: {word}") # 测试时用于验证单词选择 > ``` > > 此结构展示了 play_game() 如何通过选择单词并准备主游戏循环来启动。目前我们仅打印所选单词,以确认 select_word() 函数能正常工作。 > > 接下来,我们需要实现 select_word() 函数。我们现在就开始吗?

...

> 是的,让我们实现 select_word 函数。你能建议一个可以放在该函数中的示例单词列表吗?我认为10个单词的列表就很好。

...

> 完美!接下来我们构建 select_word() 函数,我将为你推荐一个示例单词列表。 > > - **示例单词列表:** > 以下是包含10个单词的列表。 > > **太空主题单词列表:** > ```python > ["planet", "asteroid", "galaxy", "spaceship", "comet", > "blackhole", "orbit", "meteor", "astronaut", "satellite"] > ``` > > ### 建议的 select_word() 函数: > 确认该列表后,我们可以使用 Python 的 random.choice() 随机选取一个单词。 > > 以下是该函数的建议实现: > > 以下是结合该列表的 select_word() 函数: > > ### select_word() 函数: > ```python > import random > > def select_word(): > # 太空主题的静态单词列表 > words = [ > "planet", "asteroid", "galaxy", "spaceship", > "comet", "blackhole", "orbit", "meteor", > "astronaut", "satellite" > ] > > # 从列表中随机选择一个单词 > return random.choice(words) > ``` > > 你想现在测试这个函数,还是继续下一个函数 display_word(),用于显示当前被猜测单词的状态?

...

> 不,我们先测试这个函数。这很关键。

...
None

> 当然!测试 select_word() 函数非常重要,以确保其按预期工作。以下是你可以快速测试 select_word() 和 play_game() 函数的方法。 > > **测试代码** > 你可以运行以下代码片段,查看单词选择是否正确。它将调用 play_game() 并打印所选单词。 > > ```python > import random > > def select_word(): > # 太空主题的静态单词列表 > words = [ > "planet", "asteroid", "galaxy", "spaceship", > "comet", "blackhole", "orbit", "meteor", > "astronaut", "satellite" > ] > > # 从列表中随机选择一个单词 > return random.choice(words) > > def play_game(): > # 步骤 1:为游戏选择一个单词 > word = select_word() > > # 步骤 2:初始化游戏状态 > correct_guesses = [] > incorrect_guesses = [] > > # 测试输出,用于验证单词选择 > print(f"Selected word: {word}") # 将打印随机选择的单词 > > # 运行测试 > play_game() > ``` > > **预期输出** > 运行此代码后,应显示太空主题列表中的某个单词,例如 "planet"、"galaxy" 或 "comet"。 > > 请在你的 Python 环境中运行此代码。如果结果符合预期或需要调整,请告诉我!

PageFlux AI 助手

👋 你好!我是 PageFlux AI 助手,专门帮助您理解课程内容。有什么问题尽管问我吧!

💡 选中文本后点击"AI 询问",或 Ctrl+Enter 发送消息

No configuration found for ID: 9ecba6ec8d665e1f8deb93ec460dfd9e2ee2cedabbe60d1412f5fdfafd89f54f

查看对话

以下是我们在上文基础上继续与AI编程助手的其余对话内容。

[]

[]

[]

[]

[]

[]

[]

[]

[]

[]

[]

[]

[]

[]

[]

[]

[]

[]

[]

[]

[]

[]

[]

[]

[]

[]

[]

[]

查看解决方案

1import os 2import random 3 4# 函数:清屏 5def clear_screen(): 6 os.system('cls' if os.name == 'nt' else 'clear') 7 8# 函数:从静态列表中选择一个单词 9def select_word(): 10 word_list = ["MERCURY", "VENUS", "EARTH", "MARS", "JUPITER", "SATURN", "URANUS", "NEPTUNE", "PLUTO", "COMET"] 11 return random.choice(word_list) 12 13# 函数:显示单词的当前状态 14def display_word(word, correct_guesses): 15 return ' '.join([letter if letter in correct_guesses else '_' for letter in word]) 16 17# 函数:根据错误猜测次数显示绞刑架 18def display_hangman(incorrect_guesses): 19 hangman_stages = [ 20 """ 21 ------ 22 | | 23 | 24 | 25 | 26 | 27 -------- 28 """, 29 """ 30 ------ 31 | | 32 | O 33 | 34 | 35 | 36 -------- 37 """, 38 """ 39 ------ 40 | | 41 | O 42 | | 43 | 44 | 45 -------- 46 """, 47 """ 48 ------ 49 | | 50 | O 51 | /| 52 | 53 | 54 -------- 55 """, 56 """ 57 ------ 58 | | 59 | O 60 | /|\\ 61 | 62 | 63 -------- 64 """, 65 """ 66 ------ 67 | | 68 | O 69 | /|\\ 70 | / 71 | 72 -------- 73 """, 74 """ 75 ------ 76 | | 77 | O 78 | /|\\ 79 | / \\ 80 | 81 -------- 82 """ 83 ] 84 print(hangman_stages[len(incorrect_guesses)]) 85 86# 函数:显示当前游戏状态 87def display_game_state(current_display, incorrect_guesses, total_guesses, max_guesses): 88 display_hangman(incorrect_guesses) 89 print(f"Current word: {current_display}") 90 print(f"Incorrect guesses: {', '.join(incorrect_guesses)}") 91 print(f"Total guesses made: {total_guesses}") 92 print(f"Mistakes remaining: {max_guesses - len(incorrect_guesses)}") 93 94# 函数:获取玩家的猜测 95def get_player_guess(guessed_letters): 96 while True: 97 guess = input("Enter a letter: ").strip().upper() 98 if len(guess) != 1 or not guess.isalpha() or guess in guessed_letters: 99 print("Invalid guess. Please enter a single letter that you haven't guessed yet.") 100 else: 101 guessed_letters.add(guess) 102 return guess 103 104# 函数:根据玩家猜测更新游戏状态 105def update_game_state(guess, word, correct_guesses, incorrect_guesses, guessed_letters): 106 if guess in word: 107 correct_guesses.append(guess) 108 else: 109 incorrect_guesses.append(guess) 110 111# 函数:检查游戏状态(胜利/失败) 112def check_game_status(correct_guesses, incorrect_guesses, word): 113 if set(correct_guesses) == set(word): 114 print("Congratulations! You've guessed the word!") 115 return True 116 if len(incorrect_guesses) >= 6: 117 print(f"Game over! The word was: {word}") 118 return True 119 return False 120 121# 函数:询问玩家是否想再玩一次 122def ask_to_play_again(): 123 while True: 124 replay = input("Do you want to play again? (Y/N): ").strip().upper() 125 if replay in ('Y', 'N'): 126 return replay == 'Y' # 若输入'Y'则返回True,'N'则返回False 127 else: 128 print("Invalid input. Please enter 'Y' or 'N'.") 129 130# 主函数:运行游戏 131def play_game(): 132 while True: 133 word = select_word() 134 correct_guesses = [] 135 incorrect_guesses = [] 136 guessed_letters = set() 137 max_incorrect_guesses = 6 138 total_guesses = 0 139 140 while True: 141 clear_screen() 142 current_display = display_word(word, correct_guesses) 143 display_game_state(current_display, incorrect_guesses, total_guesses, max_incorrect_guesses) 144 145 guess = get_player_guess(guessed_letters) 146 update_game_state(guess, word, correct_guesses, incorrect_guesses, guessed_letters) 147 148 total_guesses += 1 149 150 if check_game_status(correct_guesses, incorrect_guesses, word): 151 break 152 153 # 调用新函数询问是否重玩 154 if not ask_to_play_again(): 155 print("Thanks for playing! Goodbye!") 156 break 157 158# 程序入口,启动游戏 159if __name__ == "__main__": 160 play_game()
1import os 2import random 3 4# Function to clear the screen 5def clear_screen(): 6 os.system('cls' if os.name == 'nt' else 'clear') 7 8# Function to select a word from the static list 9def select_word(): 10 word_list = ["MERCURY", "VENUS", "EARTH", "MARS", "JUPITER", "SATURN", "URANUS", "NEPTUNE", "PLUTO", "COMET"] 11 return random.choice(word_list) 12 13# Function to display the current state of the word 14def display_word(word, correct_guesses): 15 return ' '.join([letter if letter in correct_guesses else '_' for letter in word]) 16 17# Function to display the hangman based on incorrect guesses 18def display_hangman(incorrect_guesses): 19 hangman_stages = [ 20 """ 21 ------ 22 | | 23 | 24 | 25 | 26 | 27 -------- 28 """, 29 """ 30 ------ 31 | | 32 | O 33 | 34 | 35 | 36 -------- 37 """, 38 """ 39 ------ 40 | | 41 | O 42 | | 43 | 44 | 45 -------- 46 """, 47 """ 48 ------ 49 | | 50 | O 51 | /| 52 | 53 | 54 -------- 55 """, 56 """ 57 ------ 58 | | 59 | O 60 | /|\\ 61 | 62 | 63 -------- 64 """, 65 """ 66 ------ 67 | | 68 | O 69 | /|\\ 70 | / 71 | 72 -------- 73 """, 74 """ 75 ------ 76 | | 77 | O 78 | /|\\ 79 | / \\ 80 | 81 -------- 82 """ 83 ] 84 print(hangman_stages[len(incorrect_guesses)]) 85 86# Function to display the current game state 87def display_game_state(current_display, incorrect_guesses, total_guesses, max_guesses): 88 display_hangman(incorrect_guesses) 89 print(f"Current word: {current_display}") 90 print(f"Incorrect guesses: {', '.join(incorrect_guesses)}") 91 print(f"Total guesses made: {total_guesses}") 92 print(f"Mistakes remaining: {max_guesses - len(incorrect_guesses)}") 93 94# Function to get the player's guess 95def get_player_guess(guessed_letters): 96 while True: 97 guess = input("Enter a letter: ").strip().upper() 98 if len(guess) != 1 or not guess.isalpha() or guess in guessed_letters: 99 print("Invalid guess. Please enter a single letter that you haven't guessed yet.") 100 else: 101 guessed_letters.add(guess) 102 return guess 103 104# Function to update the game state based on the player's guess 105def update_game_state(guess, word, correct_guesses, incorrect_guesses, guessed_letters): 106 if guess in word: 107 correct_guesses.append(guess) 108 else: 109 incorrect_guesses.append(guess) 110 111# Function to check the game status (win/lose) 112def check_game_status(correct_guesses, incorrect_guesses, word): 113 if set(correct_guesses) == set(word): 114 print("Congratulations! You've guessed the word!") 115 return True 116 if len(incorrect_guesses) >= 6: 117 print(f"Game over! The word was: {word}") 118 return True 119 return False 120 121# Function to ask if the player wants to play again 122def ask_to_play_again(): 123 while True: 124 replay = input("Do you want to play again? (Y/N): ").strip().upper() 125 if replay in ('Y', 'N'): 126 return replay == 'Y' # Returns True if 'Y', False if 'N' 127 else: 128 print("Invalid input. Please enter 'Y' or 'N'.") 129 130# Main function to play the game 131def play_game(): 132 while True: 133 word = select_word() 134 correct_guesses = [] 135 incorrect_guesses = [] 136 guessed_letters = set() 137 max_incorrect_guesses = 6 138 total_guesses = 0 139 140 while True: 141 clear_screen() 142 current_display = display_word(word, correct_guesses) 143 display_game_state(current_display, incorrect_guesses, total_guesses, max_incorrect_guesses) 144 145 guess = get_player_guess(guessed_letters) 146 update_game_state(guess, word, correct_guesses, incorrect_guesses, guessed_letters) 147 148 total_guesses += 1 149 150 if check_game_status(correct_guesses, incorrect_guesses, word): 151 break 152 153 # Call the new function to ask about replaying 154 if not ask_to_play_again(): 155 print("Thanks for playing! Goodbye!") 156 break 157 158# Entry point to start the game 159if __name__ == "__main__": 160 play_game()

面向开发者效率的AI助手

结论:从课程到职业