SOLUTION TASK 2.01: Loop
class MyPlayer extends Player { start() { while (canMove()) { move(); } } }
SOLUTION TASK 2.02: Loop Star
class MyPlayer extends Player { start() { while (!onStar()) { move(); } } }
SOLUTION TASK 2.03: Leaving the Tunnel
class MyPlayer extends Player { start() { while (treeLeft() && treeRight()) { move(); } putStar(); } }
SOLUTION TASK 2.04: Afraid of Tunnel
class MyPlayer extends Player { start() { while (!treeLeft() || !treeRight()) { move(); } say('AHHHH! This looks very dark in here!'); } }
We could also write the while
condition as follows:
while (!(treeLeft() && treeRight())
SOLUTION TASK 2.05: Climbing Up
class MyPlayer extends Player { start() { while (treeFront()) { oneStepUp(); } } /// Climbes one step. oneStepUp() { turnLeft(); move(); turnRight(); move(); } }