In [5]:
import random
def saveIdiomsList():
fh = open(r'f:\temp\idioms.txt')
text = fh.read()
text = text.replace('、', '')
idioms=text.split('\n')
fh.close()
fh=open(r'f:\temp\idioms_correct.txt','w')
for idiom in idioms:
idiom=idiom+'\n'
fh.write(idiom)
fh.close()
def savePoemsList():
fh = open(r'f:\temp\poems.txt')
text = fh.read()
text = text.replace('\n', '')
text = text.replace('、', '\n')
text = text.replace(',', '\n')
text = text.replace('。', '\n')
text = text.replace('?', '\n')
text = text.replace('!', '\n')
poems=text.split('\n')
fh.close()
fh=open(r'f:\temp\poems_correct.txt','w')
for poem in poems:
poem=poem+'\n'
fh.write(poem)
fh.close()
def idiom_game():
filename = r'f:\temp\idioms_correct.txt'
score = 10
while score >= 0:
real_idiom = idiom_robot(filename)
answer_idiom = input('请输入猜测成语,回车结束,直接回车表示退出游戏:')
if answer_idiom == real_idiom:
print('答对了,加十分')
score += 10
print('你当前的分数是:', score)
if score == 100:
win()
return
elif answer_idiom == '':
print('退出游戏。')
print('你最后的分数是:', score)
return
else:
score -= 10
print('答错了,减十分')
print('成语其实是:', real_idiom)
print('你当前的分数是:', score)
else:
lose()
return
def idiom_robot(file_name):
with open(file_name) as fh:
text = fh.read()
idioms = text.split()
idiom = random.choice(idioms)
chs = get_ch_table(text.replace(r'\n', ''))
guess_ch_table = [ch for ch in idiom]
while len(guess_ch_table) < 6:
ch = random.choice(chs)
if ch not in guess_ch_table:
guess_ch_table.append(ch)
random.shuffle(guess_ch_table)
for i in range(0,6,2):
print(guess_ch_table[i], guess_ch_table[i+1])
return idiom
def poem_game():
filename = r'f:\temp\poems_correct.txt'
score = 10
while score >= 0:
real_poem = poem_robot(filename)
answer_poem = input('请输入猜测诗句,回车结束,直接回车表示退出游戏:')
if answer_poem == real_poem:
print('答对了,加十分')
score += 10
print('你当前的分数是:', score)
if score == 100:
win()
return
elif answer_poem == '':
print('退出游戏。')
print('你最后的分数是:', score)
return
else:
score -= 10
print('答错了,减十分')
print('诗句其实是:', real_poem)
print('你当前的分数是:', score)
else:
lose()
return
def poem_robot(file_name):
with open(file_name) as fh:
text = fh.read()
poems = text.split()
poem = random.choice(poems)
chs = get_ch_table(text.replace(r'\n', ''))
guess_ch_table = [ch for ch in poem]
if(len(poem)==5):
while len(guess_ch_table) < 9:
ch = random.choice(chs)
if ch not in guess_ch_table:
guess_ch_table.append(ch)
random.shuffle(guess_ch_table)
for i in range(0,9,3):
print(guess_ch_table[i], guess_ch_table[i+1], guess_ch_table[i+2])
else:
while len(guess_ch_table) < 12:
ch = random.choice(chs)
if ch not in guess_ch_table:
guess_ch_table.append(ch)
random.shuffle(guess_ch_table)
for i in range(0,12,3):
print(guess_ch_table[i], guess_ch_table[i+1], guess_ch_table[i+2])
return poem
def get_ch_table(line):#字表统计
ch_table = []
for ch in line:
if ch not in ch_table:
ch_table.append(ch)
return ch_table
def menu():
print('''
=====游戏菜单=====
1. 游戏说明
2. 开始成语游戏
3. 开始诗词游戏
4. 退出游戏
5. 制作团队
=====游戏菜单=====
'''
)
def show_instruction():
print('''
=====INSTRUCTION=====
Please guess the right idiom/poem
from the charactors in box
=====INSTRUCTION=====
'''
)
def welcome():
print(
'''
======欢迎来到猜成语/诗词游戏=======
/ \ , ,
_._ _ |oo| _ / \__/ \
_|||| ((/ () \)) / \
|||||/| ( ==== ) |oo|
\____/ _`\ /'_ / \
/ /.-' /\<>/\ `\.( () )_._
| ` / \/ \ /`'--'////)
\__,-'`| |. |\/ |/\/\|"\"`
| |. | \___/\___/
| |. | | |
======欢迎来到猜成语/诗词游戏=======
'''
)
def game_over():
print(
'''
======GAME OVER=======
_________
/ ======= \
/ __________\
| ___________ |
| | - | |
| | | |
| |_________| |________________
\=____________/ )
/ """"""""""" \ /
/ ::::::::::::: \ =D-'
(_________________)
======GAME OVER=======
'''
)
def win():
print(
'''
======恭喜你,你赢了=======
."". ."",
| | / /
| | / /
| | / /
| |/ ;-._
} ` _/ / ;
| /` ) / /
| / /_/\_/\
|/ / |
( ' \ '- |
\ `. /
| |
| |
======恭喜你,你赢了=======
'''
)
def lose():
print(
'''
======抱歉,你输了=======
.-" "-.
/ \
| |
|, .-. .-. ,|
| )(__/ \__)( |
|/ /\ \|
(@_ (_ ^^ _)
_ ) \_______\__|IIIIII|__/__________________________
(_)@8@8{}<________|-\IIIIII/-|___________________________>
)_/ \ /
(@ `--------`
======抱歉,你输了=======
'''
)
def show_team():
print('''
=====TEAM COMPOSITION=====
Programmer:Rita
Designer:Rita
Director: Rita
=====TEAM COMPOSITION=====
'''
)
def main():
saveIdiomsList()
savePoemsList()
while True:
menu()
choice = int(input('请输入你的选择'))
if choice == 1:
show_instruction()
elif choice == 2:
welcome()
idiom_game()
elif choice == 3:
welcome()
poem_game()
elif choice == 4:
game_over()
break
else:
show_team()
main()