From 17be4149db4efa0eea0dc02c331bdc0bfdae4c17 Mon Sep 17 00:00:00 2001 From: Djeeberjr Date: Fri, 14 Apr 2023 14:36:09 +0200 Subject: [PATCH] added turtle program --- src/bin/Terminal.hx | 2 ++ src/bin/Turtle.hx | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 src/bin/Turtle.hx diff --git a/src/bin/Terminal.hx b/src/bin/Terminal.hx index a221793..53fc1e5 100644 --- a/src/bin/Terminal.hx +++ b/src/bin/Terminal.hx @@ -153,6 +153,8 @@ class Terminal extends UIApp { return new Disk(); case "gps": return new GPS(); + case "turtle": + return new Turtle(); default: return null; } diff --git a/src/bin/Turtle.hx b/src/bin/Turtle.hx new file mode 100644 index 0000000..439a1d6 --- /dev/null +++ b/src/bin/Turtle.hx @@ -0,0 +1,44 @@ +package bin; + +import lib.CLIAppBase; + +using tink.CoreApi; + +class Turtle extends CLIAppBase { + public function new() { + registerSyncSubcommand("forward", (args)->{ + return perform(kernel.turtle.Turtle.instance.forward()); + }); + + registerSyncSubcommand("back", (args)->{ + return perform(kernel.turtle.Turtle.instance.back()); + }); + + registerSyncSubcommand("left", (args)->{ + return perform(kernel.turtle.Turtle.instance.turnLeft()); + }); + + registerSyncSubcommand("right", (args)->{ + return perform(kernel.turtle.Turtle.instance.turnRight()); + }); + + registerSyncSubcommand("up", (args)->{ + return perform(kernel.turtle.Turtle.instance.up()); + }); + + registerSyncSubcommand("down", (args)->{ + return perform(kernel.turtle.Turtle.instance.down()); + }); + } + + private function perform(outcome: Outcome): Bool { + switch outcome { + case Success(_): + return true; + case Failure(error): + handle.write(error); + return false; + } + } + +}