diff --git a/src/bin/Turtle.hx b/src/bin/Turtle.hx index 0bc28ab..727826f 100644 --- a/src/bin/Turtle.hx +++ b/src/bin/Turtle.hx @@ -7,30 +7,38 @@ using tink.CoreApi; class Turtle extends CLIAppBase { public function new() { registerSyncSubcommand("forward", (args) -> { - return perform(kernel.turtle.Turtle.instance.forward()); + return checkAvailable() && perform(kernel.turtle.Turtle.instance.forward()); }); registerSyncSubcommand("back", (args) -> { - return perform(kernel.turtle.Turtle.instance.back()); + return checkAvailable() && perform(kernel.turtle.Turtle.instance.back()); }); registerSyncSubcommand("left", (args) -> { - return perform(kernel.turtle.Turtle.instance.turnLeft()); + return checkAvailable() && perform(kernel.turtle.Turtle.instance.turnLeft()); }); registerSyncSubcommand("right", (args) -> { - return perform(kernel.turtle.Turtle.instance.turnRight()); + return checkAvailable() && perform(kernel.turtle.Turtle.instance.turnRight()); }); registerSyncSubcommand("up", (args) -> { - return perform(kernel.turtle.Turtle.instance.up()); + return checkAvailable() && perform(kernel.turtle.Turtle.instance.up()); }); registerSyncSubcommand("down", (args) -> { - return perform(kernel.turtle.Turtle.instance.down()); + return checkAvailable() && perform(kernel.turtle.Turtle.instance.down()); }); } + private function checkAvailable(): Bool { + if (!kernel.turtle.Turtle.isTurtle()){ + handle.write("No turtle attached"); + return false; + } + return true; + } + private function perform(outcome:Outcome):Bool { switch outcome { case Success(_): diff --git a/src/kernel/turtle/Turtle.hx b/src/kernel/turtle/Turtle.hx index ac25769..79a6584 100644 --- a/src/kernel/turtle/Turtle.hx +++ b/src/kernel/turtle/Turtle.hx @@ -18,7 +18,7 @@ class Turtle { } public static function isTurtle():Bool { - return true; // TODO: Implement + return cc.Turtle != null; } private function conterToOutcome(r:cc.Turtle.TurtleActionResult):Outcome {