如何让玩家知道他们已经赢了子手?

时间:2019-06-10 01:58:13

标签: python

我正在研究python中的神秘文字游戏(hang子手)问题。在大多数情况下,除了告诉玩家他们实际上已经赢得了比赛之外,其他所有事情都起作用。

我采用了多种方法来尝试解决此问题。

不幸的是,到目前为止,我已经允许我告诉用户他们已经正确猜出了他们赢得的单词。如果有人可以为我提供解决方案或指导我朝正确的方向发展,我将不胜感激!

def guess_taker():
    import random
    f = open("words.txt")
    all_words = (f.read()).upper()


    split_all_words = all_words.split()

    easy_words = []
    normal_words = []
    hard_words = []
    for word in split_all_words:
        if len(word) >= 8:
            hard_words.append(word)
        elif len(word) >= 6 and len(word) <= 8:
            normal_words.append(word)
        elif len(word) <= 6 and len(word) >= 4:
            easy_words.append(word)


    difficulty_selector = input("Welcome to the Mystery Word Game!\n\nPlease enter your difficulty level:\n\nEASY\nNORMAL\nHARD\n")
    upper_case_difficulty = difficulty_selector.upper()

    while upper_case_difficulty not in ["EASY", "NORMAL", "HARD"]:
        print("INVALID ENTRY: Please try again!")
        difficulty_selector = input("Welcome to the Mystery Word Game!\n\nPlease enter your difficulty level: \n\nEASY\nNORMAL\nHARD\n")
        upper_case_difficulty = difficulty_selector.upper()


    if upper_case_difficulty == "EASY":
        word = random.choice(easy_words)
    elif upper_case_difficulty == "NORMAL":
        word = random.choice(normal_words)
    elif upper_case_difficulty == "HARD":
        word = random.choice(hard_words)

    word_match = word.replace("", " ")
    word_match = word_match.strip()

    print("This word is",len(word),"characters long")
    current_guesses= []

    def display_letter(letter, guesses):
        """
        Conditionally display a letter. If the letter is already in
        the list `guesses`, then return it. Otherwise, return "_".
        """
        if letter in guesses:
            return letter
        else:
            return "_"

    def print_word(word, guesses):
        output_letters = []
        for letter in word:
            output_letters.append(display_letter(letter, guesses))
        print("".join(output_letters))

    guess_counter = 8
    print(word_match)
    print("Lives remaining: ", guess_counter)
    guess = input("Please input your letter guess: ").upper()


    wordGuessed = False

    while guess_counter > 0 and wordGuessed is False:
        if word == print_word(word, current_guesses):
            wordGuessed is True
            print("You won")
        if len(guess) != 1:
            print("ERROR! Please enter one letter.")       
        if guess in word and len(guess) == 1 and guess not in current_guesses:
            print("Correct!", guess, "is in the word!")
        if guess not in word and guess not in current_guesses:
            print("Sorry,", guess, "is not in the word.")
            guess_counter -= 1
        if guess in current_guesses:
            print("INVALID ENTRY! Already been guessed!")
        if guess not in current_guesses and len(guess) == 1:
            current_guesses.append(guess)
        if guess_counter > 0:
            print_word(word, current_guesses)
            print("Current guesses: ", current_guesses)
            print("Lives remaining: ", guess_counter, "\n")
            guess = input("Please input your letter guess:").upper()

    print("GAME OVER! The word was", word.upper())
    end_game = input("Play again? Type Yes or No. ")
    end_game = end_game.lower()

    if end_game == "yes":
        guess_taker()
    else:
        print("Good-bye!")

guess_taker()

该程序当前需要一个文本文件来导入单词列表。我还更改了程序,以告诉您单词的含义,以便您轻松“赢”

2 个答案:

答案 0 :(得分:0)

我看不到您的代码有问题。您说过,您想告诉玩家他们赢了,但是您的代码中已经包含了:

while guess_counter > 0 and wordGuessed is False:
        if word == print_word(word, current_guesses):
            wordGuessed is True
            print("You won")

答案 1 :(得分:0)

让我们更改print_word使其返回单词以进行比较:

def print_word(word, guesses):
    output_letters = []
    for letter in word:
        output_letters.append(display_letter(letter, guesses))
    return ("".join(output_letters))

并移动条件,包括获得胜利的休息时间:

while guess_counter > 0 and wordGuessed is False:
    if len(guess) != 1:
        print("ERROR! Please enter one letter.")
    if guess in word and len(guess) == 1 and guess not in current_guesses:
        print("Correct!", guess, "is in the word!")
        if guess not in current_guesses and len(guess) == 1:
            current_guesses.append(guess)
            if word == print_word(word, current_guesses):
                wordGuessed is True
                print("You won")
                break
    if guess not in word and guess not in current_guesses:
        print("Sorry,", guess, "is not in the word.")
        guess_counter -= 1
    if guess in current_guesses:
        print("INVALID ENTRY! Already been guessed!")
    if guess_counter > 0:
        print(print_word(word, current_guesses))
        print("Current guesses: ", current_guesses)
        print("Lives remaining: ", guess_counter, "\n")
        guess = input("Please input your letter guess:").upper()