def test_game_wins_several_moves_some_misses(): game = start_new_game(['abc']) guess_letter(game, 'a') assert game['masked_word'] == 'a**' assert game['remaining_misses'] == 5 assert game['previous_guesses'] == ['a'] guess_letter(game, 'x') # Miss! assert game['masked_word'] == 'a**' assert game['remaining_misses'] == 4 assert game['previous_guesses'] == ['a', 'x'] guess_letter(game, 'c') assert game['masked_word'] == 'a*c' assert game['remaining_misses'] == 4 assert game['previous_guesses'] == ['a', 'x', 'c'] guess_letter(game, 'z') # Miss! assert game['masked_word'] == 'a*c' assert game['remaining_misses'] == 3 assert game['previous_guesses'] == ['a', 'x', 'c', 'z'] with pytest.raises(GameWonException): guess_letter(game, 'b') assert game['masked_word'] == 'abc' assert game['remaining_misses'] == 3 assert game['previous_guesses'] == ['a', 'x', 'c', 'z', 'b']
def test_game_loses_with_some_correct_guesses(): game = start_new_game(['Python'], number_of_guesses=3) guess_letter(game, 'y') assert game['masked_word'] == '*y****' assert game['remaining_misses'] == 3 assert game['previous_guesses'] == ['y'] guess_letter(game, 'x') # Miss! assert game['masked_word'] == '*y****' assert game['remaining_misses'] == 2 assert game['previous_guesses'] == ['y', 'x'] guess_letter(game, 'z') # Miss! assert game['masked_word'] == '*y****' assert game['remaining_misses'] == 1 assert game['previous_guesses'] == ['y', 'x', 'z'] guess_letter(game, 't') assert game['masked_word'] == '*yt***' assert game['remaining_misses'] == 1 assert game['previous_guesses'] == ['y', 'x', 'z', 't'] with pytest.raises(GameLostException): guess_letter(game, 'a') # Miss! assert game['masked_word'] == '******' assert game['remaining_misses'] == 0 assert game['previous_guesses'] == ['y', 'x', 'z', 't', 'a']
def test_game_with_one_incorrect_guess(): game = start_new_game(['Python']) guess_letter(game, 'x') # Miss! assert game['masked_word'] == '******' assert game['remaining_misses'] == 4 # (5 - 1) assert game['previous_guesses'] == ['x']
def test_game_loses_first_try(): game = start_new_game(['Python'], number_of_guesses=1) with pytest.raises(GameLostException): guess_letter(game, 'x') # Miss! assert game['masked_word'] == '******' assert game['remaining_misses'] == 0 assert game['previous_guesses'] == ['x']
def test_game_wins_first_try(): game = start_new_game(['aaa']) with pytest.raises(GameWonException): guess_letter(game, 'a') assert game['masked_word'] == 'aaa' assert game['remaining_misses'] == 5 assert game['previous_guesses'] == ['a']
def main(): print("=====================") print("###### Hangman ######") print("=====================") words = _input( "Enter your list of words separated by comma. Leave empty for default: " ) list_of_words = build_list_of_words(words, LIST_OF_WORDS) attempts = _input( "Enter the number of attempts allowed. Leave empty for default: ") if attempts.strip(): game = start_new_game(list_of_words, int(attempts)) else: game = start_new_game(list_of_words) print("\n### Game Initialized. Let's play!!\n") try: while True: print('') previous_masked_word = game['masked_word'] line_message = "({}) Enter new guess ({} remaining attempts): ".format( previous_masked_word, game['remaining_misses']) users_guess = _input(line_message) try: guess_letter(game, users_guess) except InvalidGuessedLetterException: print("\t Your guess is incorrect. Please guess again.") continue new_masked_word = game['masked_word'] if new_masked_word != previous_masked_word: print("\tCongratulations! That's correct.") else: print("\t:( That's a miss!") except GameWonException: print("\t YES! You win! The word was: {}".format(game['answer_word'])) except GameLostException: print("\t :( OH NO! You Lose! The word was: {}".format( game['answer_word']))
def test_start_new_game_initial_state(): # This test verifies that you haven't changed start_new_game game = start_new_game(['Python'], number_of_guesses=3) assert game == { 'answer_word': 'Python', 'masked_word': '******', 'previous_guesses': [], 'remaining_misses': 3, }
def test_game_already_lost_raises_game_finished(): game = start_new_game(['Python'], number_of_guesses=1) with pytest.raises(GameLostException): guess_letter(game, 'x') # Miss! assert game['masked_word'] == '******' assert game['remaining_misses'] == 0 assert game['previous_guesses'] == ['x'] with pytest.raises(GameFinishedException): guess_letter(game, 'n') # Doesn't matter
def test_game_already_won_raises_game_finished(): game = start_new_game(['aaa']) with pytest.raises(GameWonException): guess_letter(game, 'a') assert game['masked_word'] == 'aaa' assert game['remaining_misses'] == 5 assert game['previous_guesses'] == ['a'] with pytest.raises(GameFinishedException): guess_letter(game, 'x') # Doesn't matter
def main(): print("=====================") print("###### Hangman ######") print("=====================") words = _input("Enter your list of words separated by comma. Leave empty for default: ") list_of_words = build_list_of_words(words, LIST_OF_WORDS) attempts = _input("Enter how the number of attempts allowed. Leave empty for default: ") if attempts.strip(): game = start_new_game(list_of_words, int(attempts)) else: game = start_new_game(list_of_words) print("\n### Game Initialized. Let's play!!\n") try: while True: print('') previous_masked_word = game['masked_word'] line_message = "({}) Enter new guess ({} remaining attempts): ".format( previous_masked_word, game['remaining_misses']) users_guess = _input(line_message) try: guess_letter(game, users_guess) except InvalidGuessedLetterException: print("\t Your guess is incorrect. Please guess again.") continue new_masked_word = game['masked_word'] if new_masked_word != previous_masked_word: print("\tCongratulations! That's correct.") else: print("\t:( That's a miss!") except GameWonException: print("\t YES! You win! The word was: {}".format(game['answer_word'])) except GameLostException: print("\t :( OH NO! You Lose! The word was: {}".format(game['answer_word']))
def test_game_with_several_incorrect_guesses(): game = start_new_game(['Python']) guess_letter(game, 'x') # Miss! assert game['masked_word'] == '******' assert game['remaining_misses'] == 4 assert game['previous_guesses'] == ['x'] guess_letter(game, 'z') # Miss! assert game['masked_word'] == '******' assert game['remaining_misses'] == 3 assert game['previous_guesses'] == ['x', 'z']
def test_guess_word_is_case_insensitve(): game = start_new_game(['Python']) guess_letter(game, 'p') assert game['masked_word'] == 'p*****' assert game['remaining_misses'] == 5 assert game['previous_guesses'] == ['p'] guess_letter(game, 'N') assert game['masked_word'] == 'p****n' assert game['remaining_misses'] == 5 assert game['previous_guesses'] == ['p', 'n']
def test_game_wins_several_moves_repeated_words(): game = start_new_game(['aba']) guess_letter(game, 'a') assert game['masked_word'] == 'a*a' assert game['remaining_misses'] == 5 assert game['previous_guesses'] == ['a'] with pytest.raises(GameWonException): guess_letter(game, 'b') assert game['masked_word'] == 'aba' assert game['remaining_misses'] == 5 assert game['previous_guesses'] == ['a', 'b']
def test_game_with_several_correct_guesses(): game = start_new_game(['Python']) guess_letter(game, 'y') assert game['masked_word'] == '*y****' assert game['remaining_misses'] == 5 assert game['previous_guesses'] == ['y'] guess_letter(game, 'o') assert game['masked_word'] == '*y**o*' assert game['remaining_misses'] == 5 assert game['previous_guesses'] == ['y', 'o'] guess_letter(game, 't') assert game['masked_word'] == '*yt*o*' assert game['remaining_misses'] == 5 assert game['previous_guesses'] == ['y', 'o', 't']
def test_game_loses_several_guesses(): #passed game = start_new_game(['Python'], number_of_guesses=3) guess_letter(game, 'x') # Miss! assert game['masked_word'] == '******' assert game['remaining_misses'] == 2 assert game['previous_guesses'] == ['x'] guess_letter(game, 'z') # Miss! assert game['masked_word'] == '******' assert game['remaining_misses'] == 1 assert game['previous_guesses'] == ['x', 'z'] with pytest.raises(GameLostException): guess_letter(game, 'a') # Miss! assert game['masked_word'] == '******' assert game['remaining_misses'] == 0 assert game['previous_guesses'] == ['x', 'z', 'a']
def test_game_with_several_correct_and_incorrect_guesses(): #passed game = start_new_game(['Python']) guess_letter(game, 'y') assert game['masked_word'] == '*y****' assert game['remaining_misses'] == 5 assert game['previous_guesses'] == ['y'] guess_letter(game, 'x') # Miss! assert game['masked_word'] == '*y****' assert game['remaining_misses'] == 4 assert game['previous_guesses'] == ['y', 'x'] guess_letter(game, 'o') assert game['masked_word'] == '*y**o*' assert game['remaining_misses'] == 4 assert game['previous_guesses'] == ['y', 'x', 'o'] guess_letter(game, 'z') # Miss! assert game['masked_word'] == '*y**o*' assert game['remaining_misses'] == 3 assert game['previous_guesses'] == ['y', 'x', 'o', 'z']
def test_game_with_one_correct_guess(): #passed game = start_new_game(['Python']) guess_letter(game, 'y') assert game['masked_word'] == '*y****' assert game['remaining_misses'] == 5 assert game['previous_guesses'] == ['y']
def test_game_with_two_correct_guesses_same_move(): game = start_new_game(['rmotr']) guess_letter(game, 'r') assert game['masked_word'] == 'r***r' assert game['remaining_misses'] == 5 assert game['previous_guesses'] == ['r']
def main(): print('*******************') print('##### HANGMAN #####') print('*******************') try: words = input('Enter your list of words separated by comma. Leave empty for default: ') if words == " ": raise InvalidListOfWordsException() except InvalidListOfWordsException: print("You have entered an Invalid list!") print("Restarting Game...") print() main() list_of_words = build_list_of_words(words, LIST_OF_WORDS) attempts = input('Enter how many number of attempts allowed. Leave empty for default: ') if attempts: game = start_new_game(list_of_words, int(attempts)) else: game = start_new_game(list_of_words) print('\n### Game Initialized. Let\'s play!! ###\n') try: while True: print() if game["remaining_misses"] == 0: raise GameLostException() previous_masked_word = game['masked_word'] line_message = '({}) Enter new guess ({} remaining attempts): '.format( previous_masked_word, game['remaining_misses']) users_guess = input(line_message).lower() try: guess_letter(game, users_guess) except InvalidGuessedLetterException: print('\t Your guess is incorrect. Please guess again.') continue new_masked_word = game['masked_word'] if new_masked_word != previous_masked_word: print('\tCongratulations! That\'s correct.') else: print('\t:( That\'s a miss!') if new_masked_word == game["answer_word"]: raise GameWonException except GameWonException: print('\t YES! You win! The word was: {}'.format(game['answer_word'])) except GameLostException: print('\t :( OH NO! You Lose! The word was: {}'.format(game['answer_word'])) except InvalidWordException: print("The word was Invalid!") print("Restarting Game...") print() main()