예제 #1
0
def test_game_with_one_incorrect_guess():
    game = HangmanGame(['Python'])
    attempt = game.guess('x')
    assert attempt.is_miss() is True
    assert game.remaining_misses == 4
    assert game.previous_guesses == ['x']
    assert game.word.masked == '******'
예제 #2
0
def test_game_with_two_correct_guesses_same_move():
    game = HangmanGame(['alla'])
    attempt = game.guess('l')
    assert attempt.is_hit() is True
    assert game.remaining_misses == 5
    assert game.previous_guesses == ['l']
    assert game.word.masked == '*ll*'
예제 #3
0
def test_game_with_one_correct_guess(): #pass
    game = HangmanGame(['Python'])
    attempt = game.guess('y')

    assert attempt.is_hit() is True
    assert game.remaining_misses == 5
    assert game.previous_guesses == ['y']
    assert game.word.masked == '*y****'
예제 #4
0
def test_game_with_two_correct_guesses_same_move(): #pass
    game = HangmanGame(['rmotr'])
    attempt = game.guess('r')

    assert attempt.is_hit() is True
    assert game.remaining_misses == 5
    assert game.previous_guesses == ['r']
    assert game.word.masked == 'r***r'
예제 #5
0
def test_game_loses_first_try():
    game = HangmanGame(['Python'], number_of_guesses=1)

    with pytest.raises(GameLostException):
        game.guess('x')

    assert game.remaining_misses == 0
    assert game.previous_guesses == ['x']
    assert game.word.masked == '******'
예제 #6
0
def test_game_loses_several_guesses():
    game = HangmanGame(['Python'], number_of_guesses=3)

    attempt = game.guess('x')  # Miss!
    assert attempt.is_miss() is True
    assert game.remaining_misses == 2
    assert game.previous_guesses == ['x']
    assert game.word.masked == '******'

    attempt = game.guess('z')  # Miss!
    assert attempt.is_miss() is True
    assert game.remaining_misses == 1
    assert game.previous_guesses == ['x', 'z']
    assert game.word.masked == '******'

    with pytest.raises(GameLostException):
        game.guess('a')  # Miss!

    assert game.is_finished() is True
    assert game.is_lost() is True
    assert game.is_won() is False

    assert game.remaining_misses == 0
    assert game.previous_guesses == ['x', 'z', 'a']
    assert game.word.masked == '******'
예제 #7
0
def test_game_wins_several_moves(): #pass
    game = HangmanGame(['abc'])

    attempt = game.guess('a')
    assert attempt.is_hit() is True
    assert game.remaining_misses == 5
    assert game.previous_guesses == ['a']
    assert game.word.masked == 'a**'

    attempt = game.guess('c')
    assert attempt.is_hit() is True
    assert game.remaining_misses == 5
    assert game.previous_guesses == ['a', 'c']
    assert game.word.masked == 'a*c'

    with pytest.raises(GameWonException):
        game.guess('b')

    assert game.is_finished() is True
    assert game.is_won() is True
    assert game.is_lost() is False

    assert game.remaining_misses == 5
    assert game.previous_guesses == ['a', 'c', 'b']
    assert game.word.masked == 'abc'
예제 #8
0
def test_guess_word_is_case_insensitve(): #pass
    game = HangmanGame(['Python'])

    attempt = game.guess('p')
    assert attempt.is_hit() is True
    assert game.remaining_misses == 5
    assert game.previous_guesses == ['p']
    assert game.word.masked == 'p*****'

    attempt = game.guess('N')
    assert attempt.is_hit() is True
    assert game.remaining_misses == 5
    assert game.previous_guesses == ['p', 'n']
    assert game.word.masked == 'p****n'
예제 #9
0
def test_game_with_several_incorrect_guesses(): #pass
    game = HangmanGame(['Python'])

    attempt = game.guess('x')  # Miss!
    assert attempt.is_miss() is True
    assert game.remaining_misses == 4
    assert game.previous_guesses == ['x']
    assert game.word.masked == '******'

    attempt = game.guess('z')  # Miss!
    assert attempt.is_miss() is True
    assert game.remaining_misses == 3
    assert game.previous_guesses == ['x', 'z']
    assert game.word.masked == '******'
예제 #10
0
def test_game_already_lost_raises_game_finished():
    game = HangmanGame(['Python'], number_of_guesses=1)

    with pytest.raises(GameLostException):
        game.guess('x')  # Miss!

    assert game.is_finished() is True
    assert game.is_lost() is True
    assert game.is_won() is False

    with pytest.raises(GameFinishedException):
        game.guess('n')
예제 #11
0
def test_game_already_won_raises_game_finished():
    game = HangmanGame(['aaa'])

    with pytest.raises(GameWonException):
        game.guess('a')

    assert game.is_finished() is True
    assert game.is_won() is True
    assert game.is_lost() is False

    with pytest.raises(GameFinishedException):
        game.guess('x')
예제 #12
0
def test_start_new_game_initial_state_with_default_word_list(): #pass
    assert HangmanGame.WORD_LIST == ['rmotr', 'python', 'awesome']
    game = HangmanGame()

    assert game.remaining_misses == 5
    assert isinstance(game.word, GuessWord)
    assert game.previous_guesses == []

    assert game.word.answer in HangmanGame.WORD_LIST
예제 #13
0
def main():
    print("=====================")
    print("###### Hangman ######")
    print("=====================")
    words = _input(
        "Enter your list of words separated by comma. Leave empty for default: "
    )

    if words.strip():
        words = build_list_of_words(words)
    else:
        words = None

    game = HangmanGame(word_list=words)

    print("\n### Game Initialized. Let's play!!\n")

    try:
        while True:
            print('')

            line_message = "({}) Enter new guess ({} remaining attempts): ".format(
                game.word.masked, game.remaining_misses)

            users_guess = _input(line_message)
            if not users_guess.strip():
                print("\tEmpty is not valid. Please guess again.")
                continue

            try:
                attempt = game.guess(users_guess)
            except InvalidGuessedLetterException:
                print("\t Your guess is incorrect. Please guess again.")
                continue

            if attempt.is_hit():
                print("\tCongratulations! That's correct.")
            else:
                print("\t:( That's a miss!")
    except GameWonException:
        print("\t YES! You win! The word was: {}".format(game.word.answer))
    except GameLostException:
        print("\t :( OH NO! You Lose! The word was: {}".format(
            game.word.answer))
def test_game_with_several_correct_guesses():
    game = HangmanGame(['Python'])

    attempt = game.guess('y')
    assert attempt.is_hit() is True
    assert game.remaining_misses == 5
    assert game.previous_guesses == ['y']
    assert game.word.masked == '*y****'

    attempt = game.guess('o')
    assert attempt.is_hit() is True
    assert game.remaining_misses == 5
    assert game.previous_guesses == ['y', 'o']
    assert game.word.masked == '*y**o*'

    attempt = game.guess('t')
    assert attempt.is_hit() is True
    assert game.remaining_misses == 5
    assert game.previous_guesses == ['y', 'o', 't']
    assert game.word.masked == '*yt*o*'
def test_game_with_several_correct_and_incorrect_guesses():
    game = HangmanGame(['Python'])

    attempt = game.guess('y')
    assert attempt.is_hit is True
    assert game.remaining_misses == 5
    assert game.previous_guesses == ['y']
    assert game.word.masked == '*y****'

    attempt = game.guess('x')  # Miss!
    assert attempt.is_miss is True
    assert game.remaining_misses == 4
    assert game.previous_guesses == ['y', 'x']
    assert game.word.masked == '*y****'

    attempt = game.guess('o')
    assert attempt.is_hit() is True
    assert game.remaining_misses == 4
    assert game.previous_guesses == ['y', 'x', 'o']
    assert game.word.masked == '*y**o*'

    attempt = game.guess('z')
    assert attempt.is_miss is True
    assert game.remaining_misses == 3
    assert game.previous_guesses == ['y', 'x', 'o', 'z']
    assert game.word.masked == '*y**o*'
예제 #16
0
def test_game_wins_first_try():
    game = HangmanGame(['aaa'])
    with pytest.raises(GameWonException):
        game.guess('a')

    assert game.is_finished() is True
    assert game.is_won() is True
    assert game.is_lost() is False

    assert game.remaining_misses == 5
    assert game.previous_guesses == ['a']
    assert game.word.masked == 'aaa'
예제 #17
0
def test_game_loses_first_try(): #pass
    game = HangmanGame(['Python'], number_of_guesses=1)

    with pytest.raises(GameLostException):
        game.guess('x')  # Miss!

    assert game.is_finished() is True
    assert game.is_lost() is True
    assert game.is_won() is False

    assert game.remaining_misses == 0
    assert game.previous_guesses == ['x']
    assert game.word.masked == '******'
예제 #18
0
def test_select_random_word_with_one_word():
    list_of_words = ['asdd']
    word_to_guess = HangmanGame.select_random_word(list_of_words)
    assert word_to_guess == 'asdd'
예제 #19
0
def test_select_random_word_with_one_word(): #pass
    list_of_words = ['rmotr']
    word_to_guess = HangmanGame.select_random_word(list_of_words)
    assert word_to_guess == 'rmotr'
예제 #20
0
def test_select_random_word_with_many_words(): #pass
    list_of_words = ['rmotr', 'python', 'intro']
    word_to_guess = HangmanGame.select_random_word(list_of_words)
    assert word_to_guess in list_of_words
예제 #21
0
def test_select_random_word_with_empty_list(): #pass
    with pytest.raises(InvalidListOfWordsException):
        HangmanGame.select_random_word([])
예제 #22
0
def test_start_new_game_initial_state_with_number_of_guesses(): #pass
    game = HangmanGame(['Python'], number_of_guesses=3)

    assert game.remaining_misses == 3
    assert isinstance(game.word, GuessWord)
    assert game.previous_guesses == []
예제 #23
0
def test_start_new_game_initial_state_with_default_attempts(): #pass
    game = HangmanGame(['Python'])

    assert game.remaining_misses == 5
    assert isinstance(game.word, GuessWord)
    assert game.previous_guesses == []