def test_get_random_word(): eng_word = get_random_word() assert eng_word != 'not found' assert isinstance(eng_word, str) pattern = str_compile(r'^[a-zA-Z\-.`&]+$') assert pattern.match(eng_word)
def play_hangman(): """Play a game of hangman. At the end of the game, returns if the player wants to retry. """ # Let player specify difficulty print('Starting a game of Hangman...') attempts_remaining = get_num_attempts() min_word_length = get_min_word_length() # Randomly select a word print('Selecting a word...') word = get_random_word(min_word_length) print() # Initialize game state variables idxs = [letter not in ascii_lowercase for letter in word] remaining_letters = set(ascii_lowercase) wrong_letters = [] word_solved = False # Main game loop while attempts_remaining > 0 and not word_solved: # Print current game state print('Word: {0}'.format(get_display_word(word, idxs))) print('Attempts Remaining: {0}'.format(attempts_remaining)) print('Previous Guesses: {0}'.format(' '.join(wrong_letters))) # Get player's next letter guess next_letter = get_next_letter(remaining_letters) # Check if letter guess is in word if next_letter in word: # Guessed correctly print('{0} is in the word!'.format(next_letter)) # Reveal matching letters for i in range(len(word)): if word[i] == next_letter: idxs[i] = True else: # Guessed incorrectly print('{0} is NOT in the word!'.format(next_letter)) # Decrement num of attempts left and append guess to wrong guesses attempts_remaining -= 1 wrong_letters.append(next_letter) # Check if word is completely solved if False not in idxs: word_solved = True print() # The game is over: reveal the word print('The word is {0}'.format(word)) # Notify player of victory or defeat if word_solved: print('Congratulations! You won!') else: print('Try again next time!') # Ask player if he/she wants to try again try_again = input('Would you like to try again? [y/Y] ') return try_again.lower() == 'y'
def play_hangman(): print('Starting a game of Hangman...') attempts_remaining = get_num_attempts() min_word_length = get_min_word_length() print('Selecting a word...') word = get_random_word(min_word_length) print() idxs = [letter not in ascii_lowercase for letter in word] remaining_letters = set(ascii_lowercase) wrong_letters = [] word_solved = False while attempts_remaining > 0 and not word_solved: print('Word: {0}'.format(get_display_word(word, idxs))) print('Attempts Remaining: {0}'.format(attempts_remaining)) print('Previous Guesses: {0}'.format(' '.join(wrong_letters))) next_letter = get_next_letter(remaining_letters) if next_letter in word: print('{0} is in the word!'.format(next_letter)) for i in range(len(word)): if word[i] == next_letter: idxs[i] = True else: print('{0} is NOT in the word!'.format(next_letter)) attempts_remaining -= 1 wrong_letters.append(next_letter) if False not in idxs: word_solved = True print() print('The word is {0}'.format(word)) # Notify player of victory or defeat if word_solved: print('Congratulations! You won!') else: print('Boo you suck!') # Ask player if he/she wants to try again try_again = input('Would you like to try again? [y/Y] ') return try_again.lower() == 'y'
def get_random_songs(): ''' Search for random songs on spotify and returns list of dictionaries of the following form: {'uri', 'artist', 'name', 'image'} ''' items = [] while len(items) is 0: results = spotify.search(q=words.get_random_word()) items = results['tracks']['items'] songs = [{'uri': item['uri'], 'artist': item['artists'][0]['name'], 'name': item['name'], 'image': item['album']['images'][0]['url']} for item in items] return songs
def play_hangman(): """Play a game of hangman.At the end of the game, returns if the player wants to retry. """ print("Starting a game of Hangman... ") time.sleep(0.8) attempts_remaining = get_num_attempts() min_word_length = get_min_word_length() print("Selecting a word...") word = get_random_word(min_word_length) print() idxs = [letter not in ascii_lowercase for letter in word] remaining_letters = set(ascii_lowercase) wrong_letters = [] word_solved = False while attempts_remaining > 0 and not word_solved: print("Word: {0}".format(get_display_word(word, idxs))) print("Attempts Remaining: {0}".format(attempts_remaining)) print("Previous Guesses: {0}".format(" ".join(wrong_letters))) next_letter = get_next_letter(remaining_letters) if next_letter in word: print("{0} is in the word!".format(next_letter)) for i in range(len(word)): if word[i] == next_letter: idxs[i] = True else: print("{0} is NOT in the word!".format(next_letter)) attempts_remaining -= 1 wrong_letters.append(next_letter) if False not in idxs: word_solved = True print() print("The word is {0}".format(word)) if word_solved: print("Congrats! You Won!") else: print("Try again next time!") try_again = input("Would you like to try again? [y/Y]: ") return try_again.lower() == "y"
def new_game(self, request): """Creates new game""" user = User.query(User.name == request.user_name).get() if not user: raise endpoints.NotFoundException( 'A User with that name does not exist!') word = words.get_random_word() word_length = len(word) user_word = "".join('-' for i in range(word_length)) print user_word game = Game.new_game(user.key, word, user_word) num = str(len(game.target)) msg = 'Good luck playing Hangman! {} letters, to find the word'.format( num) return game.to_form(msg)
def play_hangman(): """Play a game of hangman. At the end of the game, returns if the player wants to retry. """ # Let player specify difficulty print('Starting a game of Hangman...') attempts_remaining = get_num_attempts() min_word_length = get_min_word_length() # Randomly select a word print('Selecting a word...') word = get_random_word(min_word_length) print() # Initialize game state variables idxs = [ letter not in ascii_lowercase for letter in word ] remaining_letters = set(ascii_lowercase) wrong_letters = [] word_solved = False
def play_hangman(): print("Starting a new game of Hangman...") attempts_remaining = get_num_attempts() min_word_length = get_min_word_length() print("Selecting a word...") word = get_random_word(min_word_length) print() idxs = [letter not in ascii_lowercase for letter in word] remaining_letters = set(ascii_lowercase) wrong_letters = [] word_solved = False # Shows the current state of the game while attempts_remaining > 0 and not word_solved: print("Word: {0}".format(get_display_word(word, idxs))) print("attempts Remaining: {0}".format(attempts_remaining)) print("Previous Guesses: {0}".format(' '.join(wrong_letters))) next_letter = get_next_letter(remaining_letters) # Checking if the guessed letter is in the word if next_letter in word: print("{0} is in the word!".format(next_letter)) # Reveals matching letters for i in range(len(word)): if word[i] == next_letter: idxs[i] = True else: print("{0} is Not in the word!".format(next_letter)) attempts_remaining -= 1 wrong_letters.append(next_letter) # Checking if the word is completely solved if False not in idxs: word_solved = True print() print("The word is {0}!".format(word)) if word_solved: print("Congratulations! You are the best!") else: print("Better luck next time!") # Ask player if they want to try again try_again = input("Would you like to play again? [Y] ") return try_again.lower() == "y"
def play_hangman(): print(' THE GAME IS ABOUT TO BEING ....') attempts_remaining = get_attempts() print('SELECTING A WORD...') word = get_random_word() print() idxs = [letter not in ascii_lowercase for letter in word] remaining_letters = set(ascii_lowercase) wrong_letters = [] word_solved = False while attempts_remaining > 0 and not word_solved: print('Word:{0}'.format(get_display_word(word, idxs))) print('Attempts reminaing: {0}'.format(attempts_remaining)) print('Previous Guesses: {0}'.format(' '.join(wrong_letters))) next_letter = get_next_letter(remaining_letters) if next_letter in word: print('{0} is in the word'.format(next_letter)) for i in range(len(word)): if word[i] == next_letter: idxs[i] = True else: print('{0} is not in the word'.format(next_letter)) attempts_remaining -= 1 wrong_letters.append(next_letter) if False not in idxs: word_solved = True print() print('The word is {0}'.format(word)) if word_solved: print('Congrats you have won the game') else: print('better luck next time loser') try_again = input('Would you like to try again? [y/Y]') return try_again.lower() == 'y'
def get_word(): '''Get minimim and maximum word length for guess''' while True: min_word_length = input( "What is the minimum word length you want? [>3]: ") max_word_length = input( "What is the maximum word length you want? [<16]: ") try: min_word_length = int(min_word_length) max_word_length = int(max_word_length) if min_word_length > 3 and max_word_length < 16: return get_random_word(min_word_length, max_word_length) else: print( f"Values must be between greater than 3 and less than 16!") except ValueError: print(f"Values must be between greater than 3 and less than 16!") except Exception as err: print( f"Exception Type: {type(err).__name__}, Arguments: {err.args}")
def play_hangman(): """play the Game At the end of the game, return if player wants ta play again""" #Let player specify the difficulty print('Starting a game of Hangman...') attempts_remaining = get_num_attempts() min_word_length = get_min_word_length() #Randomly select a word print('Selecting a word') word = get_random_word(min_word_length) print() #Initialize the game state variables idxs = [letter not in ascii_lowercase for letter in word] remaining_letters = set(ascii_lowercase) wrong_letters = [] word_solved = False #main game loop while attempts_remaining > 0 and not word_solved: #print current game status print('Word: {0}'.format(get_display_word(word, idxs))) print('Attempts remaining: {0}'.format('').join(wrong_letters)) #Get the players next letter guess next_letter = get_next_letter(remaining_letters) #check if letter guessed is in word if next_letter in word: #guessed correctly print('{0} is in the word!'.format(next_letter)) #reveal matching letter for i in range(len(word)): if word[i] == next_letter: idxs[i] = True else: #guessed incorrectly print('{0} is NOT in the word!'.format(next_letter)) #decrease number of attempts and append guess towrong guesses attempts_remaining -= 1 wrong_letters.append(next_letter) #check if word is completely solved if False not in idxs: word_solved = True print() #the game is over: reveal the word print('The word is {0}'.format(word)) #notify player victory or defeat if word_solved: print('Congratulations, You Won!') else: print('Sorry, Try again next time!') #ask player if the want totry again try_again = input('Would you like to try again? [Y/y]') return try_again.lower() == 'y'
if len(next_letter) != 1: print ('{0} is not a single character'.format(next_letter)) elif next_letter not in ascii_lowercase: print ('{0} is not a letter'.format(next_letter)) else: remaining_letters.remove(next_letter) return next_letter print ('Starting a game of Hangman...') attempts_remaining = get_num_attempts() min_word_length = get_min_word_length() print ('Selecting a word...') word = get_random_word(min_word_length) print() idxs = [letter not in ascii_lowercase for letter in word] remaining_letters = set(ascii_lowercase) wrong_letters = [] word_solved = False while attempts_remaining > 0 and not word_solved: print('Word: {0}'.format(get_display_word(word, idx))) print('Attempts Remaining: {0}'.format(attempts_remaining)) print('Previous Guesses: {0}'.format(''.join(wrong_letters))) next_letter = get_next_letter(remaining_letters) if next_letter in word:
import turtle import hangman import words def write_word(word): writer = turtle.Turtle() writer.penup() writer.goto(100, 200) writer.write(word, font=('Arial', 16, 'bold')) writer.hideturtle() # Иницилизация original_word = words.get_random_word() word = '_' * len(original_word) write_word(word) errors = 0 def on_key_press(key): global errors global word if errors <= hangman.MAX_ERRORS: if key in original_word: word = words.fill_char(key, word, original_word) write_word(word) if word == original_word: print('You win') else:
def test_words(): word = words.get_random_word(4) with open("wordlist.txt", "r") as f: file_content = f.readlines() content = [x.strip() for x in file_content] assert (word in content) is True
def play_hangman(): """Start game""" # Get attempts and word length print('Starting a game of Hangman...') attempts_remaining = get_num_attempts() min_word_length = get_min_word_length() # Randomly choose word print('Selecting a word...') word = get_random_word(min_word_length) print() # Game state vars idxs = [letter not in ascii_lowercase for letter in word] remaining_letters = set(ascii_lowercase) wrong_letters = [] word_solved = False # Main loop while attempts_remaining > 0 and not word_solved: # Print game state print('Word: {0}'.format(get_display_word(word, idxs))) print('Attempts Remaining: {0}'.format(attempts_remaining)) print('Previous Guesses: {0}'.format(' '.join(wrong_letters))) # Get next guess next_letter = get_next_letter(remaining_letters) # Check if guessed letter is in the word if next_letter in word: # Correct guess print('{0} is in the word!'.format(next_letter)) # Show guessed letter for i in range(len(word)): if word[i] == next_letter: idxs[i] = True else: # Incorrect guess print('{0} is NOT in the word!'.format(next_letter)) # Decrement remaining attempts and maintain list of incorrect guesses attempts_remaining -= 1 wrong_letters.append(next_letter) # Is word solved? if False not in idxs: word_solved = True print() # Game over, reveal word print('The word is {0}'.format(word)) # Win or loss? if word_solved: print('Congratulations! You won!') else: print('Try again next time!') # Play again? try_again = input('Would you like to try again? [y/Y] ') return try_again.lower() == 'y'