Python中的刽子手游戏
刽子手维基:
刽子手的起源是晦涩难懂的意思,没有被发现,但它似乎出现在维多利亚时代,”《牛津文字游戏指南》的作者托尼·奥加德 (Tony Augarde) 说。该游戏在 1894 年 Alice Bertha Gomme 的“传统游戏”中以“鸟、兽和鱼”的名称被提及。规则很简单;一个玩家写下一个单词的第一个和最后一个字母,另一个玩家猜中间的字母。在其他来源中,[在哪里?] 游戏被称为“绞刑架”、“Hangin 游戏”或“Hanger”。
执行
这是一个使用Python编程语言的简单刽子手游戏。初学者可以将此作为一个小项目来提高他们的编程技能和理解逻辑。
- Hangman 程序从密语列表中随机选择一个密语。 random 模块将提供此功能,因此程序中的第 1 行将其导入。
- 游戏:在这里,从我们的收藏中随机抽取一个单词(水果名称),玩家赢得游戏的机会有限。
- 当该单词中的一个字母被正确猜到时,该单词中的该字母位置就会变得可见。这样,在所有机会结束之前,单词的所有字母都将被猜到。
- 为方便起见,我们给出了单词长度 + 2 次机会。例如,要猜的单词是 mango,那么用户有 5 + 2 = 7 次机会,因为 mango 是一个五个字母的单词。
Python
# Python Program to illustrate
# Hangman Game
import random
from collections import Counter
someWords = '''apple banana mango strawberry
orange grape pineapple apricot lemon coconut watermelon
cherry papaya berry peach lychee muskmelon'''
someWords = someWords.split(' ')
# randomly choose a secret word from our "someWords" LIST.
word = random.choice(someWords)
if __name__ == '__main__':
print('Guess the word! HINT: word is a name of a fruit')
for i in word:
# For printing the empty spaces for letters of the word
print('_', end = ' ')
print()
playing = True
# list for storing the letters guessed by the player
letterGuessed = ''
chances = len(word) + 2
correct = 0
flag = 0
try:
while (chances != 0) and flag == 0: #flag is updated when the word is correctly guessed
print()
chances -= 1
try:
guess = str(input('Enter a letter to guess: '))
except:
print('Enter only a letter!')
continue
# Validation of the guess
if not guess.isalpha():
print('Enter only a LETTER')
continue
else if len(guess) > 1:
print('Enter only a SINGLE letter')
continue
else if guess in letterGuessed:
print('You have already guessed that letter')
continue
# If letter is guessed correctly
if guess in word:
k = word.count(guess) #k stores the number of times the guessed letter occurs in the word
for _ in range(k):
letterGuessed += guess # The guess letter is added as many times as it occurs
# Print the word
for char in word:
if char in letterGuessed and (Counter(letterGuessed) != Counter(word)):
print(char, end = ' ')
correct += 1
# If user has guessed all the letters
else if (Counter(letterGuessed) == Counter(word)): # Once the correct word is guessed fully,
# the game ends, even if chances remain
print("The word is: ", end=' ')
print(word)
flag = 1
print('Congratulations, You won!')
break # To break out of the for loop
break # To break out of the while loop
else:
print('_', end = ' ')
# If user has used all of his chances
if chances <= 0 and (Counter(letterGuessed) != Counter(word)):
print()
print('You lost! Try again..')
print('The word was {}'.format(word))
except KeyboardInterrupt:
print()
print('Bye! Try again.')
exit()
注意:请在您的终端上运行该程序。
omkarpathak@omkarpathak-Inspiron-3542:~/Documents/
Python-Programs$ python P37_HangmanGame.py
Guess the word! HINT: word is a name of a fruit
_ _ _ _ _
Enter a letter to guess: m
_ _ m _ _
Enter a letter to guess: o
_ _ m o _
Enter a letter to guess: l
l _ m o _
Enter a letter to guess: e
l e m o _
Enter a letter to guess: n
l e m o n
Congratulations, You won!
自己试试练习:
- 您可以通过在每次猜测后添加计时器来进一步增强程序
- 您还可以从一开始就给出提示,使用户更容易完成任务
- 只有当玩家的猜测错误时,您也可以将机会减少一。如果猜对了,
玩家的机会没有减少