def run_away(): """runs away with a 1/10 chance of taking 1 - 4 damage from the monster POSTCONDITION player runs away with a chance at taking 1 - 4 damage """ # rolls die for 1/10 chance of monster hitting player if sud.roll_die(1, 10) == 1: # monster rolls 1d4 for damage and lowers player hp by that amount monster_damage = sud.roll_die(1, 4) character.set_hitpoints(character.get_hitpoints() - monster_damage) print('The monster hits you for', monster_damage, 'Hitpoints as you run away') # if die is > 1 then user runs away safely else: print('You run away safely')
def check_monster_encounter() -> bool: """rolls a die with a 1/10 chance of encountering a monster POSTCONDITION either plays a monster encounter or not depending on roll RETURN true if monster encountered false if not """ # rolls a die for 1/10 chance of monster encounter if sud.roll_die(1, 10) == 1: monster_encounter() return True return False
def monster_fight(): """Plays a monster fight to the death POSTCONDITION either the player or the monster dies """ monster_hp = 5 # keeps fighting while both monster and player are alive while monster_hp > 0 and character.get_hitpoints() > 0: # calculates monster and player damage player_damage = sud.roll_die(1, 6) monster_damage = sud.roll_die(1, 6) # player attacks monster monster_hp -= player_damage print('You hit the monster for', player_damage, 'Damage') # if monster dies prints a message if monster_hp <= 0: print('The monster dies') # if monster didnt die it attacks the player else: character.set_hitpoints(character.get_hitpoints() - monster_damage) print('The monster hits you for', monster_damage, 'Damage')
def test_lower_bound(self): for i in range(100): self.assertTrue(roll_die(3, 6) >= 3)
def test_upper_bound(self): for i in range(100): self.assertTrue(roll_die(3, 6) <= 18)
def test_0_sides(self): self.assertEqual(roll_die(5, 0), 0)
def test_0_rolls(self): self.assertEqual(roll_die(0, 5), 0)
def test_roll_10_0(self): number_of_rolls = 10 number_of_sides = 0 expected = 0 self.assertEqual(expected, roll_die(number_of_rolls, number_of_sides))
def test_roll_true(self): number_of_rolls = 10 number_of_sides = 10 res = roll_die(number_of_rolls, number_of_sides) self.assertTrue(number_of_rolls <= res <= number_of_rolls * number_of_sides)