Ejemplo n.º 1
0
def main():
    interface = Interface()

    replay = "Y"

    menu_input_flag = 0

    while menu_input_flag == 0:
        interface.clear_screen()
        interface.write_title()
        interface.write_options()
        menu_choice = interface.menu_choice()

        if menu_choice == "1":
            menu_input_flag = 1
            interface.clear_screen()
            while replay == "Y":
                interface.clear_screen()
                player = Player()
                game = Game(player, interface)
                game.start()
                replay = interface.read_replay()

        elif menu_choice == "2":
            interface.clear_screen()
            interface.write_title()
            interface.write_instructions()

        elif menu_choice == "3":
            sys.exit(0)

        else:
            print("Invalid input. Please try again.")
            time.sleep(0.5)
Ejemplo n.º 2
0
def start_game():
    game = Game()
    player1 = HumanPlayer()

    player2_class = player1.select_other_player()
    player2 = player2_class()

    while True:
        winner = game.get_winner()

        if winner:
            print('Winner is {}'.format(winner))
            break

        if player1.should_change_turns():
            print('Changed turns!')
            player1, player2 = player2, player1
            print('Guessing: {}, opponent: {}'.format(
                player1.id,
                player2.id,
            ))

        word = player1.quess_new_word()
        current_round = Round(word)
        print('New word is guessed!')

        while not current_round.is_finished():
            print(current_round.mask_word())
            current_round.draw_field()
            print()
            letter = input('Letter: ')
            current_round.try_letter(letter)

        current_round.draw_result()
        game.add_round(current_round, player1, player2)
Ejemplo n.º 3
0
def test_is_not_missed():
    """
    Tests that correct letter is found in the word
    :return:
    """
    game = Game()
    game.word = 'word'
    assert game.is_missed('w') is False
Ejemplo n.º 4
0
def test_punish():
    """
    Tests that `punish()` actually increases the counter
    :return:
    """
    game = Game()
    game.punish()
    assert game.mistake_count == 1
Ejemplo n.º 5
0
def test_is_missed():
    """
    Tests that wrong letter isn't found in the word
    :return:
    """
    game = Game()
    game.word = 'word'
    assert game.is_missed('x') is True
Ejemplo n.º 6
0
 def setUp(self):
     interface = Interface()
     player = Player()
     self.game = Game(player, interface)
     self.game.print_graphics = Mock(return_value=None)
     self.game.interface.write_invalid_guess = Mock(return_value=None)
     self.game.interface.write_already_guessed = Mock(return_value=None)
     self.game.interface.write_incorrect_guess = Mock(return_value=None)
     self.game.interface.write_correct_guess = Mock(return_value=None)
     self.game.won = Mock(return_value=None)
     self.game.lost = Mock(return_value=None)
Ejemplo n.º 7
0
def test_check_solved():
    """
    Tests that positive end-game condition is tracked
    :return:
    """
    game = Game()
    game.word = 'word'
    game.pattern = 'word'
    game.check_solved()
    assert game.solved is True
Ejemplo n.º 8
0
def test_easy_win():
    """
    Check if we won the game the current_word is the rigth one
    """
    game = Game()
    game.set_dict(['win'])
    game.input_stream = ['w', 'i', 'n']
    result = game.start()

    assert result
Ejemplo n.º 9
0
def test_add_round():
    game = Game()
    result = inspect.getsource(game.add_round)
    assert 'won_rounds[winner.id] += 1' in result
    assert 'if' in result
Ejemplo n.º 10
0
def test_gets_winner():
    game = Game()
    assert game.get_winner() is None
    assert game.get_winner()
from hangman.game import Game
from hangman.game_status import GameStatus


def chars_list_to_str(chars):
    return ''.join(chars)


game = Game()
word = game.generate_word()

letters_count = len(word)

print(f"The word consists of {letters_count} letters.")
print("Try to guess the word letter by letter.")

while game.game_status == GameStatus.IN_PROGRESS:
    letter = input("Pick a letter.\n")
    state = game.guess_letter(letter)
    print(chars_list_to_str(state))

    print(f"Remaining tries = {game.remaining_tries}")
    print(f"Tried letters: {chars_list_to_str(game.tried_letters)}")

if game.game_status == GameStatus.LOST:
    print("You're hanged!")
    print(f"The word was: {game.word}")
else:
    print("Congratulations! You won!")
Ejemplo n.º 12
0
class TestGame(TestCase):
    """
    Unit tests for the Game class.
    """
    def setUp(self):
        interface = Interface()
        player = Player()
        self.game = Game(player, interface)
        self.game.print_graphics = Mock(return_value=None)
        self.game.interface.write_invalid_guess = Mock(return_value=None)
        self.game.interface.write_already_guessed = Mock(return_value=None)
        self.game.interface.write_incorrect_guess = Mock(return_value=None)
        self.game.interface.write_correct_guess = Mock(return_value=None)
        self.game.won = Mock(return_value=None)
        self.game.lost = Mock(return_value=None)

    def test_create_hist(self):
        """
        1. hist exists
        2. hist.keys() contains all letters of words
        """

        self.assertIsInstance(self.game.hist, dict)
        self.assertTrue(len(self.game.hist.keys()) > 0)

    def test_start_scenario_1(self):
        """
        Scenario 1: All incorrect guesses

        1.1 player is asked to make a guess 9 times
        1.2 player health is 0 at the end of the game
        1.3 player guesses contains the expected letters at the end of the
            game
        1.4 player is informed that they lost at the end of the game
        1.5 player is informed their guess is wrong 9 times
        1.6 hint consists only of dashes at the end of the game
        """

        self.setUpStart()
        incorrect_guesses = ["Q", "W", "E", "T", "Y", "U", "I", "P", "S"]
        self.game.interface.read_player_guess = Mock(
            side_effect=incorrect_guesses)

        self.game.start()

        # 1.1
        self.assertEqual(self.game.interface.read_player_guess.call_count,
                         len(incorrect_guesses))

        # 1.2
        self.assertEqual(self.game.player.health.gethealth(), 0)

        # 1.3
        self.assertEqual(self.game.player.getguesses(), incorrect_guesses)

        # 1.4
        self.game.lost.assert_called_once()

        # 1.5
        self.assertEqual(self.game.interface.write_incorrect_guess.call_count,
                         len(incorrect_guesses))

        # 1.6
        self.assertEqual(self.game.hint, "______")

    def test_start_scenario_2(self):
        """
        Scenario 1: All correct guesses

        2.1 player is asked to make a guess 5 times
        2.2 player health is 9 at the end of the game
        2.3 player guesses contains the expected letters at the end of the
            game
        2.4 player is informed that they won at the end of the game
        2.5 player is informed their guess is correct 9 times
        2.6 hint consists of letters and no dashes at the end of the game
        """

        self.setUpStart()
        correct_guesses = ["F", "O", "B", "A", "R"]
        self.game.interface.read_player_guess = Mock(
            side_effect=correct_guesses)

        self.game.start()

        # 2.1
        self.assertEqual(self.game.interface.read_player_guess.call_count,
                         len(correct_guesses))

        # 2.2
        self.assertEqual(self.game.player.health.gethealth(), 9)

        # 2.3
        self.assertEqual(self.game.player.getguesses(), correct_guesses)

        # 2.4
        self.game.won.assert_called_once()

        # 2.5
        self.assertEqual(self.game.interface.write_correct_guess.call_count,
                         len(correct_guesses))

        # 2.6
        self.assertEqual(self.game.hint, "FOOBAR")

    def test_start_scenario_3(self):
        """
        Scenario 3: realistic gameplay, game won

        3.1 player is asked to make a guess until word is guessed correctly
        3.2 player health is reduced for each incorrect guess
        3.3 player health is not 0 at the end of the game
        3.4 player guesses contains the expected letters at the end of the
          game
        3.5 player is informed they won at the end of the game
        3.6 player is informed their guess is correct for each correct guess
        3.7 player is informed their guess is incorrect for each incorrect guess
        3.8 player is informed they already guessed the letter for each repeat
          guess
        3.9 hint consists only of dashes at the end of the game
        """

        self.setUpStart()
        player_guesses = [
            "A", "E", "I", "O", "U", "B", "O", "F", "S", "A", "R"
        ]
        self.game.interface.read_player_guess = Mock(
            side_effect=player_guesses)

        self.game.start()

        # 3.1
        self.assertEqual(self.game.interface.read_player_guess.call_count,
                         len(player_guesses))

        # 3.2 & 3.3
        self.assertEqual(self.game.player.health.gethealth(), 5)

        # 3.4
        end_player_guesses = ["A", "E", "I", "O", "U", "B", "F", "S", "R"]
        self.assertEqual(self.game.player.getguesses(), end_player_guesses)

        # 3.5
        self.game.won.assert_called_once()

        # 3.6
        self.assertEqual(self.game.interface.write_correct_guess.call_count, 5)

        # 3.7
        self.assertEqual(self.game.interface.write_incorrect_guess.call_count,
                         4)

        # 3.8
        self.assertEqual(self.game.interface.write_already_guessed.call_count,
                         2)

        # 3.9
        self.assertEqual(self.game.hint, "FOOBAR")

    def test_start_scenario_4(self):
        """
        Scenario 4: realistic gameplay, game lost

        4.1 player is asked to make a guess until they run out of health
        4.3 player health is 0 at the end of the game
        4.4 player guesses contains the expected letters at the end of the
          game
        4.5 player is informed they lost at the end of the game
        4.6 player is informed their guess is correct for each correct guess
        4.7 player is informed their guess is incorrect for each incorrect guess
        4.8 player is informed they already guessed the letter for each repeat
          guess
        4.9 hint consists of dashes and letters at the end of the game
        """

        self.setUpStart()
        player_guesses = [
            "X", "W", "T", "A", "E", "I", "O", "U", "B", "O", "F", "S", "A",
            "M", "N"
        ]
        self.game.interface.read_player_guess = Mock(
            side_effect=player_guesses)

        self.game.start()

        # 4.1
        self.assertEqual(self.game.interface.read_player_guess.call_count,
                         len(player_guesses))

        # 4.3
        self.assertEqual(self.game.player.health.gethealth(), 0)

        # 4.4
        end_player_guesses = [
            "X", "W", "T", "A", "E", "I", "O", "U", "B", "F", "S", "M", "N"
        ]
        self.assertEqual(self.game.player.getguesses(), end_player_guesses)

        # 4.5
        self.game.lost.assert_called_once()

        # 3.6
        self.assertEqual(self.game.interface.write_correct_guess.call_count, 4)

        # 3.7
        self.assertEqual(self.game.interface.write_incorrect_guess.call_count,
                         9)

        # 3.8
        self.assertEqual(self.game.interface.write_already_guessed.call_count,
                         2)

        # 3.9
        self.assertEqual(self.game.hint, "FOOBA_")

    def setUpStart(self):
        mock_word = Mock(return_value="FOOBAR")
        self.game.word = mock_word()
        mock_word_hist = {"F": [0], "O": [1, 2], "B": [3], "A": [4], "R": [5]}
        mock_hist = Mock(return_value=mock_word_hist)
        self.game.hist = mock_hist()
        mock_word_hint = "_" * len(self.game.word)
        mock_hint = Mock(return_value=mock_word_hint)
        self.game.hint = mock_hint()