1、计算机必须从文本文件 gone with the wind.txt 中随机选择一个单词命名为 secretWord,单词可能包含大写和小写字母,为降低猜测难度,需要将单词转换为全部小写字母。
2、开始游戏时,用户会得到单词长度为几个字符的提示和有多少次猜测机会的提示。
3、游戏是交互的,用户每次输入猜测的字母 letter 后,计算机会给出相应的结果。
4、当用户猜到单词或用完猜测次数,游戏结束。
python猜单词游戏的实现
可以借鉴下,挺详细的
https://www.ab62.cn/article/5580.html
import random
# 从文本文件中随机选择一个单词并将其转换为小写字母
def choose_secret_word():
# 从文件中读取所有单词
with open('gone with the wind.txt', 'r') as f:
words = f.read().split()
# 从单词列表中随机选择一个单词
secret_word = random.choice(words)
# 将单词转换为小写字母
secret_word = secret_word.lower()
return secret_word
# 开始游戏
def play_game():
# 选择一个单词
secret_word = choose_secret_word()
# 获取单词长度和猜测次数
word_length = len(secret_word)
guess_count = get_guess_count(word_length)
# 提示用户单词长度和猜测次数
print(f'单词长度: {word_length}')
print(f'猜测次数: {guess_count}')
# 循环进行猜测
for i in range(guess_count):
# 提示用户输入猜测的字母
letter = input('请输入一个字母: ')
# 处理用户的猜测
result = process_guess(letter, secret_word)
# 如果猜到了单词,游戏结束
if result == 'win':
print('恭喜你,猜对了!')
break
# 如果猜错了,扣除一次机会
elif result == 'lose':
guess_count -= 1
print(f'猜错了,还剩 {guess_count} 次机会')
# 否则用户猜中了一个字母
else:
print('猜中一个字母,继续加油!')
#获取根据单词长度计算出的猜测次数
def get_guess_count(word_length):
#可以根据需要在这里设定猜测次数的计算方式
return word_length
#处理用户的猜测
def process_guess(letter, secret_word):
#如果猜测的字母在单词中,返回 'correct'
if letter in secret_word:
return 'correct'
#如果猜测的字母不在单词中,返回 'lose'
else:
return 'lose'
#开始游戏
play_game()
import random
def read_file(file):
print("正在从文件加载单词列表...")
with open(file, 'r',encoding = 'utf-8') as novel:
txt = novel.read()
symbols = '''!"#$%&()*+,-.:;[\'][\"]<=>?@[\\]^_'{|}~/'''
for ch in symbols:
txt = txt.replace(ch, " ")
txt = txt.lower()
print(f"成功加载{len(txt.split())}个单词")
return txt.split()
def secret_word(ls):
return random.choice(ls)
def get_guessed_word(cover_word, word, letter):
pos = word.find(letter)
if pos != -1:
cover_word[pos] = letter
print(f"单词现状:{''.join(cover_word)}")
return cover_word
def word_guess(word):
print(f"单词{word}的长度为{len(word)}")
print(f"你可以猜测{2*len(word)}次")
guess_word =['_ '] * len(word)
tmp_word = word
for _ in range(2*len(word)):
input_alpha = input("请输入字母...")
guess_word = get_guessed_word(guess_word, tmp_word, input_alpha)
tmp_pos = tmp_word.find(input_alpha)
if tmp_pos != -1:
tmp_word = tmp_word[:tmp_pos] + '&' + tmp_word[tmp_pos+1:]
if ''.join(guess_word) == word:
print(f"你已猜测了{_+1}次,全部已猜出")
break
print(f"你已猜测了{_+1}次,还剩{2*len(word)-(_+1)}次。")
ls = read_file('gone with the wind.txt')
guess_word = secret_word(ls)
print(guess_word)
word_guess(guess_word)
--result
正在从文件加载单词列表...
成功加载7651个单词
captain
单词captain的长度为7
你可以猜测14次
请输入字母...a
单词现状:_ a_ _ _ _ _
你已猜测了1次,还剩13次。
请输入字母...c
单词现状:ca_ _ _ _ _
你已猜测了2次,还剩12次。
请输入字母...p
单词现状:cap_ _ _ _
你已猜测了3次,还剩11次。
请输入字母...n
单词现状:cap_ _ _ n
你已猜测了4次,还剩10次。
请输入字母...t
单词现状:capt_ _ n
你已猜测了5次,还剩9次。
请输入字母...i
单词现状:capt_ in
你已猜测了6次,还剩8次。
请输入字母...a
单词现状:captain
你已猜测了7次,全部已猜出
import random
def get_random_word(filename):
# 从文本文件中随机选择一个单词
with open(filename, "r") as f:
words = f.read().split()
secret_word = random.choice(words).lower()
return secret_word
def play_game(secret_word):
# 开始游戏
max_guesses = len(secret_word) + 3
letters_guessed = []
print("单词长度为" + str(len(secret_word)) + "个字符。")
print("你有" + str(max_guesses) + "次猜测机会。")
while max_guesses > 0:
output = ""
for letter in secret_word:
if letter in letters_guessed:
output += letter
else:
output += "_"
if output == secret_word:
print("恭喜你,猜对了!单词是:" + secret_word)
break
print("单词:" + output)
print("还有" + str(max_guesses) + "次机会。")
letter = input("请输入一个字母:")
letter = letter.lower()
if letter in letters_guessed:
print("你已经猜过这个字母了。")
elif letter in secret_word:
letters_guessed.append(letter)
print("猜对了!")
else:
print("猜错了。")
max_guesses -= 1
if max_guesses == 0:
print("很遗憾,你输了。单词是:" + secret_word)
filename = "gone with the wind.txt"
secret_word = get_random_word(filename)
play_game(secret_word)