Exemple #1
0
    def test_uppercase_input(self):
        game = HangmanGame('a', 1)

        result = game.guess('A')
        self.assertEqual(result, GuessResult.CORRECT)
        self.assertIn('a', game.guesses)
        self.assertEqual(game.state, GameState.WON)
Exemple #2
0
    def test_cant_duplicate_guess(self):
        game = HangmanGame('ab', 2)
        game.guess('a')
        result = game.guess('a')
        self.assertEqual(result, GuessResult.FAIL_ALREADY_GUESSED)

        game = HangmanGame('ab', 2)
        game.guess('c')
        result = game.guess('c')
        self.assertEqual(result, GuessResult.FAIL_ALREADY_GUESSED)
Exemple #3
0
    def test_valid_input(self):
        valid_chars = 'abcdefghijklmnopqrstuvwxyz1234567890'
        game = HangmanGame(valid_chars, 1)

        for char in valid_chars:
            result = game.guess(char)
            self.assertEqual(result, GuessResult.CORRECT)
            self.assertIn(char, game.guesses)

        self.assertEqual(game.state, GameState.WON)
Exemple #4
0
class HangmanGameTestCase(TestCase):
    def setUp(self):
        self.h = HangmanGame()
        self.h.choosen_word = '3dhubshubs'
        self.h.players_name = 'test'
        self.h.prepare()

    def check_input_processed(self, input, expected_result, attempts):
        result = self.h.process_input(input)
        assert result == expected_result
        assert self.h.failed_attemts_left == attempts

    def test_process_invalid_input(self):
        self.check_input_processed('', 'not_letter', 5)
        self.check_input_processed('abc', 'not_letter', 5)
        self.check_input_processed('3', 'not_letter', 5)
        self.check_input_processed('_', 'not_letter', 5)
        self.check_input_processed('3abc', 'not_letter', 5)

    def test_process_valid_input(self):
        self.check_input_processed('a', 'unlucky_guess', 4)
        self.check_input_processed('z', 'unlucky_guess', 3)

        self.check_input_processed('d', 'lucky_guess', 3)
        self.check_input_processed('h', 'lucky_guess', 3)

        self.check_input_processed('h', 'already_revealed', 3)
        self.check_input_processed('a', 'already_wasted', 3)

    def test_process_win(self):
        self.check_input_processed('d', 'lucky_guess', 5)
        self.check_input_processed('h', 'lucky_guess', 5)
        self.check_input_processed('u', 'lucky_guess', 5)
        self.check_input_processed('b', 'lucky_guess', 5)
        self.check_input_processed('s', 'epic_win', 5)

    def test_process_fail(self):
        self.check_input_processed('z', 'unlucky_guess', 4)
        self.check_input_processed('x', 'unlucky_guess', 3)
        self.check_input_processed('y', 'unlucky_guess', 2)
        self.check_input_processed('w', 'unlucky_guess', 1)
        self.check_input_processed('v', 'epic_fail', 0)

    @mock.patch('hangman.get_input',
                mock.Mock(side_effect=['test', 'z', 'x', 'c', 'v', 'b', 'n']))
    def test_game(self):
        self.h.start_game()
        assert self.h.failed_attemts_left == 0
Exemple #5
0
    def test_cant_guess_when_game_over(self):
        game = HangmanGame('a', 1)
        game.guess('a')
        self.assertEqual(game.state, GameState.WON)

        result = game.guess('b')
        self.assertEqual(result, GuessResult.FAIL_ALREADY_GAME_OVER)
        self.assertEqual(game.guesses, ['a'])

        game = HangmanGame('a', 1)
        game.guess('b')
        self.assertEqual(game.state, GameState.LOST)

        result = game.guess('a')
        self.assertEqual(result, GuessResult.FAIL_ALREADY_GAME_OVER)
        self.assertEqual(game.guesses, ['b'])
Exemple #6
0
 def test_is_valid_filename_TypeError(self):
     with self.assertRaises(TypeError,
                            msg='TypeError was not thrown when '
                            'an absolute Path was '
                            'input'):
         path_to_file = Path('word_bank')
         HangmanGame(path_to_file=path_to_file)
Exemple #7
0
    def test_invalid_input(self):
        game = HangmanGame('a', 1)
        result = game.guess('aaaaaaa')
        self.assertEqual(result, GuessResult.FAIL_INVALID_INPUT)
        self.assertEqual(game.state, GameState.IN_PROGRESS)
        self.assertEqual(game.guesses, [])
        self.assertEqual(game.failed_guess_limit, 1)
        self.assertEqual(game.num_failed_guesses_remaining, 1)
        self.assertEqual(game.revealed_word, '_')
        self.assertEqual(game.num_revealed_letters, 0)

        result = game.guess('.')
        self.assertEqual(result, GuessResult.FAIL_INVALID_INPUT)

        result = game.guess('!')
        self.assertEqual(result, GuessResult.FAIL_INVALID_INPUT)
Exemple #8
0
 def test_is_valid_filename_NameError(self):
     with self.assertRaises(NameError,
                            msg='NameError was not thrown when '
                            'an incorrect filename was '
                            'input'):
         path_to_file = Path.cwd()
         path_to_file = path_to_file / 'word_bank'
         HangmanGame(path_to_file=path_to_file)
Exemple #9
0
 def test_read_file_stats_FileFoundError(self):
     with self.assertRaises(FileNotFoundError,
                            msg='FileNotFoundError was '
                            'not thrown when an '
                            'incorrect '
                            'filename was input'):
         path_to_file = Path.cwd()
         path_to_file = path_to_file / 'word_banker.txt'
         HangmanGame(path_to_file=path_to_file)
Exemple #10
0
    def test_lose(self):
        game = HangmanGame('a', 1)

        self.assertEqual(game.state, GameState.IN_PROGRESS)
        self.assertEqual(game.guesses, [])
        self.assertEqual(game.failed_guess_limit, 1)
        self.assertEqual(game.num_failed_guesses_remaining, 1)
        self.assertEqual(game.revealed_word, '_')
        self.assertEqual(game.num_revealed_letters, 0)

        result = game.guess('b')

        self.assertEqual(result, GuessResult.INCORRECT)
        self.assertEqual(game.state, GameState.LOST)
        self.assertEqual(game.guesses, ['b'])
        self.assertEqual(game.failed_guess_limit, 1)
        self.assertEqual(game.num_failed_guesses_remaining, 0)
        self.assertEqual(game.revealed_word, '_')
        self.assertEqual(game.num_revealed_letters, 0)
Exemple #11
0
    def test_scorer(self):
        game = HangmanGame('abc', 2)
        score = HangmanGameScorer.score(game)
        self.assertEqual(score, 20)

        game.guess('a')
        score = HangmanGameScorer.score(game)
        self.assertEqual(score, 40)

        game.guess('d')
        score = HangmanGameScorer.score(game)
        self.assertEqual(score, 30)

        game.guess('b')
        score = HangmanGameScorer.score(game)
        self.assertEqual(score, 50)

        game.guess('c')
        score = HangmanGameScorer.score(game)
        self.assertEqual(score, 70)
Exemple #12
0
    def test_revealed_word(self):
        game = HangmanGame('abac', 2)

        game.guess('b')
        self.assertEqual(game.revealed_word, '_b__')
        self.assertEqual(game.num_revealed_letters, 1)

        game.guess('a')
        self.assertEqual(game.revealed_word, 'aba_')
        self.assertEqual(game.num_revealed_letters, 3)

        game.guess('c')
        self.assertEqual(game.revealed_word, 'abac')
        self.assertEqual(game.num_revealed_letters, 4)

        self.assertEqual(game.state, GameState.WON)
""" Calls the HangmanGame
"""

from hangman import HangmanGame

WORDLIST_FILENAME = "palavras.txt"
NUMBER_OF_GUESSES = 8

GAME = HangmanGame(file_name=WORDLIST_FILENAME,
                   number_of_guesses=NUMBER_OF_GUESSES)
GAME.start_game()
Exemple #14
0
 def test_is_comment_true(self):
     result = HangmanGame.is_comment('# This is a comment')
     self.assertEqual(True, result)
Exemple #15
0
 def test_is_comment_false(self):
     result = HangmanGame.is_comment('This is a not comment')
     self.assertEqual(False, result)
Exemple #16
0
 def setUp(self):
     self.h = HangmanGame()
     self.h.choosen_word = '3dhubshubs'
     self.h.players_name = 'test'
     self.h.prepare()
Exemple #17
0
class TestGamePlay(TestCase):
    def setUp(self):
        """Creates GamePlay class for all the unittests."""
        self.game_one = HangmanGame()
        self.game_two = HangmanGame()

    def test_is_valid_filename_TypeError(self):
        with self.assertRaises(TypeError,
                               msg='TypeError was not thrown when '
                               'an absolute Path was '
                               'input'):
            path_to_file = Path('word_bank')
            HangmanGame(path_to_file=path_to_file)

    def test_is_valid_filename_NameError(self):
        with self.assertRaises(NameError,
                               msg='NameError was not thrown when '
                               'an incorrect filename was '
                               'input'):
            path_to_file = Path.cwd()
            path_to_file = path_to_file / 'word_bank'
            HangmanGame(path_to_file=path_to_file)

    def test_read_file_stats_FileFoundError(self):
        with self.assertRaises(FileNotFoundError,
                               msg='FileNotFoundError was '
                               'not thrown when an '
                               'incorrect '
                               'filename was input'):
            path_to_file = Path.cwd()
            path_to_file = path_to_file / 'word_banker.txt'
            HangmanGame(path_to_file=path_to_file)

    def test_is_comment_true(self):
        result = HangmanGame.is_comment('# This is a comment')
        self.assertEqual(True, result)

    def test_is_comment_false(self):
        result = HangmanGame.is_comment('This is a not comment')
        self.assertEqual(False, result)

    def test_set_word_empty(self):
        """Checks if the word is set correctly."""
        self.game_one.set_word()
        self.assertIsNotNone(self.game_one.word, 'Variable \'word\' is empty')
        self.assertEqual(
            len(self.game_one.word_display), len(self.game_one.word),
            'Length of \'word_display\' '
            'and \'word\' do not match.')
        self.assertIn('_', self.game_one.word_display, '\'word_display\' does '
                      'not contain dashes: '
                      '\'_\'')

    @mock.patch('builtins.input', lambda *args: 'n')
    def test_set_word_error_n(self):
        """Checks if the correct error is raised."""
        self.game_one.word_bank = set()

        with self.assertRaises(UserWarning,
                               msg='The word bank has been '
                               'exhausted and you did not '
                               'update it.'):
            with mock.patch('sys.stdout', new=io.StringIO()) as stdout:
                self.game_one.set_word()
                self.assertEqual(
                    'Error: The word bank has been exhausted. '
                    'All the words have been used.\n', stdout.getvalue(),
                    'UserWarning not raised')

    def test_set_word_error_y(self):
        """Checks if the correct error is raised.

        Notes: The syntax for this test is based on Pytest and not unittest.
        """
        self.game_one.word_bank = set()

        with mock.patch('builtins.input',
                        side_effect=['y', 'hangman/data/word_bank.txt']):
            self.game_one.set_word()

        self.assertIsNotNone(self.game_one.word_bank, 'word_bank is not '
                             'updated.')
        self.assertIsNotNone(self.game_one.word, 'Word is not updated after '
                             'updating file.')

    def test_update_word_display(self):
        self.game_one.set_word()
        letter = self.game_one.word[1]
        self.game_one.update_word_display(letter)
        self.assertEqual(
            letter, self.game_one.word_display[1],
            '\'word_display\' is not updated correctly. The '
            'second letter should be displayed.')

    def test_update_hangman_head(self):
        self.game_one.incorrect_guesses = ['a']
        self.game_one.update_hangman()
        self.assertEqual('O', self.game_one.hangman.get('head'),
                         'hangman\'s head was not updated.')

    def test_update_hangman_body(self):
        self.game_one.incorrect_guesses = ['a', 'b']
        self.game_one.update_hangman()
        self.assertEqual('|', self.game_one.hangman.get('body'),
                         'hangman\'s body was not updated.')

    def test_update_hangman_right_hand(self):
        self.game_one.incorrect_guesses = ['a', 'b', 'c']
        self.game_one.update_hangman()
        self.assertEqual('/', self.game_one.hangman.get('right_hand'),
                         'hangman\'s right_hand was not updated.')

    def test_update_hangman_left_hand(self):
        self.game_one.incorrect_guesses = ['a', 'b', 'c', 'd']
        self.game_one.update_hangman()
        self.assertEqual('\\', self.game_one.hangman.get('left_hand'),
                         'hangman\'s left_hand was not updated.')

    def test_get_status_guessing_hello(self):
        self.game_one.set_word()
        self.game_one.word = 'hello'
        self.assertEqual('guessing', self.game_one.get_status(),
                         'Initial state of the game is incorrect')

        self.game_one.word_display = ['o']
        self.assertEqual('guessing', self.game_one.get_status(),
                         'State of the game after one move is incorrect')

    def test_get_status_won_hello(self):
        self.game_one.set_word()
        self.game_one.word = 'hello'
        self.game_one.word_display = ['h', 'e', 'l', 'l', 'o']
        self.assertEqual('won', self.game_one.get_status(),
                         'State of a game won is incorrect')

    def test_get_status_guessing_world(self):
        self.game_two.set_word()
        self.game_two.word = 'world'
        self.assertEqual('guessing', self.game_two.get_status(),
                         'Initial state of the game is incorrect')

    def test_get_status_lost_world(self):
        self.game_two.set_word()
        self.game_two.word = 'world'
        self.game_two.word_display = ['w', 'o', 'r', 'l', '_']
        self.game_two.incorrect_guesses = ['i', 'p', 't', 'q', 's', 'n']
        self.assertEqual('lost', self.game_two.get_status(),
                         'State of a game lost is incorrect')

    def test_update_points_hello_right_3(self):
        self.game_one.word = 'hello'
        self.game_one.word_display = list('hello')
        self.game_one.incorrect_guesses = ['r', 'p', 'q']
        self.game_one.correct_guesses = ['h', 'e', 'l', 'o']
        points = self.game_one.update_points()
        self.assertEqual(
            7, points, f'Points for \'hello\' should be 7 and '
            f'not {points}')

    def test_update_points_hello_wrong_2(self):
        self.game_one.word = 'hello'
        self.game_one.word_display = list('h___o')
        self.game_one.incorrect_guesses = ['r', 't', 'q', 'z', 'y', 'n']
        self.game_one.correct_guesses = ['h', 'o']
        points = self.game_one.update_points()
        self.assertEqual(
            -2, points, f'Points for \'h___o\' should '
            f'be -2 and not {points}')

    def test_update_points_world_right(self):
        self.game_two.word = 'world'
        self.game_two.word_display = list('world')
        self.game_two.incorrect_guesses = ['i', 'p', 't', 'q', 's']
        self.game_two.correct_guesses = ['w', 'r', 'l', 'o', 'd']
        points = self.game_two.update_points()
        self.assertEqual(
            7, points, f'Points for \'world\' '
            f'should be 7 and not {points}')

    def test_update_points_python_right(self):
        self.game_two.word = 'python'
        self.game_two.word_display = list('python')
        self.game_two.incorrect_guesses = ['k']
        self.game_two.correct_guesses = ['h', 'o', 't', 'p', 'n', 'y']
        points = self.game_two.update_points()
        self.assertEqual(
            13, points, f'Points for \'python\' should '
            f'be 13 and not {points}')

    def test_update_points_python_guessing(self):
        self.game_two.word = 'python'
        self.game_two.word_display = list('pyt__n')
        self.game_two.incorrect_guesses = ['k']
        self.game_two.correct_guesses = ['t', 'p', 'n', 'y']
        points = self.game_two.update_points()
        self.assertEqual(
            0, points, f'Points for an unfinished \'pyt__n\' '
            f'should be 0 and not {points}')

    def test_update_points_javascript_wrong_1(self):
        self.game_two.word = 'javascript'
        self.game_two.word_display = list('javascrip_')
        self.game_two.incorrect_guesses = ['n', 'o', 'l', 'q', 'b', 'y']
        self.game_two.correct_guesses = [
            'i', 'a', 'v', 'j', 's', 'c', 'r', 'p'
        ]
        points = self.game_two.update_points()
        self.assertEqual(
            0, points, f'Points for \'javascrip_\' should be 0 '
            f'and not {points}')

    def test_update_points_javascript_wrong_7(self):
        self.game_two.word = 'javascript'
        self.game_two.word_display = list('j_v_s_____')
        self.game_two.incorrect_guesses = ['n', 'o', 'l', 'q', 'b', 'y']
        self.game_two.correct_guesses = ['v', 'j', 's']
        points = self.game_two.update_points()
        self.assertEqual(
            -5, points, f'Points for \'j_v_s_____\' should '
            f'be -5 and not {points}')

    def test_update_list_of_words_and_reset_game(self):
        self.game_one.set_word()
        self.game_one.word = 'hello'
        self.game_one.word_display = list('hello')
        self.game_one.reset_game()
        self.assertIn('hello', self.game_one.words_played,
                      'Word not present in words played set')
        self.assertIn('hello', self.game_one.words_guessed,
                      'Word not present in the correctly guessed set')
        self.assertEqual(2, self.game_one.game_number,
                         'The game class number is incorrect')

        self.game_one.set_word()
        self.game_one.word = 'javascript'
        self.game_one.word_display = list('j_v_s_____')
        self.game_one.incorrect_guesses = ['n', 'o', 'l', 'q', 'b', 'y']
        self.game_one.correct_guesses = ['v', 'j', 's']
        self.game_one.reset_game()
        self.assertIn('javascript', self.game_one.words_played,
                      'Word not present in words played set')
        self.assertNotIn('javascript', self.game_one.words_guessed,
                         'Word not present in the correctly guessed set')
        self.assertIn('hello', self.game_one.words_played,
                      'Word not present in words played set')
        self.assertIn('hello', self.game_one.words_guessed,
                      'Word not present in the correctly guessed set')
        self.assertEqual(3, self.game_one.game_number,
                         'The game class number is incorrect')
Exemple #18
0
"""Slightly more advanced example of using HangmanGame."""

from hangman import HangmanGame
from PyDictionary import PyDictionary
from goslate import Goslate
import random

print('\n\nH A N G M A N\n')
print('Hi there! Let\'s see if you can save the first Hangman\n')

game = HangmanGame()

end_game = False
while not end_game:
    game.set_word()
    game_over = not game.get_status() == 'guessing'

    print(game.get_hangman())
    print(game.get_position())

    while not game_over:
        game.guess_letter()
        print(game.get_hangman())
        print(game.get_position())
        game_over = not game.get_status() == 'guessing'

    if game.get_status() == 'won':
        print('Congratulations! You guessed the word correctly.')
    else:
        print(f'The word to be guessed is: {game.get_word()}')
        print('I am sure you will do better next time. :)\n')
Exemple #19
0
def main():
  word_to_guess = getpass('Please enter a word for the opponent to guess: ')
  new_game = HangmanGame(word_to_guess)
  new_game.play_game()
Exemple #20
0
"""Simple example of using HangmanGame."""

from hangman import HangmanGame


print('\n\nH A N G M A N\n')

# Create the game.
game = HangmanGame()

game.set_word()
game_over = not game.get_status() == 'guessing'

# Loop while the player is guessing.
while not game_over:
    print(game.get_hangman())
    print(game.get_position())
    game.guess_letter()
    game_over = not game.get_status() == 'guessing'

print(game.get_hangman())
print(game.get_position())

# Messages for the player post-game.
if game.get_status() == 'won':
    print('Congratulations! You guessed the word correctly.')
else:
    print(f'The word is: {game.get_word()}')
    print('I am sure you will do better next time. :)')

print('\nThanks for playing!')
Exemple #21
0
        print('  +------+ \n'
              '  |      | \n'
              '  0      | \n'
              ' /|\     | \n'
              ' / \     | \n'
              '         | \n'
              '########### ')


def will_continue(option: str) -> bool:
    option = option.upper()
    return option == 'Y' or option == 'YES'


if __name__ == '__main__':
    hm = HangmanGame()
    print('  HANGMAN')

    while True:
        if hm.is_active_game():
            try:
                print_gallows(hm.get_tries())
                print(hm)
                hm.make_guess(input('Make your guess: '))
                print()
            except InvalidGuess as invalid:
                print('\n' + str(invalid))
        else:
            print_gallows(hm.get_tries())
            if hm.is_victory(): print('YOU DID IT!')
            else: print('GAME OVER')
Exemple #22
0
 def setUp(self):
     """Creates GamePlay class for all the unittests."""
     self.game_one = HangmanGame()
     self.game_two = HangmanGame()