In [2]:
def welcome():
print('Welcome to the game.')
def win():
print('Congratulation !you win!')
def lose():
print('Sorry you lose. ')
def gameover():
print('''
GAMEOVER!''')
def good_bye():
print('Good bye!')
In [3]:
def get_idioms (filename):
fh=open(filename)
text=fh.read()
fh.close()
idioms=text.split()
return idioms
def get_chtable(filename):
fh=open(filename)
text=fh.read()
text.replace('\n','')
ch_table=[]
for ch in text :
if ch not in ch_table:
ch_table.append(ch)
return ch_table
def get_gusstable(idiom,ch_table):
guss_table=[ch for ch in idiom]
while len(guss_table)<6:
ch=random.choice(ch_table)
if ch not in guss_table:
guss_table.append(ch)
random.shuffle(guss_table)
print()
for i in range(0,6,2):
print(guss_table[i],guss_table[i+1],sep=' ')
In [4]:
def play_gussidiom():
filename=r'd:/temp/idioms_correct.txt'
idioms=get_idioms(filename)
ch_table=get_chtable(filename)
score=10
welcome()
print('你的初始分数是:',score,'分')
while score>-10:
idiom=random.choice(idioms)
get_gusstable(idiom,ch_table)
your_guss=input('请输入你猜的成语: ')
if your_guss==idiom :
score+=10
if score==100:
print('你当前的分数是:',score,'分')
win()
break
print('你猜对了!你当前的分数是:',score,'分')
elif your_guss=='':
break
else:
score-=10
print('你猜错了!你当前的分数是:',score,'分')
print('正确的成语是:',idiom)
else:
lose()
gameover()
In [5]:
def find_poem_word_table(poems):
poem_word_table=[]
for poem in poems :
for ch in poem:
if ch not in poem_word_table:
poem_word_table.append(ch)
return poem_word_table
In [6]:
import random
def build_guss_table(poem,table):
guss_table=[ch for ch in poem]
if len(poem)==5:
while len(guss_table)<9:
ch=random.choice(table)
if ch not in guss_table:
guss_table.append(ch)
random.shuffle(guss_table)
for i in range(0,9,3):
print(guss_table[i],guss_table[i+1],guss_table[i+2],sep=' ')
else:
while len(guss_table)<12:
ch=random.choice(table)
if ch not in guss_table:
guss_table.append(ch)
random.shuffle(guss_table)
print()
for i in range(0,12,4):
print(guss_table[i],guss_table[i+1],guss_table[i+2],guss_table[i+3],sep=' ')
In [7]:
import random
def play_poems_guss():
score=10
print('你的基础分是:',score)
filename='d:/temp/诗词大全.txt'
with open(filename) as fh: #得到诗句列表
poems=fh.read().split()
table=find_poem_word_table(poems) #得到字表
while score>-10:
poem=random.choice(poems)
build_guss_table(poem,table)
your_guss=input('请输入你猜的诗句:')
if your_guss==poem:
score+=10
if score==100:
print('你当前的分数是:',score,'分')
win()
break
print('你猜对了!你当前的分数是:',score,'分')
elif your_guss=='':
break
else:
score-=10
print('你猜错了!你当前的分数是:',score,'分')
print('正确的成语是:',poem)
else:
lose()
gameover()
In [8]:
def menu():
print('''
==================
1.游戏说明
2.点字成语
3.点字成诗
4.团队介绍
5.退出游戏
===================''')
def instruction():
print('''
游戏介绍:
1.点字成语:
本游戏将给出6个字其中包含所猜成语,请根据给出的字表猜出成语
2.点字成诗:
点字成诗游戏基本与中国诗词大会中的相同,这里仅考虑5言及7言诗词句,前者给出3X3共9个字,包含5言中的所有字,但不与其重复,随机打乱
请你根据给出的字表猜出对应的诗句。''')
def team_instruction():
print('''
=======================
主创人:X-star
成员:熊猫
竹子
发行人:
金毛
=======================''')
In [10]:
def main():
welcome()
while 1:
menu()
choice=int(input('Please enter your choice.'))
if choice==1:
instruction()
elif choice==2:
play_gussidiom()
elif choice==3:
play_poems_guss()
elif choice==4:
team_instruction()
else:
print('你确定要退出游戏吗? True or False')
to=input()
if to :
break
good_bye()
if __name__=='__main__':
main()
In [11]:
def find_poem_sentence(poems, characters):
for poem in poems:
same_character_number = 0
for ch in poem:
if ch in characters:
same_character_number += 1
if same_character_number == 5:
return poem
return None
with open(r'd:/temp/诗词大全.txt') as fh:
poems=fh.read().split()
list='卷 白 扬望 酒 云置 晚 明'
list.replace(' ','')
print(find_poem_sentence(poems,list))