def get_parsed(phrase, select): hidden, key = parse_phrase(phrase) if select == 'hidden': #hidden list is converted to string with join return ''.join(hidden) elif select == 'key': return key
def test_improper_input(self): self.assertFalse(parse_phrase('!')) self.assertFalse(parse_phrase('?')) self.assertFalse(parse_phrase(2)) self.assertFalse(parse_phrase('4'))
def test_key(self): hidden, key = parse_phrase('test') self.assertEqual(get_parsed('test', 'key'), {'T': [0, 3], 'E': [1], 'S': [2]})
def main(): #clear screen def clear(): if os.name == 'nt': os.system('cls') else: os.system('clear') #clear screen on start clear() #print welcome message print('Welcome to Hangman') print('You will have 6 guesses to guess each word') input('Press Enter to continue..') #parse phrases in phrases.txt and store as list of Phrase objects phrase_list = [] with open('phrases.txt', 'r') as f: for phrase in re.findall('\'([^\n]+)\'', f.read()): phrase_hidden, phrase_key = parse_phrase(phrase) phrase_list.append(Phrase(phrase, phrase_hidden, phrase_key)) #initiate game game = Game(phrase_list) #clear screen to start game clear() #loop game session until game ends while True: #print hidden phrase and guesses remaining game.display() #move to next phrase if current phrase is solved if game.is_solved(): #check if any phrases remaining if game.is_won(): print('All phrases solved! You win!') input('Press Enter to exit..') break else: print('Solved!') input('Press Enter to move to next phrase..') game.get_next_phrase() clear() continue #end game if 0 guesses remaining if game.guesses_remaining == 0: print('Game Over') input('Press Enter to exit..') break #loop until player guesses a valid letter while True: guess = input('Enter guess: ').upper() clear() if game.is_guess_valid(guess): break else: game.display(False) continue #check if guess is in phrase and update accordingly game.check_guess(guess)