def test_spawn_agent(self):
     self.game_state.kill_agent(self.game_state.get_agent_of_player(2))
     self.game_state.spawn_agent(Point(1, 1), 2)
     self.assertEqual(
         self.game_state.get_agent_of_player(2).get_position(), Point(1, 1))
     self.assertEqual(
         self.game_state.get_field(Point(1, 1)).get_agent().get_player(), 2)
 def test_check_is_on_plan(self):
     game_plan = GamePlan(5, 10, {}, 1)
     self.assertRaises(RuntimeError, game_plan.check_is_on_plan, Point(5, 0))
     self.assertRaises(RuntimeError, game_plan.check_is_on_plan, Point(4, 10))
     self.assertRaises(RuntimeError, game_plan.check_is_on_plan, Point(-1, 0))
     self.assertRaises(RuntimeError, game_plan.check_is_on_plan, Point(0, -1))
     game_plan.check_is_on_plan(Point(0, 0))
    def test_moving_agent(self):
        agent = self.game_state.get_field(Point(2, 2)).get_agent()
        self.game_state.move_agent(agent, Point(3, 2))

        self.assertEqual(agent.get_position(), Point(3, 2))
        self.assertIsNone(self.game_state.get_field(Point(2, 2)).get_agent())
        self.assertEquals(
            self.game_state.get_field(Point(3, 2)).get_agent(), agent)
    def test_field_with_agent(self):
        field = self.game_state.get_field(Point(3, 0))

        self.assertEquals(field.get_agent().get_player(), 1)
        self.assertEquals(field.get_agent().get_position(), Point(3, 0))
        self.assertEquals(field.get_agent().get_last_action(), None)

        self.assertIsNotNone(field.get_base())
        self.assertEqual(field.get_resource_count(), 0)
    def test_point_transformation(self):
        source = Point(3, 7)

        self.assertEquals(Actions.UP.from_point(source), Point(3, 6))
        self.assertEquals(Actions.DOWN.from_point(source), Point(3, 8))
        self.assertEquals(Actions.LEFT.from_point(source), Point(2, 7))
        self.assertEquals(Actions.RIGHT.from_point(source), Point(4, 7))
        self.assertEquals(Actions.HOLD.from_point(source), source)
        self.assertEquals(Actions.PLANT_BOMB.from_point(source), source)
    def test_iterator(self):
        game_plan = GamePlan(3, 4, {}, 1)

        positions = set()
        for position in game_plan:
            positions.add(position)

        self.assertEquals(len(positions), 12)
        self.assertTrue(Point(0, 0) in positions)
        self.assertTrue(Point(2, 3) in positions)
        self.assertTrue(Point(1, 0) in positions)
Ejemplo n.º 7
0
def dto_2_game_state(dto: Any) -> StandardGameState:
    players = create_player_list_from_dto(dto)
    game_plan = create_game_plan_from_dto(dto)

    game_state = StandardGameState(game_plan, players, False,
                                   dto["currentRound"], dto["remainingRounds"])

    for player_id_str, playerDto in dto["players"].items():
        player_id = int(player_id_str)
        agent_location_dto = playerDto["agentLocation"]
        agent_last_move = playerDto[
            "agentLastMove"] if "agentLastMove" in playerDto else None

        if agent_location_dto is not None:
            agent_location = Point(agent_location_dto["x"],
                                   agent_location_dto["y"])
            agent = game_state.spawn_agent(agent_location, player_id)
            agent.last_move = string_2_action(agent_last_move)

        game_state.set_player_resources(player_id, playerDto["resources"])

    for position, field in game_state.all_fields():
        game_state.set_resources_on_field(
            position, dto["fieldResources"][position.x][position.y])

        bomb_dto = dto["fieldBombs"][position.x][position.y]
        if bomb_dto is not None:
            game_state.set_bomb_on_field(
                position, Bomb(bomb_dto["ownerId"], bomb_dto["countdown"]))

    return game_state
Ejemplo n.º 8
0
    def test_creating_from_valid_string(self):
        game_state = string_2_game_state(" A \n" "B  \n")

        self.assertEquals(game_state.game_plan.width, 3)
        self.assertEquals(game_state.game_plan.height, 2)

        self.assertEquals(game_state.get_field(Point(2, 0)).get_agent(), None)
        self.assertEquals(game_state.get_field(Point(1, 1)).get_agent(), None)
        self.assertEquals(
            game_state.get_field(Point(1, 0)).get_agent().get_player(), 1)

        self.assertEquals(len(game_state.get_players()), 2)
        self.assertEquals(
            game_state.get_agent_of_player(1).get_position(), Point(1, 0))
        self.assertEquals(
            game_state.get_agent_of_player(2).get_position(), Point(0, 1))
Ejemplo n.º 9
0
    def test_from_dto(self):
        game_state = dto_2_game_state(json.loads(create_game_state_string(5)))

        self.assertEqual(game_state.current_round, 5)
        self.assertEqual(game_state.rounds_remaining, 94)

        self.assertEqual(
            game_state.get_field(Point(0, 0)).get_resource_count(), 1)
        self.assertEqual(
            game_state.get_field(Point(3, 10)).get_resource_count(), 0)
        self.assertEqual(game_state.get_field(Point(3, 10)).get_base(), 1)
        self.assertEqual(
            game_state.get_field(Point(0, 11)).get_agent().get_player(), 1)

        self.assertEqual(
            game_state.get_agent_of_player(1).get_position(), Point(0, 11))

        bomb_0_0 = game_state.get_field(Point(0, 0)).get_bomb()
        self.assertIsNotNone(bomb_0_0)
        self.assertEqual(bomb_0_0.countdown, 2)
        self.assertEqual(bomb_0_0.owner, 1)
        self.assertIsNone(game_state.get_field(Point(1, 0)).get_bomb())

        self.assertEqual(game_state.game_plan.width, 14)
        self.assertEqual(game_state.game_plan.height, 14)
    def test_dto_2_game_setup(self):
        game_setup = dto_2_game_setup(json.loads(create_game_state_string(7)))

        self.assertEqual(game_setup.ai_player_id, 1)

        self.assertEqual(game_setup.game_plan.width, 14)
        self.assertEqual(game_setup.game_plan.height, 14)
        self.assertEqual(len(game_setup.game_plan.starting_positions), 4)
        self.assertEqual(game_setup.game_plan.starting_positions[1],
                         Point(3, 10))
        self.assertEqual(game_setup.game_plan.starting_positions[4],
                         Point(3, 3))
        self.assertEqual(game_setup.game_plan.max_rounds, 100)

        self.assertEqual(len(game_setup.player_ids), 4)
        self.assertTrue(1 in game_setup.player_ids)
        self.assertTrue(2 in game_setup.player_ids)
        self.assertTrue(3 in game_setup.player_ids)
        self.assertTrue(4 in game_setup.player_ids)
Ejemplo n.º 11
0
def create_game_plan_from_dto(dto: Any) -> GamePlan:
    starting_positions = {
        int(player): Point(playerDto["homeBaseLocation"]["x"],
                           playerDto["homeBaseLocation"]["y"])
        for player, playerDto in dto["players"].items()
    }

    return GamePlan(width=dto["gamePlanWidth"],
                    height=dto["gamePlanHeight"],
                    starting_positions=starting_positions,
                    max_rounds=dto["remainingRounds"] + dto["currentRound"] +
                    1)
Ejemplo n.º 12
0
def string_2_game_state(game_state_as_string: str) -> StandardGameState:
    """
        Creates a game state from a string representation.
        See VisualisationUtil.java for description of all available characters.
        In general:
        - Capital letters A-H are locations of home bases. Agents are placed on the home bases.
        - Characters " ." are fields with different number of resources on it.
           ' '  has 0 resources,
           '.'  has 1 resource
          - Numbers 1-9 are bombs belonging to player A with countdown equal to the number.
            There are 0 resources under a bomb.

        For example the following will create 5x5 game plan.
        There will be:
         - 1 agent of player 1 (letter 'A')
         - 1 agent of player 2 in the home base 'B'
         - resources in the upper left corner and along the bottom
         - bomb with countdown 2 in the center belonging to player A
        "...  \n" +
        ".A   \n" +
        "  2  \n" +
        "  B  \n" +
        ".....\n" +
    """

    lines = game_state_as_string.splitlines()

    height = len(lines)
    width = len(lines[0])

    players = []
    starting_positions = {}

    for line in lines:
        if len(line) != width:
            raise Exception(
                'String is not rectangular - all lines must have the same length.'
            )

    for x in range(0, width):
        for y in range(0, height):
            c = lines[y][x]

            if c in VisualisationUtil.BASE_CHARS:
                player_id = VisualisationUtil.get_player_id(c)
                players.append(player_id)
                starting_positions[player_id] = Point(x, y)
            elif c not in VisualisationUtil.AGENT_CHARS and c not in VisualisationUtil.RESOURCES_CHARS and c not in VisualisationUtil.BOMB_CHARS:
                raise Exception("Unknown character '" + c + "'")

    game_plan = GamePlan(width, height, starting_positions, DEFAULT_MAX_ROUNDS)
    game_state = StandardGameState(game_plan, players, True)

    for position, field in game_state.all_fields():
        c = lines[position.y][position.x]

        if c in VisualisationUtil.AGENT_CHARS:
            game_state.spawn_agent(position,
                                   VisualisationUtil.get_player_id(c))
            game_state.set_resources_on_field(position, 0)
        elif c in VisualisationUtil.RESOURCES_CHARS:
            game_state.set_resources_on_field(
                position, VisualisationUtil.get_resources(c))
        elif c in VisualisationUtil.BOMB_CHARS:
            player_id = VisualisationUtil.get_player_id('a')
            bomb = Bomb(player_id, int(c))
            game_state.set_bomb_on_field(position, bomb)
            game_state.set_resources_on_field(position, 0)
        elif c not in VisualisationUtil.BASE_CHARS:
            raise Exception("Unknown character '" + c + "'")

    return game_state
    def setUp(self):
        super().setUp()

        self.player_state = PlayerState(1)
        self.player_state.spawn_new_agent(Point(1, 2))
 def test_get_agent(self):
     self.assertEquals(self.player_state.agent.get_position(), Point(1, 2))
 def test_set_resources_on_field(self):
     self.game_state.set_resources_on_field(Point(2, 2), 0)
     self.assertEquals(
         self.game_state.get_field(Point(2, 2)).get_resource_count(), 0)
 def test_empty_field(self):
     field = self.game_state.get_field(Point(0, 0))
     self.assertIsNone(field.get_agent())
     self.assertIsNone(field.get_base())
     self.assertGreater(field.get_resource_count(), 0)
 def test_mismatch_starting_positions(self):
     self.assertRaises(Exception, StandardGameState,
                       GamePlan(10, 10, {1: Point(1, 1)}, 5), [1, 2])
Ejemplo n.º 18
0
 def __iter__(self) -> Iterator[Point]:
     """ Iterate over all fields on the game plan """
     return (Point(i // self.height, i % self.height)
             for i in range(0, self.width * self.height))
Ejemplo n.º 19
0
 def from_point(self, point: Point) -> Point:
     return Point(point.x + self.dx, point.y + self.dy)
 def test_setting_more_resources_than_allowed(self):
     self.game_state.set_resources_on_field(Point(2, 2), 200000)
     self.assertEquals(
         self.game_state.get_field(Point(2, 2)).get_resource_count(),
         GameConstants.FIELD_MAX_RESOURCES)
Ejemplo n.º 21
0
 def test_equality(self):
     self.assertTrue(Point(1, 2) == Point(1, 2))
     self.assertFalse(Point(2, 2) == Point(1, 2))
Ejemplo n.º 22
0
 def test_values_accessible(self):
     self.assertEquals(Point(1, 2).x, 1)
     self.assertEquals(Point(1, 2).y, 2)
 def test_field_with_base(self):
     self.assertEquals(self.game_state.get_field(Point(2, 2)).get_base(), 2)