def test_hero_validation_raises_typeerror_if_title_not_str(self): name = 'Bron' title = ['Dragonslayer'] mana_regeneration_rate = 2 with self.assertRaisesRegex(TypeError, 'Title must be of "str" type.'): Hero.validate_input_hero(name, title, mana_regeneration_rate)
def test_hero_validation_raises_typeerror_if_mana_regen_rate_not_int(self): name = 'Bron' title = 'Dragonslayer' mana_regeneration_rate = 'p' with self.assertRaisesRegex( TypeError, 'Mana regeneration rate must be of "int" type.'): Hero.validate_input_hero(name, title, mana_regeneration_rate)
def test_hero_known_as_represents_hero_as_expected(self): test_obj = Hero(name="Bron", title="Dragonslayer", health=100, mana=100, mana_regeneration_rate=2) self.assertEqual(test_obj.known_as(), 'Bron the Dragonslayer')
def test_hero_validation_raises_exception_if_mana_regen_rate_negative( self): name = 'Bron' title = 'Dragonslayer' mana_regeneration_rate = -5 with self.assertRaisesRegex( ValueError, 'Mana regeneration rate cannot be negative.'): Hero.validate_input_hero(name, title, mana_regeneration_rate)
def test_when_unrecognise_means_then_raises_value_error(self): test_obj = Hero(name="Bron", title="Dragonslayer", health=100, mana=100, mana_regeneration_rate=2) with self.assertRaisesRegex(ValueError, 'Unrecognized means of attack.'): test_obj.attack(by='testing')
def test_when_hero_has_no_weapons_then_return_none(self): test_obj = Hero(name="Bron", title="Dragonslayer", health=100, mana=100, mana_regeneration_rate=2) self.assertEqual(test_obj.attack(by='weapon'), None) self.assertEqual(test_obj.attack(by='magic'), None) self.assertEqual(test_obj.attack(), None)
def test_hero_take_mana_method_gives_mana_equal_to_mana_points(self): test_obj = Hero(name="Bron", title="Dragonslayer", health=100, mana=100, mana_regeneration_rate=2) setattr(test_obj, 'mana', 25) test_obj.take_mana(50) self.assertEqual(getattr(test_obj, 'mana'), 75)
def test_when_hero_wants_to_attack_with_magic_and_has_it_but_has_no_mana_then_return_none( self): hero = Hero(name="Bron", title="Dragonslayer", health=100, mana=100, mana_regeneration_rate=2) spell = Spell(name="Fireball", damage=30, mana_cost=150, cast_range=2) spell.equip_to(hero) self.assertEqual(hero.attack(by='magic'), None)
def test_when_hero_wants_to_attack_with_weapon_and_has_it_then_return_weapon( self): hero = Hero(name="Bron", title="Dragonslayer", health=100, mana=100, mana_regeneration_rate=2) weapon = Weapon(name="The Axe of Destiny", damage=20) weapon.equip_to(hero) self.assertEqual(hero.attack(by='weapon'), weapon)
def test_when_hero_wants_to_attack_with_magic_and_has_and_can_cast_spell_then_return_spell( self): hero = Hero(name="Bron", title="Dragonslayer", health=100, mana=100, mana_regeneration_rate=2) spell = Spell(name="Fireball", damage=30, mana_cost=50, cast_range=2) spell.equip_to(hero) self.assertEqual(hero.attack(by='magic'), spell) self.assertEqual(getattr(hero, 'mana'), 50)
def test_hero_take_mana_method_cannot_give_more_mana_than_max(self): test_obj = Hero(name="Bron", title="Dragonslayer", health=100, mana=100, mana_regeneration_rate=2) max_mana = 100 mana_points = 200 setattr(test_obj, 'mana', 25) test_obj.take_mana(mana_points) self.assertEqual(getattr(test_obj, 'mana'), max_mana)
def test_when_hero_attacks_and_enemy_has_no_armor(self): dungeon = Dungeon('level1.txt') hero = Hero(name="Bron", title="Dragonslayer", health=100, mana=100, mana_regeneration_rate=2) hero.equip(Weapon('Axe', 20)) enemy = Enemy(50, 50, 20) expected_damage_taken = 20 result_damage_taken = dungeon.attack(hero, enemy)[0] self.assertEqual(result_damage_taken, expected_damage_taken)
def test_dungeon_spawn_raises_exception_if_hero_already_in_dungeon(self): test_hero1 = Hero(name="Bron", title="Dragonslayer", health=100, mana=100, mana_regeneration_rate=2) test_hero2 = Hero(name="Jon", title="King in the North", health=100, mana=100, mana_regeneration_rate=2) test_filename = 'level1.txt' test_obj = Dungeon(test_filename) test_obj.spawn(test_hero1) with self.assertRaisesRegex( AssertionError, 'Cannot have more than 1 hero in the dungeon.'): test_obj.spawn(test_hero2)
def test_hero_init_initializes_object_as_expected(self): test_obj = Hero(name="Bron", title="Dragonslayer", health=100, mana=100, mana_regeneration_rate=2) self.assertEqual(getattr(test_obj, 'health'), 100) self.assertEqual(getattr(test_obj, 'max_health'), 100) self.assertEqual(getattr(test_obj, 'mana'), 100) self.assertEqual(getattr(test_obj, 'max_mana'), 100) self.assertEqual(getattr(test_obj, 'name'), 'Bron') self.assertEqual(getattr(test_obj, 'title'), 'Dragonslayer') self.assertEqual(getattr(test_obj, 'mana_regeneration_rate'), 2)
def test_when_hero_has_no_weapon_or_spell(self): dungeon = Dungeon('level1.txt') hero = Hero(name="Bron", title="Dragonslayer", health=100, mana=100, mana_regeneration_rate=2) dungeon.spawn(hero) enemy = Enemy(50, 50, 20) result = dungeon._fight(enemy) self.assertTrue( 'He doesn`t stand a chance against the enemy.' in result)
def test_drink_mana_potion(self): hero = Hero(name="Bron", title="Dragonslayer", health=100, mana=100, mana_regeneration_rate=2) setattr(hero, 'mana', 20) # restore mana potion = Potion('mana', 20) potion.equip_to(hero) self.assertEqual(getattr(hero, 'mana'), 40)
def test_check_for_enemy_when_no_enemy_in_range_then_return_invalid_enemy_position( self): dungeon = Dungeon('level1.txt') hero = Hero(name="Bron", title="Dragonslayer", health=100, mana=100, mana_regeneration_rate=2) dungeon.spawn(hero) expected_enemy_position = {'x': -1, 'y': -1} # dungeon_map = [['H', '.', '#', '#', '.', '.', '.', '.', '.', 'T'], # ['#', 'T', '#', '#', '.', '.', '#', '#', '#', '.'], # ['#', '.', '#', '#', '#', 'E', '#', '#', '#', 'E'], # ['#', '.', 'E', '.', '.', '.', '#', '#', '#', '.'], # ['#', '#', '#', 'T', '#', '#', '#', '#', '#', 'G']] self.assertEqual(dungeon.check_for_enemy(2), expected_enemy_position)
def test_dungeon_move_hero_to_position_works_as_expected(self): test_filename = 'level1.txt' test_obj = Dungeon(test_filename) test_hero1 = Hero(name="Bron", title="Dragonslayer", health=100, mana=100, mana_regeneration_rate=2) test_obj.spawn(test_hero1) expected_result = [['.', 'H', '#', '#', '.', '.', '.', '.', '.', 'T'], ['#', 'T', '#', '#', '.', '.', '#', '#', '#', '.'], ['#', '.', '#', '#', '#', 'E', '#', '#', '#', 'E'], ['#', '.', 'E', '.', '.', '.', '#', '#', '#', '.'], ['#', '#', '#', 'T', '#', '#', '#', '#', '#', 'G']] test_obj._Dungeon__move_hero_to_position(0, 1, '.') self.assertEqual(getattr(test_obj, 'map'), expected_result)
def test_dungeon_spawn_places_hero_on_the_map_and_initializes_hero_variable_with_argument( self): test_hero1 = Hero(name="Bron", title="Dragonslayer", health=100, mana=100, mana_regeneration_rate=2) test_filename = 'level1.txt' test_obj = Dungeon(test_filename) expected_result = [['H', '.', '#', '#', '.', '.', '.', '.', '.', 'T'], ['#', 'T', '#', '#', '.', '.', '#', '#', '#', '.'], ['#', '.', '#', '#', '#', 'E', '#', '#', '#', 'E'], ['#', '.', 'E', '.', '.', '.', '#', '#', '#', '.'], ['#', '#', '#', 'T', '#', '#', '#', '#', '#', 'G']] test_obj.spawn(test_hero1) self.assertEqual(getattr(test_obj, 'hero'), test_hero1) self.assertEqual(getattr(test_obj, 'map'), expected_result)
def test_check_for_enemy_when_enemy_in_range_1_then_return_enemy_position( self): dungeon = Dungeon('level1.txt') hero = Hero(name="Bron", title="Dragonslayer", health=100, mana=100, mana_regeneration_rate=2) dungeon.spawn(hero) dungeon.move_hero('right') dungeon.move_hero('down') dungeon.move_hero('down') dungeon.move_hero('down') expected_enemy_position = {'x': 3, 'y': 2} # dungeon_map = [['.', '.', '#', '#', '.', '.', '.', '.', '.', 'T'], # ['#', '.', '#', '#', '.', '.', '#', '#', '#', '.'], # ['#', '.', '#', '#', '#', 'E', '#', '#', '#', 'E'], # ['#', 'H', 'E', '.', '.', '.', '#', '#', '#', '.'], # ['#', '#', '#', 'T', '#', '#', '#', '#', '#', 'G']] self.assertEqual(dungeon.check_for_enemy(1), expected_enemy_position)
def test_when_hero_has_same_attack_points_as_enemy_armor_points(self): dungeon = Dungeon('level1.txt') hero = Hero(name="Bron", title="Dragonslayer", health=100, mana=100, mana_regeneration_rate=2) dungeon.spawn(hero) hero.equip(Weapon('Axe', 20)) hero.equip(Armor('Shield', 20)) enemy = Enemy(50, 50, 20) enemy.equip(Armor('Vest', 20)) result = dungeon._fight(enemy) self.assertTrue('Hero got tired and let his guard down.' in result)
def start_game(): result = False print(fg.orange, fg.bold, "\n Welcome to Dungeons and Pythons ", '\033[0m') print(fg.orange, " If you want to play, create your hero now: \n", fg.end_color) n = input("Give him/her a name:") t = input("And a title:") hero = Hero(str(n), str(t), 100, 100, 2) print(f"\n ~~~~~~~~~~Your hero is known as {fg.lightred} {fg.bold} {str(hero.known_as())} {fg.end_color} ~~~~~~~~~~~~~~~") print(f" ~~~~~~~~~~You have: {fg.blue} {fg.bold} health - {hero.get_health()}, mana - {hero.get_mana()} {fg.end_color} ~~~~~~~~~~~~~~~\n") s = spell_four hero.learn(s) w = weapon_three hero.equip(w) map = Dungeon("level1.txt") map.spawn(hero) print(f"{fg.yellow}{fg.bold}This is your map for the game:\n {fg.end_color}") map.print_map() print(f"\n'H' - your hero") print(f"'E' - enemy") print(f"'.' - you can move there") print(f"'#' - you can't go there \n\n ") while result is not True: print(fg.bold, fg.yellow, "Current map: ", fg.end_color) map.print_map() print(f"{fg.yellow}Choose one of the following: {fg.end_color}") print(f"{fg.yellow}Move your hero: 'm'\n {fg.end_color}") print(f"{fg.yellow}Stats of your hero: 's'\n {fg.end_color}") print(f"{fg.yellow}Attack with your hero: 'a'\n {fg.end_color}") choice = input() if choice == 'm': t = direction_checker() result = map.move_hero(str(t)) elif choice == 's' or choice == 'a': result = choice_checker(map, choice) else: print('Invalid choice!')
def demo(): h = Hero(name="Bron", title="Dragonslayer", health=100, mana=100, mana_regeneration_rate=2) w = Weapon(name="The Axe of Destiny", damage=20) h.equip(w) s = Spell(name="Fireball", damage=30, mana_cost=50, cast_range=2) h.equip(s) map = Dungeon("level1.txt") map.spawn(h) map.move_hero("right") map.move_hero("down") map.hero_attack(by="magic") map.move_hero("down") map.move_hero("down") map.move_hero("right") map.move_hero("right") map.move_hero("right") map.move_hero("right") map.move_hero("up") map.move_hero("up") map.move_hero("up") map.move_hero("right") map.move_hero("right") map.move_hero("right") map.move_hero("right") map.move_hero("down") map.move_hero("down") map.move_hero("down") map.move_hero("down")