예제 #1
0
    def test_init(self):
        heroes = [Hero.get_hero_by_id(self.all_heroes, i) for i in range(1, 4)]
        draft1 = Draft(heroes)
        self.assertEqual(3, draft1.get_heroes_num())

        heroes = [Hero.get_hero_by_id(self.all_heroes, i) for i in range(1, 7)]
        with self.assertRaises(OverflowError):
            draft2 = Draft(heroes)
예제 #2
0
 def setUp(self):
     self.hero = Hero(health=100, mana=50,
                      name="Yamamoto", title="Samurai", mana_regeneration_rate=2)
     self.enemy = Enemy(health=100, mana=100, damage=20)
     self.weapon = Weapon.generate(self.hero)
     self.spell = Spell(name="Fireball", damage=30,
                        mana_cost=50, cast_range=2)
     self.hero.learn(self.spell)
     self.hero.equip(self.weapon)
예제 #3
0
 def test_get_hero_by_name(self):
     # Test to get hero 'antimage' (id 1)
     hero1 = Hero.get_hero_by_name(self.heroes, 'antimage')
     self.assertEqual('antimage', hero1.name)
     self.assertEqual(1, hero1.id)
     self.assertEqual('Anti-Mage', hero1.localized_name)
     # Test to get a hero whose name is not in heores
     hero2 = Hero.get_hero_by_name(self.heroes, 'Not a hero name')
     self.assertIsNone(hero2)
     # Test to get hero 'arc_warden' (id 113)
     hero3 = Hero.get_hero_by_name(self.heroes, 'arc_warden')
     self.assertEqual('arc_warden', hero3.name)
     self.assertEqual(113, hero3.id)
     self.assertEqual('Arc Warden', hero3.localized_name)
예제 #4
0
 def setUp(self):
     self.hero = Hero(name='Ivan',
                      title='Dr',
                      health=150,
                      mana=80,
                      mana_regeneration_rate=2)
     self.enemy = Enemy(health=150, mana=50, damage=20)
예제 #5
0
 def setUp(self):
     self.hero = Hero(health=100,
                      mana=50,
                      name="Yamamoto",
                      title="Samurai",
                      mana_regeneration_rate=2)
     self.enemy = Enemy(health=100, mana=100, damage=20)
예제 #6
0
 def setUp(self):
     self.test_hero = Hero(name='Ivan',
                           title='Dr',
                           health=100,
                           mana=50,
                           mana_regeneration_rate=2)
     self.test_weapon = Weapon()
     self.test_spell = Spell(mana_cost=20)
예제 #7
0
class HeroTests(unittest.TestCase):
    def setUp(self):
        self.dummy = Hero(name="Bron",
                          title="Dragonslayer",
                          health=100,
                          mana=100,
                          mana_regeneration_rate=2)

    def test_known_as_method(self):
        self.assertEqual(self.dummy.known_as(), 'Bron the Dragonslayer')
예제 #8
0
    def test_add_hero(self):
        draft = Draft()
        hero_ids = list()
        i = 0
        while not draft.is_full():
            self.assertEqual(i, draft.get_heroes_num())
            hero_id = random.randint(1, 113)
            if hero_id != 24:
                draft.add_hero(Hero.get_hero_by_id(self.all_heroes, hero_id))
                hero_ids.append(hero_id)
                i += 1

        self.assertEqual(i, draft.get_heroes_num())

        with self.assertRaises(TypeError):
            draft.add_hero(1)

        with self.assertRaises(OverflowError):
            draft.add_hero(Hero(1))
예제 #9
0
class TestFight(unittest.TestCase):
    def setUp(self):
        self.hero = Hero(health=100, mana=50,
                         name="Yamamoto", title="Samurai", mana_regeneration_rate=2)
        self.enemy = Enemy(health=100, mana=100, damage=20)
        self.weapon = Weapon.generate(self.hero)
        self.spell = Spell(name="Fireball", damage=30,
                           mana_cost=50, cast_range=2)
        self.hero.learn(self.spell)
        self.hero.equip(self.weapon)

    def test_simple_fight_in_one_cell(self):
        fight = Fight(self.hero, self.enemy)
        self.assertIs(fight.start_fight(), self.hero)

    def test_sipmle_fight_starts_with_weapon(self):
        fight = Fight(self.hero, self.enemy, "spell")
        self.assertIs(fight.start_fight(), self.hero)
        print(self.hero.__dict__)
예제 #10
0
    def test_get_hero_by_id(self):
        # Test to get hero 'antimage' (id 1)
        hero1 = Hero.get_hero_by_id(self.heroes, 1)
        self.assertEqual('antimage', hero1.name)
        self.assertEqual(1, hero1.id)
        self.assertEqual('Anti-Mage', hero1.localized_name)
        # Test to get hero 'arc_warden' (id 113)
        hero2 = Hero.get_hero_by_id(self.heroes, 113)
        self.assertEqual('arc_warden', hero2.name)
        self.assertEqual(113, hero2.id)
        self.assertEqual('Arc Warden', hero2.localized_name)
        # Test to get a hero whose id is 24 (which is missing!)
        with self.assertRaises(AssertionError):
            Hero.get_hero_by_id(self.heroes, 24)
        # Test to get heroes whose id are out of range
        with self.assertRaises(AssertionError):
            Hero.get_hero_by_id(self.heroes, 114)

        with self.assertRaises(AssertionError):
            Hero.get_hero_by_id(self.heroes, 0)
예제 #11
0
def main():
    screen = curses.initscr()
    curses.noecho()
    curses.cbreak()
    m = Dungeon()
    m.spawn(
        Hero(name='Ivan',
             title='Dr',
             health=100,
             mana=80,
             mana_regeneration_rate=2))
    while True:
        key = screen.getch()
        if key == ord('w'):
            m.move_hero(direction='up')
        elif key == ord('s'):
            m.move_hero(direction='down')
        elif key == ord('d'):
            m.move_hero(direction='right')
        elif key == ord('a'):
            m.move_hero(direction='left')
        m.print_map()
예제 #12
0
 def test_rect_x_position(self):
   hero = Hero()
   assert hero.rect.x == 240
예제 #13
0
 def test_rect_y_position(self):
   hero = Hero()
   assert hero.rect.y == 176
예제 #14
0
def let_them_fight(counter):
    dir_name_A = "D:\\projekty\\warIO\\src\\population\\object" + str(counter)
    dir_name_B = "D:\\projekty\\warIO\\src\\population\\object" + str(counter +
                                                                      1)
    if not os.path.exists(dir_name_A):
        os.mkdir(dir_name_A)
    if not os.path.exists(dir_name_B):
        os.mkdir(dir_name_B)

    hero1_x = random.randrange(0, GAME_WIDTH)
    hero1_y = random.randrange(0, GAME_HEIGHT)
    hero1 = Hero(hero1_x, hero1_y, (0, 0, 255))
    hero2_x = random.randrange(0, GAME_WIDTH)
    hero2_y = random.randrange(0, GAME_HEIGHT)
    hero2 = Hero(hero2_x, hero2_y, (0, 255, 0))

    heroes = [hero1, hero2]
    running = True

    ai_A = DFF_Neural_Network()
    ai_A.create_network()
    ai_B = DFF_Neural_Network()
    ai_B.create_network()

    file_weights_A = open(dir_name_A + "\\weights.txt", "w+")
    file_biases_A = open(dir_name_A + "\\biases.txt", "w+")
    replay_A = open(dir_name_A + "\\replay.txt", "w+")
    file_weights_B = open(dir_name_B + "\\weights.txt", "w+")
    file_biases_B = open(dir_name_B + "\\biases.txt", "w+")
    replay_B = open(dir_name_B + "\\replay.txt", "w+")

    ai_A.draw_weights(file_weights_A)
    ai_A.draw_biases(file_biases_A)
    file_weights_A.close()
    file_biases_A.close()
    ai_B.draw_weights(file_weights_B)
    ai_B.draw_biases(file_biases_B)
    file_weights_B.close()
    file_biases_B.close()

    ai_A.get_weights(dir_name_A + "\\weights.txt")
    ai_A.get_biases(dir_name_A + "\\biases.txt")
    ai_B.get_weights(dir_name_B + "\\weights.txt")
    ai_B.get_biases(dir_name_B + "\\biases.txt")

    write_start_to_replay(replay_A, hero1, hero2)
    write_start_to_replay(replay_B, hero1, hero2)
    frames_counter = 0
    while running:
        replay_string = ["_", "_", "_", "_", "_", "_"]

        bullets_position = []
        for i in range(3):
            if hero2.bullets[i] == None:
                bullets_position.append([0, 0])
            else:
                bullets_position.append(
                    [hero2.bullets[i].position_x, hero2.bullets[i].position_y])

        inputs_a = return_inputs(hero1, hero2)
        ai_A_move = ai_A.calculate_output(inputs_a)

        inputs_b = return_inputs(hero2, hero1)
        ai_B_move = ai_B.calculate_output(inputs_b)

        do_ai_moves(ai_A_move[-1], replay_string, hero1, 0)
        do_ai_moves(ai_B_move[-1], replay_string, hero2, 1)

        execute_collision(heroes, hero1, hero2)

        replay_A.write(''.join(replay_string) + "\n")
        replay_B.write(''.join(replay_string) + "\n")

        for hero in heroes:
            hero.movement()
            if hero.health <= 0:
                running = False
            if frames_counter >= NUMBER_OF_FRAMES:
                running = False

        frames_counter += 1

    fitness_A = fitness_function(hero1, hero2)
    fitness_B = fitness_function(hero2, hero1)
    return [fitness_A, fitness_B]
예제 #15
0
 def setUp(self):
     self.dummy = Hero(name="Bron",
                       title="Dragonslayer",
                       health=100,
                       mana=100,
                       mana_regeneration_rate=2)
예제 #16
0
 def setUp(self):
     self.heroes_json_file_path = '../data/heroes.json'
     self.heroes = Hero.load_heroes_data(self.heroes_json_file_path)
예제 #17
0
        if map_tile == EXIT:
            print('found exit gj')
            self.set_hero_position(x, y)
            return True

    def move_hero(self, direction):
        if not self.hero.is_alive():
            raise Exception('Hero is dead! You need a new Hero!')
        if direction in self.__directions:
            x, y = self.__directions[direction]
            cur_x, cur_y = self.hero_pos
            new_x = cur_x + x
            new_y = cur_y + y
            if self.check_next_map_tile(new_x, new_y):
                self.layout[cur_x][cur_y] = WALKABLE_PATH
            else:
                return False
        self.hero.take_mana(self.hero.mana_regeneration_rate)
        return True


m = Dungeon('small')
if __name__ == '__main__':
    m.print_map()
    print(m.spawn_point)
    m.spawn(
        Hero(name='Ivan',
             title='dr',
             health=150,
             mana=200,
             mana_regeneration_rate=2))
예제 #18
0
'''this is going to be a manual test'''

from src.hero import Hero
from src.enemy import Enemy
from src.spell import Spell
from src.weapon import Weapon
from src.fight import Fight
from src.fight_status_bar import FightStatusBar

h = Hero(name="Genadi", title="Gopnik")

s = Spell(name="Kwass Molotov", damage=5, manaCost=10, castRange=7)
s1 = Spell(name="Magic spit", damage=10, manaCost=25, castRange=2)

w = Weapon(name="Beer Bottle", damage=25)
w1 = Weapon(name="Bat", damage=22)

h.equip(w)
h.learn(s)
h.coordinates = (0, 7)

e = Enemy()

e.learn(s1)
e.equip(w1)
e.coordinates = (0, 0)
"""
f = Fight(h, e,)
f.initialize_fight() """
 def setUp(self):
     self.dummy = Dungeon()
     self.hero = Hero(name='Fuggboi', title='Wise')
class DungeonTests(unittest.TestCase):
    def setUp(self):
        self.dummy = Dungeon()
        self.hero = Hero(name='Fuggboi', title='Wise')

    def test_spawn_method(self):
        self.dummy.open_map(fileDir=plainMapDir)
        self.dummy.spawn(hero=self.hero)
        # self.dummy.print_map()
        self.assertEqual(
            self.hero.get_coords(),
            (1, 1))

    def test_move_method(self):

        with self.subTest('Test moving up.'):
            self.dummy.open_map(fileDir=plainMapDir)
            self.dummy.spawn(hero=self.hero)
            self.dummy.move_hero(direction='up')
            self.assertEqual(
                self.hero.get_coords(),
                (1, 0)
            )

        with self.subTest('Test moving down.'):
            self.dummy.open_map(fileDir=plainMapDir)
            self.dummy.spawn(hero=self.hero)
            self.dummy.move_hero(direction='down')
            self.assertEqual(
                self.hero.get_coords(),
                (1, 2)
            )

        with self.subTest('Test moving right.'):
            self.dummy.open_map(fileDir=plainMapDir)
            self.dummy.spawn(hero=self.hero)
            self.dummy.move_hero(direction='right')

            self.assertEqual(
                self.hero.get_coords(),
                (2, 1)
            )

        with self.subTest('Test moving left.'):
            self.dummy.open_map(fileDir=plainMapDir)
            self.dummy.spawn(hero=self.hero)
            self.dummy.move_hero(direction='left')

            self.assertEqual(
                self.hero.get_coords(),
                (0, 1)
            )

        with self.subTest('Test moving into treasure chest.'):
            self.dummy.open_map(fileDir=treasureMapDir)
            self.dummy.spawn(hero=self.hero)

        with self.subTest('Test moving into enemy.'):
            self.dummy.open_map(fileDir=enemyMapDir)
            self.dummy.spawn(hero=self.hero)
            self.dummy.move_hero(direction='right')

        with self.subTest('Test shoot_blindly method.'):
            self.hero.heal_to_full()
            self.dummy.open_map(fileDir=enemyMapDir)
            s1 = Spell(name="Magic spit", damage=30, manaCost=25, castRange=2)
            self.hero.learn(s1)
            self.dummy.spawn(hero=self.hero)
            self.dummy.shoot_blindly()
            self.dummy.print_map()

        with self.subTest('Test moving into gateway.'):
            self.dummy.open_map(fileDir=validMapDir)
            self.dummy.spawn(hero=self.hero)
            self.assertEqual(self.dummy.move_hero(direction='right'), True)

        with self.subTest('Test moving into gateway work when dirs have larger indexes.'):
            temp = 'dungeon_maps/level_99/'
            self.dummy.open_map(fileDir=temp)
            self.dummy.spawn(hero=self.hero)
            self.assertEqual(
                type(self.dummy.move_hero(direction='right')), str)
예제 #21
0
def let_them_fight_population(counter1, counter2):
    dir_name_A = "D:\\projekty\\warIO\\src\\population\\object" + str(counter1)
    if len(str(counter2)) <= 4:
        dir_name_B = "D:\\projekty\\warIO\\src\\population\\object" + str(
            counter2)
    else:
        dir_name_B = counter2
    if not os.path.exists(dir_name_A):
        os.mkdir(dir_name_A)
    if not os.path.exists(dir_name_B):
        os.mkdir(dir_name_B)

    hero1_x = random.randrange(0, GAME_WIDTH)
    hero1_y = random.randrange(0, GAME_HEIGHT)
    hero1 = Hero(hero1_x, hero1_y, (0, 0, 255))
    hero2_x = random.randrange(0, GAME_WIDTH)
    hero2_y = random.randrange(0, GAME_HEIGHT)
    hero2 = Hero(hero2_x, hero2_y, (0, 255, 0))

    heroes = [hero1, hero2]
    running = True

    ai_A = DFF_Neural_Network()
    ai_A.create_network()
    ai_B = DFF_Neural_Network()
    ai_B.create_network()

    treasure = None
    learning_flag = True

    replay_A = open(dir_name_A + "\\replay.txt", "w+")
    replay_B = open(dir_name_B + "\\replay.txt", "w+")

    ai_A.get_weights(dir_name_A + "\\weights.txt")
    ai_A.get_biases(dir_name_A + "\\biases.txt")
    ai_B.get_weights(dir_name_B + "\\weights.txt")
    ai_B.get_biases(dir_name_B + "\\biases.txt")

    write_start_to_replay(replay_A, hero1, hero2)
    write_start_to_replay(replay_B, hero1, hero2)
    frames_counter = 0
    while running:

        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_s:
                    print("stopped")
                    learning_flag = False

        replay_string = ["_", "_", "_", "_", "_", "_", "_", "_"]

        ai_A_move = ai_A.calculate_output(return_inputs(
            hero1, hero2, treasure))
        ai_B_move = ai_B.calculate_output(return_inputs(
            hero2, hero1, treasure))

        do_ai_moves(ai_A_move[-1], replay_string, hero1, 0)
        do_ai_moves(ai_B_move[-1], replay_string, hero2, 1)

        if treasure is None:
            treasure_replay = False
        treasure = try_respawn_treasure(treasure)
        if treasure is not None and treasure_replay is False:
            replay_A.write(str(treasure.position_x) + "\n")
            replay_A.write(str(treasure.position_y) + "\n")
            replay_B.write(str(treasure.position_x) + "\n")
            replay_B.write(str(treasure.position_y) + "\n")
            treasure_replay is True

        execute_collision(heroes, hero1, hero2)
        treasure = treasure_collision([hero1, hero2], treasure)

        replay_A.write(''.join(replay_string) + "\n")
        replay_B.write(''.join(replay_string) + "\n")

        hero1.movement(hero1.speed, hero2)
        if hero1.health <= 0:
            running = False
        hero2.movement(hero2.speed, hero1)
        if hero2.health <= 0:
            running = False
        if frames_counter >= NUMBER_OF_FRAMES:
            running = False

        frames_counter += 1

    fitness_A = fitness_function(hero1, hero2)
    fitness_B = fitness_function(hero2, hero1)
    return [fitness_A, fitness_B, learning_flag]
예제 #22
0
 def test_is_match(self):
     self.assertTrue(Hero.is_match(self.heroes, 1, 'antimage'))
     self.assertFalse(Hero.is_match(self.heroes, 2, 'antimage'))
     with self.assertRaises(AssertionError):
         Hero.is_match(self.heroes, 0, 'Non-existed hero')
예제 #23
0
from src.map import Map
from src.hero import Hero


def main():
    lvl = Map.load('./assets/maps/level1.txt')
    lvl.print_map()


if __name__ == '__main__':
    main()

lvl = Map.load('./assets/maps/level1.txt')
lvl.print_map()
h = Hero(name='foo',
         title='bar',
         health=100,
         mana=100,
         mana_regeneration_rate=2)
lvl.spawn(h)