def turn_right():
turn_left()
turn_left()
turn_left()
def jump():
turn_left()
move()
turn_right()
move()
turn_right()
move()
turn_left()
while not at_goal():
if front_is_clear():
move()
elif wall_in_front():
jump()
장애물을 뛰는 것과, 오른쪽을 보는 것이 지속적으로 나온다.
계속 move()와 turn_left()를 사용하는 것은 매우 비효율적이다
그래서 turn_right() 과 jump() 라는 함수를 따로 만들어서, 해당 코드를 실행한다
그리고 while문을 통해, 목적지까지 도착할때까지, 뛰거나 움직이는 것을 반복한다
실습 2
null
def turn_right():
turn_left()
turn_left()
turn_left()
def jump():
cnt = 0
while wall_in_front():
turn_left()
move()
turn_right()
cnt += 1
move()
turn_right()
for _ in range(cnt):
move()
turn_left()
while not at_goal():
if front_is_clear():
move()
elif wall_in_front():
jump()
이번에는 점프를 하는데, 장애물 크기가 다 다르다
이럴 때는 while문을 써서, 앞으로 움직일때마다 장애물이 있는지 확인을 하며 몇번을 움직였는지 `cnt`에 누적한다
wall_on_right() 과 front_is_clear()로 대체할 수 있다
그리고 그 `cnt`만큰 내려오면 된다
실습 3
null
def turn_right():
turn_left()
turn_left()
turn_left()
while not at_goal():
if right_is_clear():
turn_right()
move
if front_is_clear():
move()
elif wall_in_front():
if wall_on_right():
turn_left()
elif right_is_clear():
turn_right()