From b40e293d6e3e10c2c8b9222ceb42f5066cc37346 Mon Sep 17 00:00:00 2001 From: Djeeberjr Date: Wed, 13 Apr 2022 21:40:45 +0200 Subject: [PATCH] added turtle executer --- src/lib/turtle/TurtleExecuter.hx | 51 ++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 src/lib/turtle/TurtleExecuter.hx diff --git a/src/lib/turtle/TurtleExecuter.hx b/src/lib/turtle/TurtleExecuter.hx new file mode 100644 index 0000000..83055dc --- /dev/null +++ b/src/lib/turtle/TurtleExecuter.hx @@ -0,0 +1,51 @@ +package lib.turtle; + +import kernel.turtle.Turtle; +using tink.CoreApi; + +class TurtleExecuter { + + private var instructions:Array; + + public function new(instructions:Array) { + this.instructions = instructions; + } + + public function execute() { + for (inst in instructions){ + executeInst(inst); + } + } + + private function executeInst(instruction: TurtleInstruction): Outcome { + switch instruction { + case Forward: + return Turtle.instance.forward(); + case Back: + return Turtle.instance.back(); + case Up: + return Turtle.instance.up(); + case Down: + return Turtle.instance.down(); + case TurnLeft: + return Turtle.instance.turnLeft(); + case TurnRight: + return Turtle.instance.turnRight(); + case Dig(dir): + return Turtle.instance.dig(dir); + case Place(dir): + return Turtle.instance.place(dir); + case PlacseSign(dir, text): + return Turtle.instance.placeSign(dir, text); + case Select(slot): + var r = Turtle.instance.selectSlot(slot); + if (r.isSuccess()) { + return Outcome.Success(null); + } else { + return Outcome.Failure("Failed to select slot"); + } + } + + return Outcome.Failure("Unknown instruction: " + instruction); + } +}