Exemplo n.º 1
0
    def get_path(self, pos_a: Position, pos_b: Position) -> List[Field]:
        """
        Calculates a passable path between the two given positions
        :param pos_a:
        :param pos_b:
        :return:
        """

        class Node:
            def __init__(self, node, cost, expected, parent):
                self.node = node
                 # same position reference that Field-Object has
                self.pos = node.position
                self.cost = cost
                self.expected = expected
                self.total = self.cost + self.expected
                self.parent = parent

            def __eq__(self, other):
                return self.pos == other.pos

        current_field = Node(self.get(pos_a.tuple), 0, pos_a.distance_to(pos_b), None)
        path = [current_field]
        already_checked = []

        while current_field.pos != pos_b:

            valid_neighbours = filter(
                lambda x: x.passable,
                self._get_neighbours(current_field.pos)
            )

            for field in valid_neighbours:

                # calculate the values of each field
                cost = current_field.pos.distance_to(field.position)

                h = field.position.distance_to(pos_b)

                node = Node(field, cost, h, current_field)

                # dont want duplicates
                if node not in path and node not in already_checked:
                    already_checked.append(node)

            already_checked.sort(key=lambda x: x.total)
            current_field = already_checked[0]
            already_checked.remove(current_field)
            path.append(current_field)

        ordered_path = []
        while current_field is not None:
            ordered_path.append(current_field.node)
            current_field = current_field.parent
        ordered_path.reverse()
        return ordered_path
Exemplo n.º 2
0
    def test_position_range(self):
        p1 = Position(0, 0)
        p2 = Position(10, 0)
        p3 = Position(10, 10)
        self.assertTrue(p1.in_range(p2, 20), "Position.in_range function not working")
        self.assertTrue(p1.in_range(p2, 10), "Position.in_range function not working")
        self.assertFalse(p1.in_range(p2, 9.9999), "Position.in_range function not working")
        self.assertFalse(p1.in_range(p2, 0), "Position.in_range function not working")
        self.assertTrue(p1.in_range(p3, 14.15), "Position.in_range function not working")
        self.assertFalse(p1.in_range(p3, 14), "Position.in_range function not working")

        with self.assertRaises(TypeError):
            p1.in_range(p2)
Exemplo n.º 3
0
    def test_position_distance(self):
        p1 = Position(0, 0)
        p2 = Position(10, 0)
        p3 = Position(10, 10)
        self.assertEqual(p1.distance_to(p2), 10, "Position.distance_to not working as expected")
        self.assertEqual(p2.distance_to(p1), 10, "Position.distance_to not working as expected")
        self.assertAlmostEqual(p1.distance_to(p3), sqrt(200), 5, "Position.distance_to not working as expected")

        with self.assertRaises(TypeError):
            p1.distance_to("nothing")