added turtle executer
This commit is contained in:
parent
dffb493d4f
commit
b40e293d6e
51
src/lib/turtle/TurtleExecuter.hx
Normal file
51
src/lib/turtle/TurtleExecuter.hx
Normal file
@ -0,0 +1,51 @@
|
||||
package lib.turtle;
|
||||
|
||||
import kernel.turtle.Turtle;
|
||||
using tink.CoreApi;
|
||||
|
||||
class TurtleExecuter {
|
||||
|
||||
private var instructions:Array<TurtleInstruction>;
|
||||
|
||||
public function new(instructions:Array<TurtleInstruction>) {
|
||||
this.instructions = instructions;
|
||||
}
|
||||
|
||||
public function execute() {
|
||||
for (inst in instructions){
|
||||
executeInst(inst);
|
||||
}
|
||||
}
|
||||
|
||||
private function executeInst(instruction: TurtleInstruction): Outcome<Noise,String> {
|
||||
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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user