def test_show_game_when_game_starts(self, mock_word, mock_dict): mock_dict.return_value.draw_random_word.return_value = self.tword tgame = sm.Game() expected_snowman = "\n\n\n\n\n\n" expected_text = "\n\n\nWORD TO GUESS: _ _ _ _ _ _ _\n\nMISSED LETTERS: \n\nMISSINGS LEFT: 8\n\n" self.assertEqual(tgame.show_game(), f"{expected_snowman}{expected_text}", "Incorrect appearance when game start.")
def test_process_letter_when_letter_not_in_word_and_not_used(self, mock_word, mock_dict): mock_dict.return_value.draw_random_word.return_value = self.tword tgame = sm.Game() result = tgame.process_letter("x") self.assertTrue(result, "Incorrect result.") self.assertEqual(len(tgame.letters_used), 1, "Expected letter added to set.") self.assertEqual(len(tgame.letters_missed), 1, "Expected letter added to set.")
def test_is_letter_correct_no_language_specific(self, mock_dict): tgame = sm.Game() self.assertTrue(tgame.is_letter_correct("a"), "Incorrect letter validation.") self.assertFalse(tgame.is_letter_correct("1"), "Incorrect letter validation.") self.assertFalse(tgame.is_letter_correct("-"), "Incorrect letter validation.") self.assertFalse(tgame.is_letter_correct("abc"), "Incorrect letter validation.") self.assertFalse(tgame.is_letter_correct(""), "Incorrect letter validation.")
def test_show_game_when_all_possible_missed_letters(self, mock_word, mock_dict): mock_dict.return_value.draw_random_word.return_value = self.tword tgame = sm.Game() tgame.letters_missed = set("qrtyuipd") expected_snowman = '\n _\n \ _[_]_ /\n \ ( ">) /\n (__:__)\n / : \\\n \,_____,/\n' expected_text = "\n\nWORD TO GUESS: _ _ _ _ _ _ _\n\nMISSED LETTERS: D, I, P, Q, R, T, U, Y\n\nMISSINGS LEFT: 0\n\n" self.assertEqual(tgame.show_game(), f"{expected_snowman}{expected_text}", "Incorrect appearance when game end.")
def test_show_game_when_in_the_middle_of_the_game(self, mock_word, mock_dict): mock_dict.return_value.draw_random_word.return_value = self.tword tgame = sm.Game() tgame.letters_missed = set("qrty") tgame.letters_used = set("qrtysna") expected_snowman = '\n\n\n ( " )\n (__:__)\n / : \\\n \,_____,/\n' expected_text = "\n\nWORD TO GUESS: S N _ _ _ A N\n\nMISSED LETTERS: Q, R, T, Y\n\nMISSINGS LEFT: 4\n\n" self.assertEqual(tgame.show_game(), f"{expected_snowman}{expected_text}", "Incorrect appearance when in the mddle of the game.")
def test_process_letter_when_letter_in_used_letters(self, mock_word, mock_dict): mock_dict.return_value.draw_random_word.return_value = self.tword tgame = sm.Game() tgame.letters_used = set(["n"]) result = tgame.process_letter("n") mock_word.assert_not_called() self.assertFalse(result, "Incorrect result.") self.assertEqual(len(tgame.letters_used), 1, "Expected letter not added to set.") self.assertEqual(len(tgame.letters_missed), 0, "Expected letter not added to set.")
def test_reset_game_state(self, mock_dict): mock_dict.return_value.draw_random_word.return_value = self.tword tgame = sm.Game() tword2 = sm.Word("house") tgame.words_used.add(tword2) tgame.word_to_guess = tword2 tgame.letters_missed = set("qrtyipd") tgame.letters_used = set("qrtyipdus") tgame.reset_game_state() self.assertIsInstance(tgame.letters_missed, set, "Expected type of set.") self.assertEqual(len(tgame.letters_missed), 0, "Expected empty set.") self.assertIsInstance(tgame.letters_used, set, "Expected type of set.") self.assertEqual(len(tgame.letters_used), 0, "Expected empty set.") self.assertNotEqual(tgame.word_to_guess, tword2, "Expected new word drawed.")
def test_is_letter_correct_pl(self, mock_dict): tgame = sm.Game("pl") for letter in ["ę", "ó", "ą", "ś", "ł", "ż", "ź", "ć", "ń"]: self.assertTrue(tgame.is_letter_correct(letter), "Incorrect letter validation for polish language.")
def test_is_letter_correct_en(self, mock_dict): tgame = sm.Game("en") for letter in ["ę", "ó", "ą", "ś", "ł", "ż", "ź", "ć", "ń"]: self.assertFalse(tgame.is_letter_correct(letter), "Incorrect letter validation for english language.")
def test_draw_random_word(self, mock_dict): mock_dict.return_value.draw_random_word.return_value = self.tword tgame = sm.Game() self.assertEqual(len(tgame.words_used), 1, "Expected used words modified.")
def test_is_lost(self, mock_dict): tgame = sm.Game() tgame.letters_missed = set("qrtyuipd") self.assertTrue(tgame.is_lost())
def test_is_win(self, mock_word, mock_dict): mock_dict.return_value.draw_random_word.return_value = self.tword tgame = sm.Game() self.assertTrue(tgame.is_win())
import snowman import word_selector # This is a riff on the old game "hangman", but with a melting snowman instead # # _|@|_ # (") # '--( : )--' # ( : ) word = "puppy" game = snowman.Game(word_selector.get_word(), 5) # Start the main thread for the game print("Guess the word before the snowman melts!") while game.wrong_guesses <= game.tries and game.word_revealed is False: print(game.get_snowman()) print(f"The mystery word so far: {game.revealed_word()}") player_response = input("Guess a letter\n") print(f"You entered {player_response}") if game.valid_guess(player_response): if game.repeat_letter(player_response): print( f"I'm sorry, but you already guessed the letter {player_response}." ) print(f"You've guessed the following letters: {game.guesses}\n") else: if game.guess(player_response): print( f"Congratulations! The letter {player_response} is in the secret word."
elif challenger == "brian": guesser = snowman_solver.BozSolver() else: print(f"No guessing strategy was found for the challenger {challenger}") exit(1) # create output file of statistics stats_file = f"stats/stats-{player}-vs-{challenger}.txt" fhand = open(stats_file, "w") print("word,length,right,wrong,guesses,outcome", file=fhand) # run contest over selected number of words for r in range(0,num_games): # pick a word - create a snowman game for it word = word_selector.get_word(player) game = snowman.Game(word, num_allowed) print("Running word is: ",word," <- ") # initialize guesser guesser.initialize(len(word)) # run game while game.word_revealed is False: guess = guesser.make_guess(game.revealed_letters) result = game.guess(guess) # collect stats info = {} info['word'] = word info['length'] = len(word) info['num_hit'] = game.right_guesses
guesses_needed = [] stats = {} # track statistics while round < games: round += 1 tries = 0 game_result = "Loss" challenge_word = word_selector.get_word() print(f"Round {round}: Word is {challenge_word}") print(" ", end="") # my_solver = snowman_solver.Solver("frequency by length", len(challenge_word)) my_solver = snowman_solver.DanSolver("ds adaptive", len(challenge_word)) # 93 % success my_game = snowman.Game(challenge_word, 20) # keep statistics length = len(my_game.word) # length of the word if length in stats: stats[length]['num'] += 1 else: stats[length] = {} stats[length]['num'] = 1 stats[length]['win'] = 0 while my_game.word_revealed is False: tries += 1 guess = my_solver.make_guess(my_game.revealed_letters) round_result = my_game.guess(guess) if round_result: