finished uebung10

This commit is contained in:
Johannes Schmelz 2024-06-18 13:17:01 +00:00
parent b439030fee
commit 9908656813
9 changed files with 56 additions and 9 deletions

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,30 @@
package uebung10.logo;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import org.junit.Before;
public class HanselGretelTest {
public static final double EPS = 1e-10;
public static final Stmt PROG = new Sequence(new PenDown(), new Go(50),
new Turn(120), new Go(50),
new Turn(120), new Go(50));
private Turtle turtle;
@Before
public void setup() {
turtle = new Turtle(2, 1, 0);
}
@Test
public void TestTriangle() {
final Visitor<Void> visitor = new HanselGretelVisitor(10, turtle, new BlackForest()); //final ist egal
PROG.accept(visitor);
assertEquals(2, turtle.getX(), EPS);
assertEquals(1, turtle.getY(), EPS);
}
}

View File

@ -46,6 +46,15 @@ public class HanselGretelVisitor implements Visitor<Void> {
return null; return null;
} }
private void breadCrumb() {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'breadCrumb'");
}
private void layTrail(double d) {
forest.breadCrumb(turtle.getX(), turtle.getY());
}
private abstract class State { private abstract class State {
abstract void go(double dist); abstract void go(double dist);
void up() {} void up() {}
@ -54,20 +63,28 @@ public class HanselGretelVisitor implements Visitor<Void> {
private final State shortUp = new ShortUpState();// muss nicht geschrieben werden private final State shortUp = new ShortUpState();// muss nicht geschrieben werden
private final State down = new DownState();// muss nicht geschrieben werden private final State down = new DownState();// muss nicht geschrieben werden
private final State longUp = new LongUpSate(); private final State longUp = new State(){
private class LongUpSate extends State{ @Override
void go(double d) {} void go(double d) {turtle.go(d);}
void up(){}
void down(){} @Override
} void down(){
breadCrumb();
state = down;
}
};
private class DownState extends State{ private class DownState extends State{
void go(double d){} void go(double d){
void up(){} layTrail(d);
void down(){} }
void up(){state = shortUp;}
} }
//TODO
private class ShortUpState extends State { private class ShortUpState extends State {
void go(double d){} void go(double d){}
void up(){} void up(){}