def combat(): """ Process the combat loop between character and monster. POST-CONDITION character and monster takes damage until one dies RETURN True if monster is slain RETURN False if character is slain """ if combat_flee(): if character.get_hp() > 0: return True else: return False monster.reset() print("--- COMBAT STARTS ---") while True: print("YOUR STRUCTURAL INTEGRITY:", character.get_hp(), "\nENEMY STRUCTURAL INTEGRITY:", monster.get_hp()) input("--- PRESS ANY KEY TO CONTINUE ---") character_roll = roll_die(1, 6) print("USS ENTERPRISE FIRES:", character_roll, "PHOTON TORPEDO(ES)") if not monster.modify_hp(-character_roll): print("ENEMY STRUCTURAL INTEGRITY: 0", "\nENEMY DESTROYED", "\n--- COMBAT ENDS ---") input("--- PRESS ANY KEY TO CONTINUE ---") return True monster_roll = roll_die(1, 6) print("BIRD OF PREY FIRES:", monster_roll, "PHOTON TORPEDO(ES)") if not character.modify_hp(-monster_roll): print("ENEMY STRUCTURAL INTEGRITY:", monster.get_hp(), "\nYOUR STRUCTURAL INTEGRITY: 0", "\nDEFEATED") return False
def test_potential_attack_hp(self): """Assert users HP after damage.""" random.seed(43) opponent = {'Name': 'Lapras', 'Attack': 'Ice Shard', 'HP': 5} potential_attack(opponent) self.assertEqual(get_hp(), 4) random.seed()
def test_potential_attack_fled(self): """Assert HP of user after successful flee.""" random.seed(21) opponent = {'Name': 'Scyther', 'Attack': 'Slash', 'HP': 5} potential_attack(opponent) self.assertEqual(get_hp(), 7) random.seed()
def test_game_over_reset(self): character.set_hp(0) try: game_over() except SystemExit: pass self.assertEqual(10, character.get_hp())
def test_restart_game_reset(self, mock_input): character.set_hp(0) try: restart_game() except SystemExit: pass self.assertEqual(10, character.get_hp())
def test_play_game_game_event(self, mock_stdout, mock_input, mock_random): character.set_hp(9) character.set_coordinates(20, 20) try: play_game() except SystemExit: pass self.assertEqual(10, character.get_hp())
def game_event(): """ Process the game play loop. POST-CONDITION make appropriate changes to character and monster according to events """ # Combat if random.random() < 0.1: if not combat.combat(): game_over() else: # Heal if character.get_hp() < character.MAX_HP(): character.modify_hp(1) print("STRUCTURAL INTEGRITY +1 POINT", "\nYOUR STRUCTURAL INTEGRITY:", character.get_hp()) map.print_map(character.get_coordinates())
def test_quit_game_save_game(self): character.set_hp(5) try: quit_game() except SystemExit: pass character.set_hp(10) load_game() self.assertEqual(5, character.get_hp())
def test_load_game(self): character_dictionary = {'hp': 5, 'column': 20, 'row': 20} with open('character.json', 'w') as file_object: json.dump(character_dictionary, file_object, sort_keys=True, indent=4) save.load_game() current_character = {"hp": character.get_hp(), "column": character.get_coordinates()[0], "row": character.get_coordinates()[1]} self.assertEqual({"hp": 5, "column": 20, "row": 20}, current_character)
def combat_round(opponent: dict) -> None: """Determine the amount of damage between character and opponent. PRECONDITION opponent: must be a well-formed dictionary from random_pokemon >>> random.seed(3) >>> combat_round({'Name': 'Palkia', 'Class': 'Water', 'Attack': 'Pressure', 'HP': 5}) You attacked Palkia with a slap and he took 2 damage. Palkia attacked you with Pressure and you took 5 damage. You attacked Palkia with a slap and he took 5 damage. Success! Your opponent has fainted and you gained 20 prize dollars from your battle. <BLANKLINE> >>> random.seed() """ pokemon_attack = roll_die(1, 6) opponent_attack = roll_die(1, 6) if opponent["HP"] > 0: opponent["HP"] -= pokemon_attack print("You attacked %s with a slap and he took %s damage." % (opponent["Name"], pokemon_attack)) if opponent["HP"] <= 0: print( "Success! Your opponent has fainted and you gained 20 prize dollars from your battle.\n" ) if opponent["HP"] <= 0: return None if get_hp() > 0: change_hp(-opponent_attack) print("%s attacked you with %s and you took %s damage." % (opponent["Name"], opponent["Attack"], opponent_attack)) if get_hp() <= 0: print("You fainted. Try again next time!") exit() while True: return combat_round(opponent)
def save_game(): """ Save character data to character.json. POST-CONDITION character data is saved to character.json """ character_dictionary = { 'hp': character.get_hp(), 'column': character.get_coordinates()[0], 'row': character.get_coordinates()[1] } with open('character.json', 'w') as file_object: json.dump(character_dictionary, file_object, sort_keys=True, indent=4)
def combat_flee_damage(): """ Check if character takes damage from fleeing. POST-CONDITION 10% chance character takes damage from fleeing """ if random.random() < 0.1: monster_roll = roll_die(1, 4) print("--- WARP OUT PENALTY ---", "\nBIRD OF PREY FIRES:", monster_roll, "PHOTON TORPEDO(ES)") if not character.modify_hp(-monster_roll): print("YOUR STRUCTURAL INTEGRITY: 0", "\nYOU ARE DESTROYED") else: print("YOUR STRUCTURAL INTEGRITY:", character.get_hp()) else: print("WARPED OUT SAFELY")
def test_encounter_percentage_fight(self, mock_user_input): """Test for HP change after completed combat.""" random.seed(31) encounter_percentage({"Name": "Egg", "Attack": "Punch", "HP": 5}) self.assertEqual(get_hp(), 9) random.seed()
def test_combat_flee_damage_take_damage_hp(self, mock_random, mock_roll): character.modify_hp(10) combat_flee_damage() self.assertEqual(9, character.get_hp())
def test_get_hp(self): self.assertEqual(character.hp, character.get_hp())
def test_get_hp_output(self): """Assert users most updated HP.""" self.assertEqual(get_hp(), 9)
def test_get_hp_type(self): """Assert that users HP is an int.""" self.assertEqual(type(get_hp()), int)
def test_game_event_no_heal(self, mock_random): character.set_hp(10) game_event() self.assertEqual(10, character.get_hp())
def test_move_character_hp(self, mock_user_input): """Assert that HP will increase by one if HP is less than 10.""" move_character() self.assertEqual(get_hp(), 9)
def test_move_character_hp_max(self, mock_user_input): """Assert that HP will not be greater than 10.""" pokemon['HP'] = 10 move_character() self.assertEqual(get_hp(), 10)