Ejemplo n.º 1
0
    def test_reset_bot_bot_positions(self):

        test_reset_bot = (""" ########
                #0     #
                #2    3#
                #     1#
                ######## """)
        number_bots = 4
        universe = CTFUniverse.create(test_reset_bot, number_bots)
        assert str(universe) == \
                str(Layout(test_reset_bot, layout_chars, number_bots).as_mesh())
        assert universe.bot_positions == \
                [(1, 1), (6, 3), (1, 2), (6, 2)]
        test_shuffle = (""" ########
                #   0 3#
                # 1    #
                # 2    #
                ######## """)
        universe.bots[0].current_pos = (4, 1)
        universe.bots[1].current_pos = (2, 2)
        universe.bots[2].current_pos = (2, 3)
        universe.bots[3].current_pos = (6, 1)
        assert universe.bot_positions == \
                [(4, 1), (2, 2), (2, 3), (6, 1)]
        assert str(universe) == \
                str(Layout(test_shuffle, layout_chars, number_bots).as_mesh())
        universe.bots[0]._to_initial()
        universe.bots[1]._to_initial()
        universe.bots[2]._to_initial()
        universe.bots[3]._to_initial()
        assert str(universe) == \
                str(Layout(test_reset_bot, layout_chars, number_bots).as_mesh())
        assert universe.bot_positions == \
                [(1, 1), (6, 3), (1, 2), (6, 2)]
Ejemplo n.º 2
0
    def test_get_initial_positions(self):
        test_layout = (
            """ #######
                #0    #
                #  1  #
                #    2#
                ####### """)
        number_bots = 3
        layout = Layout(test_layout, layout_chars, number_bots)
        mesh = layout.as_mesh()
        initial_pos = extract_initial_positions(mesh, number_bots)
        target = [(1, 1), (3, 2), (5, 3)]
        assert target == initial_pos
        # also test the side-effect of initial_positions()
        target = Mesh(7, 5, data =list('########     ##     ##     ########'))
        assert target == mesh

        # now for a somewhat more realistic example
        test_layout2 = (
            """ ##################
                #0#      #       #
                #1#####    #####2#
                #       #      #3#
                ################## """)
        number_bots = 4
        layout = Layout(test_layout2, layout_chars, number_bots)
        mesh = layout.as_mesh()
        initial_pos = extract_initial_positions(mesh, number_bots)
        target = [(1, 1), (1, 2), (16, 2), (16, 3)]
        assert target == initial_pos
        # also test the side-effect of initial_positions()
        target = Mesh(18, 5, data = list('################### #      #       #'+\
                '# #####    ##### ##       #      # ###################'))
        assert target == mesh
Ejemplo n.º 3
0
def create_CTFUniverse(layout_str, number_bots,
        team_names=None):
    """ Factory to create a 2-Player Capture The Flag Universe.

    Parameters
    ----------
    layout_str : str
        the string encoding the maze layout
    number_bots : int
        the number of bots in the game
    team_names : length 2 list of strings, optional
        default = None -> ['black', 'white']
        the names of the playing teams

    Raises
    ------
    UniverseException
        if the number of bots or layout width are odd
    LayoutEncodingException
        if there is something wrong with the layout_str, see `Layout()`

    """
    if team_names is None:
        team_names = ["black", "white"]

    layout_chars = [cls.char for cls in [Wall, Free, Food]]

    if number_bots % 2 != 0:
        raise UniverseException(
            "Number of bots in CTF must be even, is: %i"
            % number_bots)
    layout = Layout(layout_str, layout_chars, number_bots)
    layout_mesh = layout.as_mesh()
    initial_pos = extract_initial_positions(layout_mesh, number_bots)
    maze = create_maze(layout_mesh)
    if maze.width % 2 != 0:
        raise UniverseException(
            "Width of a layout for CTF must be even, is: %i"
            % maze.width)
    homezones = [(0, maze.width // 2 - 1),
            (maze.width // 2, maze.width - 1)]

    teams = []
    teams.append(Team(0, team_names[0], homezones[0], bots=range(0,
        number_bots, 2)))
    teams.append(Team(1, team_names[1], homezones[1], bots=range(1,
        number_bots, 2)))

    bots = []
    for bot_index in range(number_bots):
        team_index = bot_index % 2
        bot = Bot(bot_index, initial_pos[bot_index],
                team_index, homezones[team_index])
        bots.append(bot)

    return CTFUniverse(maze, teams, bots)
Ejemplo n.º 4
0
    def test_get_initial_positions(self):
        test_layout = (""" #######
                #0    #
                #  1  #
                #    2#
                ####### """)
        number_bots = 3
        layout = Layout(test_layout, layout_chars, number_bots)
        mesh = layout.as_mesh()
        initial_pos = extract_initial_positions(mesh, number_bots)
        target = [(1, 1), (3, 2), (5, 3)]
        assert target == initial_pos
        # also test the side-effect of initial_positions()
        target = Mesh(7, 5, data=list('########     ##     ##     ########'))
        assert target == mesh

        # now for a somewhat more realistic example
        test_layout2 = (""" ##################
                #0#      #       #
                #1#####    #####2#
                #       #      #3#
                ################## """)
        number_bots = 4
        layout = Layout(test_layout2, layout_chars, number_bots)
        mesh = layout.as_mesh()
        initial_pos = extract_initial_positions(mesh, number_bots)
        target = [(1, 1), (1, 2), (16, 2), (16, 3)]
        assert target == initial_pos
        # also test the side-effect of initial_positions()
        target = Mesh(18, 5, data = list('################### #      #       #'+\
                '# #####    ##### ##       #      # ###################'))
        assert target == mesh
Ejemplo n.º 5
0
    def test_reset_bot_bot_positions(self):

        test_reset_bot = (""" ########
                #0     #
                #2    3#
                #     1#
                ######## """)
        number_bots = 4
        universe = CTFUniverse.create(test_reset_bot, number_bots)
        self.assertEqual(
            str(universe),
            str(Layout(test_reset_bot, layout_chars, number_bots).as_mesh()))
        self.assertEqual(universe.bot_positions, [(1, 1), (6, 3), (1, 2),
                                                  (6, 2)])
        test_shuffle = (""" ########
                #   0 3#
                # 1    #
                # 2    #
                ######## """)
        universe.bots[0].current_pos = (4, 1)
        universe.bots[1].current_pos = (2, 2)
        universe.bots[2].current_pos = (2, 3)
        universe.bots[3].current_pos = (6, 1)
        self.assertEqual(universe.bot_positions, [(4, 1), (2, 2), (2, 3),
                                                  (6, 1)])
        self.assertEqual(
            str(universe),
            str(Layout(test_shuffle, layout_chars, number_bots).as_mesh()))
        universe.bots[0]._reset()
        universe.bots[1]._reset()
        universe.bots[2]._reset()
        universe.bots[3]._reset()
        self.assertEqual(
            str(universe),
            str(Layout(test_reset_bot, layout_chars, number_bots).as_mesh()))
        self.assertEqual(universe.bot_positions, [(1, 1), (6, 3), (1, 2),
                                                  (6, 2)])