完整诗词游戏


In [4]:
def win():
    print('win!')

In [5]:
def lose():
    print('lose!')

In [1]:
def get_ch_table(line):   #返回汉字表
    ch_table = []
    for ch in line:
        if ch not in ch_table:
            ch_table.append(ch)
    return ch_table

In [2]:
def get_whole(filename):  #返回全体列表
    with open(filename) as fh:
        text = fh.read()
    whole = text.split()
    return whole

In [2]:
def poem_robot(poems):   #随机选择一句诗并在列表中删除,打印打乱顺序的猜诗字表,并返回正确诗句
    poem = random.choice(poems)
    poems.remove(poem)
    chs = get_ch_table(text.replace(r'\n', ''))

    guess_ch_table = [ch for ch in poem]
    n = len(guess_ch_table)
    
    if n == 5:
        while len(guess_ch_table) < 9:
            ch = random.choice(chs)
            if ch not in guess_ch_table:
                guess_ch_table.append(ch)
                
    if n == 7:
        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)
    
    print(guess_ch_table)
    
    return poem

In [ ]:
def idiom_robot(idioms):
    idiom = random.choice(idioms)
    chs = get_ch_table(text.replace(r'\n', ''))
    idioms.remove(idiom)

    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

In [ ]:
def play_idiom():
    score = 10
    while score >= 0:
        real_idiom = idiom_robot(idioms)
        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

In [ ]:
def play_poem()    #猜诗游戏
    score = 10
    while score >= 0:
        real_poem = poem_robot(poems)
        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

In [3]:
def main():      #运行整个游戏
    filename1 = r'd:\temp\idioms_correct.txt'
    filename2 = r'd:\temp\poems_correct.txt'
    n = int(input('点字成语输入1,点字成诗输入2:'))
    
    if n == 2:
        poems = get_whole(filename2)
        play_poem()
        
    if n == 1:
        idioms = get_whole(filename1)
        play_idiom()
        
    else:
        print('退出游戏。')
        return
           
if __name__ == '__main()__':
    main()