Beispiel #1
0
    def test_learn_with_given_player_with_spell_and_valid_spell_as_argument_should_change_spell(
            self):
        pl = Player(health=100, mana=60)
        spell = Spell(name='Fireball', damage=30, mana_cost=50, cast_range=2)
        pl.learn(spell=spell)

        new_spell = Spell(name='New spell',
                          damage=30,
                          mana_cost=50,
                          cast_range=2)
        pl.learn(new_spell)

        self.assertEqual(new_spell, pl.spell)
Beispiel #2
0
    def test_with_given_player_and_spell_with_mana_cost_less_than_player_mana_should_lower_players_mana_and_return_True(
            self):
        pl = Player(health=100, mana=60)
        spell = Spell(name='Fireball', damage=30, mana_cost=50, cast_range=2)
        pl.learn(spell)

        result = pl.can_cast()

        self.assertEqual(True, result)
Beispiel #3
0
    def test_with_given_player_with_non_castable_spell_and_spell_as_argument_should_return_zerol(
            self):
        pl = Player(health=100, mana=20)
        spell = Spell(name='Fireball', damage=30, mana_cost=50, cast_range=2)
        pl.learn(spell)
        by = PLAYER_ATTACK_BY_SPELL_STRING

        result = pl.attack(by=by)

        self.assertEqual(0, result)
    def test_with_weapon_and_spell_should_attack_by_higher_damage(self):
        h = Hero(name="Bron",
                 title="Dragonslayer",
                 health=100,
                 mana=100,
                 mana_regeneration_rate=2)
        h.learn(Spell(name="Fireball", damage=20, mana_cost=50, cast_range=2))
        h.equip(Weapon(name="The Axe of Destiny", damage=30))
        e = Enemy()

        Fight(h, e)

        self.assertEqual(h.attacking, PLAYER_ATTACK_BY_WEAPON_STRING)
    def test_init_spell(self):
        name = "Fireball"
        damage = 30
        mana_cost = 50
        cast_range = 50

        s = Spell(name=name, damage=damage,\
         mana_cost=mana_cost, cast_range=cast_range)

        self.assertEqual(s.name, name)
        self.assertEqual(s.damage, damage)
        self.assertEqual(s.mana_cost, mana_cost)
        self.assertEqual(s.cast_range, cast_range)
    def test_with_spell_and_weapon_with_distance_and_no_mana_should_move(self):
        h = Hero(name="Bron",
                 title="Dragonslayer",
                 health=100,
                 mana=100,
                 mana_regeneration_rate=2)
        h.learn(Spell(name="Fireball", damage=30, mana_cost=50, cast_range=2))
        h.equip(Weapon(name="The Axe of Destiny", damage=40))
        e = Enemy()
        h.mana = 0

        f = Fight(h, e, distance=2)

        self.assertEqual(f.distance, 0)
    def test_by_spell_with_no_enemy_in_casting_range(self):
        h = Hero(name="Bron", title="Dragonslayer",
                 health=100, mana=100, mana_regeneration_rate=2)
        h.learn(Spell(name="Fireball", damage=30, mana_cost=50, cast_range=2))
        a = Dungeon.from_string('''..##.....T
.S##..###.
#.###E###E
#.E...###.
###.#####G''')
        a.spawn(h)

        attack = a.hero_attack(by=PLAYER_ATTACK_BY_SPELL_STRING, direction='down')

        self.assertEqual(attack, "Nothing in casting range 2")
    def test_with_unvalid_direction_returns_false(self):
        h = Hero(name="Bron", title="Dragonslayer",
                 health=100, mana=100, mana_regeneration_rate=2)
        h.learn(Spell(name="Fireball", damage=30, mana_cost=50, cast_range=2))
        a = Dungeon.from_string('''..##.....T
..##..###.
#.S.E###E.
#.E...###.
##..#####G''')
        a.spawn(h)

        result = a.enemy_in_casting_range('downleft')

        self.assertFalse(result, "unvalid direction")
    def test_with_down_with_enemy_in_spell_range_returns_true(self):
        h = Hero(name="Bron", title="Dragonslayer",
                 health=100, mana=100, mana_regeneration_rate=2)
        h.learn(Spell(name="Fireball", damage=30, mana_cost=50, cast_range=2))
        a = Dungeon.from_string('''..##.....T
..##..###.
#.S.E###E.
#.E...###.
##..#####G''')
        a.spawn(h)

        result = a.enemy_in_casting_range('down')

        self.assertTrue(result, "cannot attack down")
    def test_with_down_with_enemy_behind_wall_returns_false(self):
        h = Hero(name="Bron", title="Dragonslayer",
                 health=100, mana=100, mana_regeneration_rate=2)
        h.learn(Spell(name="Fireball", damage=30, mana_cost=50, cast_range=2))
        a = Dungeon.from_string('''..##.....T
..S#..###.
#.#.#E###E
#.E...###.
###.#####G''')
        a.spawn(h)

        result = a.enemy_in_casting_range('down')

        self.assertFalse(result, "attempt to attack through the wall")
Beispiel #11
0
def choose_random_spell_from_file():
    try:
        with open('spells.txt', 'r') as f:
            spells_in_file = f.read().splitlines()
            spell_args_string = random.choice(spells_in_file)
            if spell_args_string == 'None':
                return None
            args = spell_args_string.split(',')
            spell = Spell(name=args[0],
                          damage=int(args[1]),
                          mana_cost=int(args[2]),
                          cast_range=int(args[3]))
            return spell
    except Exception as e:
        print(e)
Beispiel #12
0
    def test_with_given_hero_with_non_castable_spell_and_spell_as_argument_should_return_zerol(
            self):
        name = 'Bron'
        title = 'Dragonslayer'
        health = 100
        mana = 20
        mana_regeneration_rate = 2
        h = Hero(name, title, health, mana, mana_regeneration_rate)
        spell = Spell(name='Fireball', damage=30, mana_cost=50, cast_range=2)
        h.learn(spell)
        by = PLAYER_ATTACK_BY_SPELL_STRING

        result = h.attack(by=by)

        self.assertEqual(0, result)
Beispiel #13
0
def choose_random_treasure_from_file(file=None):
    if not file:
        file = "test_treasures.txt"
    try:
        with open(file, 'r') as f:
            treasures_in_file = f.read().splitlines()
            treasure_args_string = random.choice(treasures_in_file)
            if treasure_args_string == 'None':
                return None
            args = treasure_args_string.split(',')
            if args[0] == 'Spell':
                treasure = Spell(name=args[1],
                                 damage=int(args[2]),
                                 mana_cost=int(args[3]),
                                 cast_range=int(args[4]))
            elif args[0] == 'Weapon':
                treasure = Weapon(name=args[1], damage=int(args[2]))
            elif args[0] == 'HealthPotion':
                treasure = HealthPotion(name=args[1], healing=args[2])
            elif args[0] == 'ManaPotion':
                treasure = ManaPotion(name=args[1], mana_regen=args[2])
            return treasure
    except Exception as e:
        print(e)
Beispiel #14
0
        else:
            pass  # enemy only moves

    def move_hero(self):
        if self.hero.weapon:
            self.distance -= 1
            self.happened += 'Hero moves one square {d} in\
 order to get to the enemy. This is his move.\n'.format(d=self.direction)
        else:
            self.happened += 'Hero has nothing to do.\n'

    def move_enemy(self):
        self.distance -= 1
        self.happened += 'Enemy moves one square {d} in\
 order to get to the hero. This is his move.\n'.format(
            d=self.opposite_direction)


if __name__ == '__main__':
    h = Hero(name="Bron",
             title="Dragonslayer",
             health=100,
             mana=100,
             mana_regeneration_rate=2)
    h.learn(Spell(name="Fireball", damage=30, mana_cost=50, cast_range=2))
    # h.equip(Weapon(name="The Axe of Destiny", damage=20))
    e = Enemy()

    # print(Fight(h, e))
    print(Fight(h, e, distance=2, direction='down'))