0
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、missedLetters 和 secretWord,但我不明白正确字母的参数在哪里定义/替换。

用外行的话(因为我是新手),它在哪里说正确的字母应该做什么?我已经研究这个游戏一个星期了,到目前为止一切似乎都很好,除了这个。

请帮忙。

4

2 回答 2

0

correctLetters在这一行初始化:

correctLetters = ''

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

if guess in secretWord:
    correctLetters = correctLetters + guess

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

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

于 2013-10-13T04:52:48.683 回答
0

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

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

如果用户猜到了那个字母,它被用来输入字母而不是空白。

此处的其中一颗子弹上对此进行了描述

它的用法在这里解释

于 2013-10-13T04:53:06.977 回答