def read_character(self): """Read the current character.""" char = self.guess[self.pos] if char == ' ': # Change it to blank. char = 'blank' ao2.speak('Character %s is %s.' % ( self.pos + 1, # The position of the cursor (plus one for humans). char # The variable we made earlier. ))
def setup(self, handler = None): # First let's get us a word. self.word = choice(self.words).lower() # Make sure it's in lower case. self.guess = ''.join([' ' for x in xrange(len(self.word))]) # Make a string of spaces the same length as the actual word. self.guesses = 0 # The total number of guesses. self.pos = -1 # Our current position in the string so we can use left and right arrows to read what we have. Start at -1 so whatever the first keypress is we'll always start at the beginning. # Tell the user what they're dealing with. Use print formatters since they provide more flexibility and I hate typing + all the time. ao2.speak('Welcome to hangman. Your word contains %s %s.' % ( len(self.word), # That's the number of letters. 'letter' if len(self.word) == 1 else 'letters' # one-line if statements are a god send. )) self.lives = len(self.lives_messages) # Number of lives raiming. Expand the lives messages with inpunity. self.fails = '' # The list of letters the player guessed and failed.
def read_score(self, event): """Speak the scores.""" letters = len(self.guess.replace(' ', '')) # The number of correctly-guessed letters in the guess. # Let's try a different method of printing loads of variables. ao2.speak('After {guesses} {guesses_str}, you have guessed {letters} {letters_str} out of {word_length} in the word and have {lives} {lives_str} left.'.format( guesses = self.guesses, guesses_str = 'guess' if self.guesses == 1 else 'guesses', letters = letters, letters_str = 'letter' if letters == 1 else 'letters', word_length = len(self.word), lives = self.lives, lives_str = 'life' if self.lives == 1 else 'lives' ))
def do_guess(self, handler): """Make a guess.""" char = handler.event.unicode if ' ' not in self.guess: # They already won the game. return ao2.speak('You already won. Press enter to start another game.') elif self.lives <= 0: return ao2.speak('You already lost the game. Press enter to start another.') if char in self.guess or char in self.fails: # They guessed that letter before. return ao2.speak('You already guessed %s.' % char) self.guesses += 1 # Increase the number of guesses by 1. ao2.speak('Letter %s is %s the word.' % (char, 'in' if char in self.word else 'not in')) if char in self.word: res = '' # You can't change the contents of strings. for position, letter in enumerate(self.word): # The correct way to loop through something when you need your current position as well. if letter == char: # Give them the correctly guessed letter. res += char else: # Don't give them any free letters. res += self.guess[position] self.guess = res if ' ' not in self.guess: # They have guessed all letters correctly. self.finish() # Finish up. else: self.fails += char self.lives -= 1 # Subtract a life. ao2.speak(choice(self.lives_messages)) if not self.lives: # They are out of lives. self.die()
def die(self): """They lost all their lives.""" self.guess = self.word ao2.speak('Hard luck. Go buy a dictionary and try again. The word you were looking for was %s.' % self.word)
def finish(self): """Finish up the game. Could play a fanfare or something.""" ao2.speak('Congratulations! You will live to spell another day. Your word was %s.' % self.word)
def read_all(self, handler): """Read the whole guess, blanks and all.""" ao2.speak(', '.join(['blank' if x == ' ' else x for x in self.guess]) + '.') # One-liner stops us from making a blank variable, then filling it, and putting a full stop at the end, and loads of other painful things that bore you to death.