to follow-wall

launch [loop [

ifelse sensors-equal?

[ab, setpower 2 a, on thatway b, on thisway d, on]

[ifelse front-strong

[turn-left ]

[ifelse back-strong

[turn-right]

[if front-clear [stoprules ab, off corner-turn]]]]]]

forever [if (forward-sensor) > 150 [ab, off stoprules right-corner-turn]]

end


to follow

ab, setpower 4

loop [

ifelse front-strong

[turn-right]

[ifelse back-strong

[turn-left]

[fwd]

] ]

end


to right-corner-turn

bwd-follow wait 30 two-wheel-turn

end


to two-wheel-turn

ab, off

a, setpower 5

b, setpower 3

turn-right

waituntil [back-clear]

a, setpower 3

turn-right

waituntil [not front-clear]

follow-wall

end


to bwd

a, thisway on b, thatway on

end


to bwd-follow

launch [loop [

ifelse sensors-equal?

[ab, setpower 2 a, on thisway b, on thatway d, on]

[ifelse front-strong

[turn-left waituntil sensors-equal?]

[if back-strong

[turn-right]]]]]


end

to sensors-equal?

output and not front-strong not back-strong

end


to front-strong

output (back-sensor + 2) < (front-sensor)

end


to back-strong

output (front-sensor + 2) < (back-sensor)

end


to front-clear

output (front-sensor) > 240

end


to turn-right

a, thisway on b, off

end


to turn-left

b, thatway on a, off

end


to back-clear

output (back-sensor) > 240

end


to front-sensor

output sensor 1

end


to back-sensor

output sensor 2

end


to forward-sensor

output sensor 0

end


to left-wheel

a,

end


to fwd

a, thatway on b, thisway on

end


to corner-turn

forever [turn-right wait 2 turn-left wait 5 fwd wait 5 turn-left wait 5 ifelse (front-sensor < 170) [turn-right wait 20 fwd waituntil [not back-clear] follow-wall] [corner-turn]]]

end


}