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 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 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_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_modify_hp_negative(self): character.hp = 6 character.modify_hp(-1) self.assertEqual(5, character.hp)
def test_modify_hp_over_max(self): character.hp = 5 character.modify_hp(10) self.assertEqual(10, character.hp)
def test_modify_hp_under_min(self): character.hp = 5 character.modify_hp(-10) self.assertEqual(0, character.hp)
def test_modify_hp_zero(self): character.hp = 5 character.modify_hp(0) self.assertEqual(5, character.hp)
def test_modify_hp_positive(self): character.hp = 4 character.modify_hp(1) self.assertEqual(5, character.hp)