Hangman Game正确的单词功能

时间:2013-10-13 04:32:37

标签: python

import random

HANGMANPICS = ['''

+------+
|      |
|      |
       |
       |
       |
       |
       |
       |
       |
       |
==============''', '''

+      +
|      |
|      |
O      |
       |
       |
       |
       |
       |
       |
       |
==============''', '''
+------+
|      |
|      |
|      |
O      |
|      |
|      |
|      |
       |
       |
==============''']

words = 'ant baboon badger bat bear beaver beetle bird camel cat clam cobra cougar coyote crab crane crow deer dog donkey duck eagle ferret fish fox frog goat goose hawk iguana jackal koala leech lemur lion lizard llama mite monkey moose moth mouse mule newt otter owl oyster panda parrot pigeon python quail rabbit ram rat raven rhino salmon seal shark sheep skunk sloth slug snail snake spider squid stork swan tick tiger toad trout turkey turtle wasp weasel whale wolf wombat worm zebra'.split()

# This function returns a random string from the list of strings.
def getRandomWord(wordList): 
    wordIndex = random.randint(0, len(wordList) - 1)
return wordList[wordIndex]

def displayBoard(HANGMANPICS, missedLetters, correctLetters, secretWord):
    print HANGMANPICS[len(missedLetters)]
print

print 'Missed Letters:',
for letter in missedLetters:
    print letter,
print

blanks = '_' * len(secretWord)

for i in range(len(secretWord)):
  if secretWord[i] in correctLetters:
    blanks = blanks[:i] + secretWord[i] + blanks[i+1:]

for letter in blanks:
    print letter,
print

def getGuess(alreadyGuessed):
    while True:
        print 'Guess a letter.'
        guess = raw_input()
        guess = guess.lower()
        if len(guess) != 1:
            print 'Please enter a single letter.'
        elif guess in alreadyGuessed:
            print 'You have already guessed that letter. Choose again.'
        elif guess not in 'abcdefghijklmnopqrstuvwxyz':
            print 'Please enter a LETTER.'
        else:
            return guess

def playAgain():
    print 'Do you want to play again? (yes or no)'
    return raw_input().lower().startswith('y')


print 'HANGMAN'
missedLetters = ''
correctLetters = ''
secretWord = getRandomWord(words)
gameIsDone = False

while True:
    displayBoard(HANGMANPICS, missedLetters, correctLetters, secretWord)

    guess = getGuess(missedLetters + correctLetters)

if guess in secretWord:
    correctLetters = correctLetters + guess

    foundAllLetters = True
    for i in range(len(secretWord)):
        if secretWord[i] not in correctLetters:
            foundAllLetters = False
            break
    if foundAllLetters:
        print 'Yes! The secret word is "' + secretWord + '"! You have won!'
        gameIsDone = True
else:
  missedLetters = missedLetters + guess

  if len(missedLetters) == len(HANGMANPICS) - 1:
    displayBoard(HANGMANPICS, missedLetters, correctLetters, secretWord)
    print 'You have run out of guesses!\nAfter ' + str(len(missedLetters)) + ' missed guesses and ' + str(len(correctLetters)) + ' correct guesses, the word was "' + secretWord + '"'
    gameIsDone = True

if gameIsDone:
    if playAgain():
        missedLetters = ''
        correctLetters = ''
        gameIsDone = False
        secretWord = getRandomWord(words)
    else:
        break

在第二个函数displayBoard中,传递了4个参数。我理解HANGMANPICS,missLetters和secretWord,但我不明白correctLetters的参数定义/替换的位置。

在外行人的任期内(因为我是新手),它在哪里说出正确的信件应该做什么?我一直在研究这个游戏已经有一个星期了,到目前为止,除了这个之外,一切似乎都很好。

请帮忙。

2 个答案:

答案 0 :(得分:0)

correctLetters在此行中初始化:

correctLetters = ''

并附加到包含大量游戏逻辑的while循环内部:

if guess in secretWord:
    correctLetters = correctLetters + guess

它用于跟踪正确猜到的字母,并传递给displayBoard,以便它们可以在屏幕上显示。

顺便说一句,由于空格在Python代码中很重要,因此值得正确格式化您的问题。

答案 1 :(得分:0)

correctLetters进行快速关键字搜索,您可以看到它被描述为字符串。在函数displayBoard中,您可以在第78行看到它。

if secretWord[i] in correctLetters:
            blanks = blanks[:i] + secretWord[i] + blanks[i+1:]

如果用户猜到了那封信,它就会被用来放入字母而不是空白。

在其中一个项目符号here

中对其进行了描述

它的用法解释为here