def play(user: dict) -> None: """Get a dictionary(user's info) and play game. Until user input 'quit', keep playing game check if user's input is valid, if so move inside boundary if user encounters monster, ask whether fight or run away if user tries to run away, monster hits user as much as 1d4 by 10 percent chance if user fight, invoke combat() to death if user doesn't encounter monster and user's hp is below 10, up 1hp each move if user input invalid input for direction, print helpful message PARAM: user, a dictionary PRECONDITION: user, must be a dictionary has valid keys and values POSTCONDITION: Print helpful message when user input invalid string """ user_input = "" map.show_map(user) while user_input != "quit": user_input = input( "Which way do you want to go? (E/W/N/S) ").strip().lower() if moving.can_move(user_input, user['row'], user['column']): moving.move(user_input, user) if monster.encounter_monster(): combat.fight_or_run_away(user) if combat.is_dead(user): user[ 'Hp'], user_input = 10, "quit" # Hp full up for the next game, and quit the game. else: support_function.up_1_hp(user) elif user_input == "quit": support_function.save_user_info(user) else: print("Enter the direction again please.")
def test_move_raises_key_error(self): """Test move with a dictionary doesn't have key 'row'. The result is expected to raise KeyError """ with self.assertRaises(KeyError): moving.move('n', self.no_row_dict)
def test_move_third_condition_with_valid_arguments(self): """Test the third condition of move_first with valid arguments. user_input in ['north', 'n'] The result is expected True """ moving.move('n', self.test_dict) self.assertTrue(self.test_dict['row'] == 3)
def test_move_fourth_condition_with_valid_arguments(self): """Test the fourth condition of move_first with valid arguments. user_input in ['south', 's'] The result is expected True """ moving.move('s', self.test_dict) self.assertTrue(self.test_dict['row'] == 5)
def test_move_second_condition_with_valid_arguments(self): """Test the second condition of move_first with valid arguments. user_input in ['west', 'w'] The result is expected True """ moving.move('w', self.test_dict) self.assertTrue(self.test_dict['column'] == 3)