编写代码,实现成语接龙: (1)可以进行人机对战 (2)有三个难度:Hard: 接上的成语的首字必须和题目的末字相同才行(课上已经实现,自己写一下)、Middle: 接上的成语的首字和题目末字拼音相同即可(声调也要相同)、Easy: 接上的成语的首字和题目末字拼音相同(无需声调相同)
时间: 2024-03-09 09:46:30 浏览: 17
好的,以下是 Python 代码实现:
```python
import random
import pinyin
def read_file(file_path):
with open(file_path, 'r', encoding='utf-8') as f:
data = f.read()
return data.split()
def get_pinyin(word):
return pinyin.get(word, format='strip', delimiter='')
def get_last_pinyin(word):
return get_pinyin(word)[-1]
def get_first_pinyin(word):
return get_pinyin(word)[0]
def get_word_by_last_char(char, word_list):
candidates = [word for word in word_list if word[0] == char]
if not candidates:
return None
return random.choice(candidates)
def get_word_by_last_pinyin(pinyin, word_list):
candidates = [word for word in word_list if get_first_pinyin(word) == pinyin]
if not candidates:
return None
return random.choice(candidates)
def get_word_by_last_pinyin_with_tone(pinyin, word_list):
candidates = [word for word in word_list if get_first_pinyin(word) == pinyin]
if not candidates:
return None
return random.choice(candidates)
def get_user_input(difficulty):
if difficulty == 'hard':
return input('请回答,成语接龙(Hard):\n')
elif difficulty == 'middle':
return input('请回答,成语接龙(Middle):\n')
elif difficulty == 'easy':
return input('请回答,成语接龙(Easy):\n')
def play_game(start_word, word_list, difficulty):
current_word = start_word
while True:
print(f'当前成语:{current_word}')
if difficulty == 'hard':
user_input = get_user_input('hard')
if user_input[0] != current_word[-1]:
print('回答错误!')
return False
elif user_input not in word_list:
print('不符合成语规范或不在词库中!')
return False
elif get_last_pinyin(user_input) != get_first_pinyin(current_word):
print('回答错误!')
return False
elif difficulty == 'middle':
user_input = get_user_input('middle')
if user_input[0] != current_word[-1]:
print('回答错误!')
return False
elif user_input not in word_list:
print('不符合成语规范或不在词库中!')
return False
elif get_last_pinyin(user_input) != get_first_pinyin(current_word):
if get_last_pinyin_with_tone(user_input) != get_first_pinyin_with_tone(current_word):
print('回答错误!')
return False
elif difficulty == 'easy':
user_input = get_user_input('easy')
if user_input[0] != current_word[-1]:
print('回答错误!')
return False
elif user_input not in word_list:
print('不符合成语规范或不在词库中!')
return False
elif get_last_pinyin(user_input) != get_first_pinyin(current_word):
print('回答错误!')
return False
current_word = user_input
print('回答正确!')
print('--------------')
def main():
word_list = read_file('idiom.txt')
start_word = random.choice(word_list)
print(f'开始成语:{start_word}')
while True:
play_again = input('是否再来一局?(Y/N)\n')
if play_again.lower() == 'y':
start_word = get_word_by_last_char(start_word[-1], word_list)
if not start_word:
print('无法继续接龙,游戏结束!')
break
if play_game(start_word, word_list, 'hard') is False:
print('游戏结束!')
break
else:
break
if __name__ == '__main__':
main()
```
其中,`idiom.txt` 是成语词库文件,需要自行准备。难度分别为 Hard、Middle、Easy,对应三种不同的拼音匹配方式。运行代码后,会先随机生成一个开始成语,然后进入游戏循环。每次游戏结束后,会询问玩家是否再来一局。