extented TurtleExcuter

This commit is contained in:
Djeeberjr 2022-04-26 19:43:34 +02:00
parent ceb55e86dc
commit 1ad78c23bb

View File

@ -1,5 +1,6 @@
package lib.turtle; package lib.turtle;
import util.Pos3;
import kernel.turtle.Turtle; import kernel.turtle.Turtle;
using tink.CoreApi; using tink.CoreApi;
@ -11,6 +12,51 @@ class TurtleExecuter {
this.instructions = instructions; this.instructions = instructions;
} }
public function getRequiredFuel(): Int {
var fuel = 0;
for(inst in instructions){
if (inst == Forward || inst == Back || inst == Up || inst == Down) {
fuel++;
}
}
return fuel;
}
public function getRequiredBlocks(): Int {
var blocks = 0;
for(inst in instructions){
switch inst {
case Place(_):
blocks++;
default:
}
}
return blocks;
}
public function getFinalOffset(): Pos3 {
var pos: Pos3 = {x:0, y:0, z:0};
var forwardVec: Pos3 = {x: 1, y: 0, z: 0};
for (inst in instructions){
switch inst {
case Forward:
pos = pos + forwardVec;
case Back:
pos = pos - forwardVec;
case TurnRight:
forwardVec = {x: -forwardVec.z, z: forwardVec.x, y: forwardVec.y};
case TurnLeft:
forwardVec = {x: forwardVec.z, z: -forwardVec.x , y: forwardVec.y};
default:
}
}
return pos;
}
public function execute() { public function execute() {
for (inst in instructions){ for (inst in instructions){
executeInst(inst); executeInst(inst);