In [1]:
#完整诗词游戏

#欢迎界面 done
#游戏菜单 done
#进入成语游戏
#进入诗词游戏
#退出游戏
#显示游戏制作团队 done

import itertools, random

def welcome():
    print('Welcome to the funny game!')

def menu():
    print('''=====game menu=====
                1.guess idioms
                2.guess poems
                3.show the producing team
                4.exit
             =====game menu=====''')
    
def show():
    print('''The producing team of this game:
             No.201611680595 from IS1.''')
    
def win():
    print('Congrats! You win!')
    
def lose():
    print('Sorry, you lose.')
    
def exit():
    print('Game over!')
    

def get_ch_table(line):                     #令字符表`ch_talbe`为空列表,取字符串中每一个字:如果该字不在字符表中则:将其加入字符表
    ch_table = []                           #返回字符表
    for ch in line:
        ch_table.append(ch)
    return ch_table
            
def guess_idioms():
    fh = open(r'd:\temp\idioms_correct.txt')
    text = fh.read()
    chs = get_ch_table(text.replace(r'\n',''))
    
    already_guess = []
    score = 0
    idioms = text.split()
    fh.close()
    
    if score >= 0:
        for i in range(15):
            idiom = random.choice(idioms)
            if idiom in already_guess:
                continue
            else:
                win()
                print('Your final score is: ',score)
        
            guess_ch_table = [ch for ch in idiom] #随机从字表中抽取不在成语中的2个字,与随机得到的成语idiom中的4个字
            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):                            #合并为6个字以3X2的形态输出。
                print(guess_ch_table[i],guess_ch_table[i+1])
        
            guess = input('please enter an idiom you guess, end with a tab.')
            if guess == idiom:
                score += 10
                already_guess.append(guess)
                idioms.remove(guess)
            else:
                score -= 10
        else:
            lose()
            exit()
        
def guess_poems():
    fh = open(r'd:\temp\诗词大全.txt')
    poems = fh.read().split()
    text = random.choice(poems)
    fh.close()
    
    score = 0
    guess = input('please enter the sentence you guess,end with a tab.')
    guesses = itertools.permutation(text,7)
    
    
    for guess in guesses:
        if guess in poems:
            win()
        else:
            lose()
            exit()
        
    
def main():
    while True:
        welcome()
        menu()
        choice = int(input('please enter a number of your choice: '))
        if choice == 1:
            guess_idioms()
        elif choice == 2:
            guess_poems()
        elif choice == 3:
            show()
        elif choice == 4:
            exit()
                     
            
#主程序
if __name__ == '__main()__':
    main()