def main(): parser = argparse.ArgumentParser(description='hangman game') parser.add_argument('--dict', type=str, required=False, default='dict.txt', help='path to dictionary, file with words') args = parser.parse_args() game = Game(words_path=args.dict) game.start_game()
def test_4(): game = Game() word = 'rrrrr' char = 'r' game.visible_word = ['*', '*', '*', '*', '*'] game.remains = len(word) result = game._check_word(word, char) assert result == 'Hit!', 'Wrong method output' assert game.remains == 0 and game.curr_mistake == 0, 'Wrong counters' assert game.visible_word == ['r', 'r', 'r', 'r', 'r'], 'Wrong word output' print('TEST 4 is OK')
def test_1(): game = Game() word_1 = 'look' char = 'o' game.visible_word = ['*', '*', '*', '*'] game.remains = len(word_1) result = game._check_word(word_1, char) assert result == 'Hit!', 'Wrong method output' assert game.remains == 2 and game.curr_mistake == 0, 'Wrong counters' assert game.visible_word == ['*', 'o', 'o', '*'], 'Wrong word output' print('TEST 1 is OK')
def test_2(): game = Game() word = 'qwerr' char = 'l' game.visible_word = ['*', '*', '*', '*', '*'] game.remains = len(word) result = game._check_word(word, char) assert result == 'Missed, mistake 1 out of 5', 'Wrong method output' assert game.remains == len(word) and game.curr_mistake == 1, 'Wrong counters' assert game.visible_word == ['*', '*', '*', '*', '*'], 'Wrong word output' print('TEST 2 is OK')
def test_play(): def new_iteration(self): self.boolean = False captured_output = StringIO.StringIO() sys.stdout = captured_output with patch.object(Game, 'iteration', new_iteration): new_game = Game() new_game.word = 'hello' new_game.play() sys.stdout = sys.__stdout__ assert captured_output.getvalue() == 'The word: *****\n\nGuess a letter:\n' assert not new_game.boolean
def test_game_win(inp, capfd): game = Game() game._answer = 6 game() assert game._win is True out = capfd.readouterr()[0] expected = [ '4 is too low', 'Number not in range', '9 is too high', 'Already guessed', '6 is correct!', 'It took you 3 guesses' ] output = [line.strip() for line in out.split('\n') if line.strip()] for line, exp in zip(output, expected): assert line == exp
def test_two_letters(): """ Checks if a guess is correct and there are more than one guessed letter in a word, it changes all """ game = Game() game.words = ['kitty'] game.pick_random() guess = 't' _, word = game.check_guess(guess) assert word == '**tt*'
def test_bad_guess(): """ Checks if a guess is incorrect """ game = Game() game.words = ['game'] game.pick_random() guess = 'q' check, word = game.check_guess(guess) assert not check assert word == '****'
def test_win(): """ Checks win scenario """ game = Game() game.words = ['game'] game.pick_random() game.input_stream = [ 'a', 'g', 'm', 'e', ] check = game.start_game() assert check assert ''.join(game.guess_word) == 'game'
def test_validate_guess(capfd): game = Game() game._answer = 2 assert not game._validate_guess(1) out, _ = capfd.readouterr() assert out.rstrip() == '1 is too low' assert not game._validate_guess(3) out, _ = capfd.readouterr() assert out.rstrip() == '3 is too high' assert game._validate_guess(2) out, _ = capfd.readouterr() assert out.rstrip() == '2 is correct!'
def test_fail(): """ Checks lose scenario """ game = Game() game.words = ['game'] game.pick_random() game.input_stream = [ 'a', 'g', 'm', 'b', 'c', 'd', 'f', 'i', ] check = game.start_game() assert not check assert ''.join(game.guess_word) == 'gam*'
from hangman import User from hangman import Game from os import system print("Nice to meet you! Welcome to Hangman!") print("First some rules:") print("1. You can input ONLY one letter per round") print("2. You've got 10 chances") print("3. Category is fruit name\n") print("If You are ready to play press y [YES]") start = input() system('clear') if start == 'y': user1 = User() game = Game(user1) user1.introduce() game.random_word() while True: user1.choose_letter() game.play_game() else: print("See you next time!")
def test_5(): game = Game() game.curr_mistake = 5 result = game.play() assert result == 'You lost!', 'Wrong game result' print('TEST 5 is OK')
def test_game_lose(inp, capfd): game = Game() game._answer = 13 game() assert game._win is False
def test_guess(inp): game = Game() # good assert game.guess() == 'a' assert game.guess() == 'c' # not in range a-z with pytest.raises(ValueError): game.guess() # not in range a-z with pytest.raises(ValueError): game.guess() # good assert game.guess() == 'z' # only single letters allowed with pytest.raises(ValueError): game.guess() # already guessed with pytest.raises(ValueError): game.guess() # good assert game.guess() == 'g' # user hit enter with pytest.raises(ValueError): game.guess()
def test_iteration(): # test_1 captured_output = StringIO.StringIO() sys.stdout = captured_output new_game = Game() with patch('hangman.letter_input') as perm_mock: perm_mock.return_value = 'e' new_game.word = 'kek' new_game.missed = 3 new_game.guesses = set(['k']) new_game.iteration() sys.stdout = sys.__stdout__ assert captured_output.getvalue() == 'Hit!\n\nThe word: kek\n\nYou won!\n' assert new_game.missed == 3 assert new_game.guesses == set(['k', 'e']) # test_2 captured_output = StringIO.StringIO() sys.stdout = captured_output new_game = Game() with patch('hangman.letter_input') as perm_mock: perm_mock.return_value = 'e' new_game.word = 'lol' new_game.missed = 4 new_game.guesses = set(['k']) new_game.iteration() sys.stdout = sys.__stdout__ printed_str = 'Missed, mistake 5 out of 5 \n\nThe word: ***\n\nYou lost!\n' assert captured_output.getvalue() == printed_str assert new_game.missed == 5 assert new_game.guesses == set(['k', 'e'])