コード例 #1
0
def _navigate_with_heading(navigation: NavigationWithHeading,
                           nav: NavigationInstruction) -> None:
    if nav.action == RIGHT:
        navigation.right(nav.value)
    elif nav.action == LEFT:
        navigation.left(nav.value)
    elif nav.action == FORWARD:
        navigation.forward(nav.value)
    elif nav.action in {NORTH, EAST, SOUTH, WEST}:
        navigation.drift(heading=Headings[nav.action].value, amount=nav.value)
    else:
        raise ValueError("invalid input")
コード例 #2
0
    def test_navigation_with_heading(self):
        navigation = NavigationWithHeading(
            Position(0, 0), Headings["N"].value,
            lambda pos: pos.x <= 4 and pos.y <= 4)
        navigation.drift(Headings["N"].value, 1)
        navigation.left(90)
        navigation.forward(1)
        navigation.right(180)
        navigation.forward(10)

        self.assertEqual(navigation.position, Position(4, 1))
        visited_positions = navigation.get_visited_positions(True)
        self.assertEqual(
            visited_positions,
            [Position(0, 0),
             Position(0, 1),
             Position(-1, 1),
             Position(4, 1)])
        visited_positions = navigation.get_visited_positions(False)
        self.assertEqual(
            visited_positions,
            [Position(0, 1), Position(-1, 1),
             Position(4, 1)])