2013-11-04 44 views
2

我是編程新手,目前正在嘗試使用Python 3製作Hangman的簡化遊戲。大多數情況下,除了最重要的部分之外,我已經將它關閉了,如何隱藏隨機單詞的字母,然後一旦猜到如何揭示它們。主要是,我只需要一個快速的答案。任何幫助將不勝感激。這裏是我到目前爲止的代碼(抱歉,如果它的長,就像我說的,我比較新,在此,再次感謝!):如何隱藏/顯示Python遊戲中的字母?

import random 

#list of words for game 
hangmanWords = ("Halloween","Hockey","Minnesota","Vikings","Twins","Timberwolves","Wild","Playstation","Achievement","Minecraft","Metallica","Portal","Xbox","Guitar") 

#randomizes the word chosen for game 
index = random.randint(0,len(hangmanWords)-1) 

#assigns radomized word to variable 
randomWord = hangmanWords[index] 


''' 
menu function, provides user with choices for game, user chooses via imput 
''' 
def menu(): 
    print(""" 
Welcome to Hangman! 

Select a difficulty: 
1. Easy (9 Misses) 
2. Medium (7 Misses) 
3. Advanced (5 Misses) 

4. Exit Game 
""") 
    selection = int(input("What difficulty do you pick (1-4)?: ")) 
    return selection 

''' 
the function for easy mode, prints the word chosen by randomizer 
asks the player to enter a letter that they guess is in the word 
player gets 9 guesses to figure out word, correct guesses don't 
count, returns player to main menu when they lose 
''' 
def easyMode(): 
    wrongGuesses = 0 
    listOfGuesses = [] 
    print(randomWord) 
    while(wrongGuesses != 9): 
     x = input("Enter a letter: ") 
     if x.lower() in randomWord.lower(): 
     print(x,"is in the word!") 
     listOfGuesses.append(x) 
     print("Letters guessed so far: ",listOfGuesses) 
     print() 
     else: 
     print(x,"is not in the word.") 
     wrongGuesses += 1 
     print(wrongGuesses, "wrong guesses.") 
     listOfGuesses.append(x) 
     print("Letters guessed so far: ",listOfGuesses) 
     print() 

    print("You lost the game!") 
    return x 

''' 
the function for medium mode, prints the word chosen by randomizer 
asks the player to enter a letter that they guess is in the word 
player gets 7 guesses to figure out word, correct guesses don't 
count, returns player to main menu when they lose 
''' 
def medium(): 
    wrongGuesses = 0 
    listOfGuesses = [] 
    print(randomWord) 
    while(wrongGuesses != 7): 
     x = input("Enter a letter: ") 
     if x.lower() in randomWord.lower(): 
     print(x,"is in the word!") 
     listOfGuesses.append(x) 
     print("Letters guessed so far: ",listOfGuesses) 
     print() 
     else: 
     print(x,"is not in the word.") 
     wrongGuesses += 1 
     print(wrongGuesses, "wrong guesses.") 
     listOfGuesses.append(x) 
     print("Letters guessed so far: ",listOfGuesses) 
     print() 

    print("You lost the game!") 
    return x 

''' 
the function for advanced mode, prints the word chosen by randomizer 
asks the player to enter a letter that they guess is in the word 
player gets 5 guesses to figure out word, correct guesses don't 
count, returns player to main menu when they lose 
''' 
def advanced(): 
    wrongGuesses = 0 
    listOfGuesses = [] 
    print(randomWord) 
    while(wrongGuesses != 5): 
     x = input("Enter a letter: ") 
     if x.lower() in randomWord.lower(): 
     print(x,"is in the word!") 
     listOfGuesses.append(x) 
     print("Letters guessed so far: ",listOfGuesses) 
     print() 
     else: 
     print(x,"is not in the word.") 
     wrongGuesses += 1 
     print(wrongGuesses, "wrong guesses.") 
     listOfGuesses.append(x) 
     print("Letters guessed so far: ",listOfGuesses) 
     print() 

    print("You lost the game!") 
    return x 

''' 
main function, deals with what happens depending on 
what the player selected on the menu 
''' 
def main(): 
    select = menu() 
    while(select != 4): 
     if(select == 1): 
     easyMode() 
     elif(select == 2): 
     medium() 
     elif(select == 3): 
     advanced() 

     select = menu() 

    print("You don't want to play today? :'(") 

main() 
+0

您可以直接使用'randomWord = random.choice(hangmanWords)'。 –

回答

0

如果你想嘗試一下自己,那麼請不要看代碼呢。就像在hang子手一樣,我們可以看到我們的進展(部分猜測的詞),您應該創建另一個變量來保存這樣的字符串。而且,在每一個正確的猜測下,你都應該相應地更新這個字符串根據要猜測的單詞的長度,該字符串顯然開始爲#####或*****。

隨着幾項改進,我向您介紹Hang子手!

當然,所有的學分都會轉給你!

import random 

#list of words for game 
hangmanWords = ("Halloween","Hockey","Minnesota","Vikings","Twins","Timberwolves","Wild","Playstation","Achievement","Minecraft","Metallica","Portal","Xbox","Guitar") 

#assigns radomized word to variable 
randomWord = random.choice(hangmanWords) 


''' 
menu function, provides user with choices for game, user chooses via imput 
''' 
def menu(): 
    print(""" 
Welcome to Hangman! 

Select a difficulty: 
1. Easy (9 Misses) 
2. Medium (7 Misses) 
3. Advanced (5 Misses) 

4. Exit Game 
""") 
    selection = int(input("What difficulty do you pick (1-4)?: ")) 
    return selection 

def game(mode): 
    ''' 
    the game function, prints the word chosen by randomizer 
    asks the player to enter a letter that they guess is in the word 
    player gets guesses according to value passed as per mode, 
    to figure out the word, correct guesses don't count, 
    returns player to main menu when they lose 
    ''' 
    modes = {1:9, 2:7, 3:5} # Matches mode to guesses 
    guesses = modes[mode] 
    wrongGuesses = 0 
    listOfGuesses = [] 
    # print(randomWord) Dont't print the solution!! 

    # Get a random word which would be guessed by the user 
    to_guess = random.choice(hangmanWords) 
    to_guess = to_guess.lower() 

    # The correctly guessed part of the word that we print after every guess 
    guessed = "#"*len(to_guess) # e.g. "Hangman" --> "#######" 
    while(wrongGuesses != guesses): 
     x = input("Word - %s . Guess a letter: "%guessed).lower() 
     if x in to_guess: 
      print(x,"is in the word!") 
      listOfGuesses.append(x) 
      # Now replace the '#' in 'guessed' to 'x' as per our word 'to_guess' 
      new_guessed = "" 
      for index, char in enumerate(to_guess): 
       if char == x: 
        new_guessed += x 
       else: 
        new_guessed += guessed[index] 
      guessed = new_guessed # Change the guessed word according to new guess 
      # If user has guessed full word correct, then he has won! 
      if guessed == to_guess: 
       print("You have guessed the word! You win!") 
       print("The word was %s"%to_guess) 
       return True # return true on winning 
      else: 
       print("Letters guessed so far:", listOfGuesses, "\n") 

     else: 
      print(x,"is not in the word.") 
      wrongGuesses += 1 
      print("Wrong guesses:", wrongGuesses) 
      listOfGuesses.append(x) 
      print("Letters guessed so far:", listOfGuesses, "\n") 

    print("You lost the game!") 
    print("The word was %s"%to_guess) 
    return False # return false on loosing 

''' 
main function, deals with what happens depending on 
what the player selected on the menu 
''' 
def main(): 
    select = menu() 
    while(select != 4): 
     game(select) 
     select = menu() 

    print("You don't want to play today? :'(") 

main() 

無論你看到複製粘貼或代碼重複,它都不是Pythonic!儘量避免重複,特別是在功能上。 (如您的easymedium & hard功能)

0

對方回答實現它適合你,但我會步行通過該方法,因此您可以改善作爲一個程序員。

你會想要兩個列表,一個是完整的單詞,一個是你想要顯示的列表。您可以創建一個長度與該單詞相同的二進制列表,也可以製作一個「顯示列表」,該列表中包含下劃線,直到正確的字母被猜出。

的顯示列表的方法應該是這個樣子,是比較容易impliment:if語句內

for _ in range(len(randomword)): 
    displaylist.append("_") 

然後:

要初始化顯示列表

for i in range(len(randomword)): 
    if x == randomword[i]: 
     displaylist[i] = x 

再到打印,你需要這樣的東西:

print(''.join(displaylist)) 

您可以做出的另一個改進是製作一個單獨的函數來檢查您的單詞,以便最有效地使用模塊化編程。這將減少代碼的複雜性,冗餘度,並使實現更改變得更容易。