PythonTODAY-PICK

python) Reeborg’s World Hurdle4 풀기

은근히 안되서 될 때까지 붙잡고 풀었습니다.

python 코드를 연습하기 좋은. 스크래치 게임과 비슷한 Reeborg’s World 입니다.

Reeborg's World
Reeborg’s World

1, 2, 3 단계는 쉽게 풀릴테고, 4단계부터는 코드 하나에 하나하나 어떻게 돌아가는지 확인이 필요합니다.

코드를 짜는 건 귀찮으니 빠르게 해결하려고 했던 건데… 가는지 안가는지 확인해야 한다니ㅋㅋㅋ

사람이라면 장애물이 대략 몇 미터인지 파악해서 4미터면 4번 갔다 도는 것, 1미티 정도면 1번 돌고… 이런 식으로 빠를 텐데.

귀여운 Reeborg는 눈 앞에 장애물이 없으면 이동해, 우측에는 벽이 있어? 앞에는 장애물이 있어? 만약 우측에 벽이 없다면…

이라고 하나부터 열까지 하나하나 설정해야 합니다.

일단 4단계 풀었던 코드는 요렇습니다. 그런데 해결된 코드 복붙해봐야 재미없으니 직접 하는 걸 강추…

원래 가지고 있던 def와 따로 만들어서 움직이도록 짰습니다.

하다가 에러나면 오기도 생기고, 될 때까지 한다. 이런 느낌으로 해보면 엄청 재밌습니다.

사이트 주소:

https://reeborg.ca/reeborg.html?lang=en&mode=python&menu=worlds%2Fmenus%2Freeborg_intro_en.json&name=Hurdle%201&url=worlds%2Ftutorial_en%2Fhurdle1.json

def turn_right():
    turn_left()
    turn_left()
    turn_left()

def jump():
    turn_left()
    move()
    turn_right()
    move()
    turn_right()
    move()
    turn_left()
    
while at_goal() != True:
    if right_is_clear():
        turn_right()
        move()
        turn_right()
        move()
    elif front_is_clear():
        move()
    elif wall_in_front():
        turn_left()
    elif wall_on_right():
        move()
        turn_right()

그리고 다음 스테이지로 이동!

또다른 솔루션은

def turn_right():
    turn_left()
    turn_left()
    turn_left()

def jump():
    turn_left()
    while wall_on_right():
        move()
    turn_right()
    move()
    turn_right()
    while front_is_clear():
        move()
    turn_left()
        
    
while at_goal() != True:
    if wall_in_front():
        jump()
    else:
        move()

좀 더 깔끔한 코드

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다

error: Content is protected !!