update program
This commit is contained in:
@@ -251,4 +251,14 @@ public abstract class GameObject {
|
||||
|
||||
return Math.sqrt(Math.pow((otherLocation.getX() - location.getX()), 2) + Math.pow((otherLocation.getY() - location.getY()), 2));
|
||||
}
|
||||
|
||||
public double getAngle(GameObject o) {
|
||||
|
||||
double x1 = getLocation().getX();
|
||||
double y1 = getLocation().getY();
|
||||
double x2 = o.getLocation().getX();
|
||||
double y2 = o.getLocation().getY();
|
||||
|
||||
return Math.atan(Math.abs(x1 - x2) / Math.abs(y1 - y2));
|
||||
}
|
||||
}
|
@@ -0,0 +1,26 @@
|
||||
package worms.programs;
|
||||
|
||||
import java.util.function.BiFunction;
|
||||
|
||||
public class ArgumentBinaryExpression<T, O, R> extends ArgumentExpression<R, O> {
|
||||
|
||||
private final Expression expression;
|
||||
private final BiFunction<T, O, R> function;
|
||||
|
||||
ArgumentBinaryExpression(Expression expression, BiFunction<T, O, R> function, Type type) {
|
||||
super(type);
|
||||
this.expression = expression;
|
||||
this.function = function;
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public R execute(O o) {
|
||||
|
||||
try {
|
||||
return function.apply(((Expression<T>) expression).execute(), o);
|
||||
} catch (ClassCastException e) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
}
|
||||
}
|
27
OGP1718-Worms/src/worms/programs/ArgumentExpression.java
Normal file
27
OGP1718-Worms/src/worms/programs/ArgumentExpression.java
Normal file
@@ -0,0 +1,27 @@
|
||||
package worms.programs;
|
||||
|
||||
public abstract class ArgumentExpression<R, O> implements Expression<R> {
|
||||
|
||||
public final Type type;
|
||||
|
||||
protected ArgumentExpression(Type type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
@Override
|
||||
public R execute() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
public abstract R execute(O o);
|
||||
|
||||
|
||||
public Type getType() {
|
||||
return this.type;
|
||||
}
|
||||
public enum Type {
|
||||
WORM,
|
||||
VAR,
|
||||
GOBJECTS
|
||||
}
|
||||
}
|
@@ -2,7 +2,7 @@ package worms.programs;
|
||||
|
||||
import java.util.function.BiFunction;
|
||||
|
||||
public class BinaryExpression<T, R> extends Expression {
|
||||
public class BinaryExpression<T, R> implements Expression<R> {
|
||||
|
||||
private final Expression left;
|
||||
private final Expression right;
|
||||
@@ -22,6 +22,5 @@ public class BinaryExpression<T, R> extends Expression {
|
||||
} catch (ClassCastException e) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@@ -2,7 +2,7 @@ package worms.programs;
|
||||
|
||||
import worms.model.Program;
|
||||
|
||||
public abstract class Expression<T> extends Program {
|
||||
public interface Expression<T> {
|
||||
|
||||
public abstract T execute();
|
||||
T execute();
|
||||
}
|
||||
|
@@ -2,7 +2,7 @@ package worms.programs;
|
||||
|
||||
import worms.model.Program;
|
||||
|
||||
public class Procedure extends Program {
|
||||
public class Procedure {
|
||||
|
||||
|
||||
private final String name;
|
||||
|
@@ -5,9 +5,10 @@ import worms.model.Program;
|
||||
import worms.model.Worm;
|
||||
import worms.util.ModelException;
|
||||
import worms.util.MustNotImplementException;
|
||||
import worms.programs.Expression;
|
||||
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
public class ProgramFactory implements IProgramFactory<Expression, Statement, Procedure, Program> {
|
||||
/**
|
||||
@@ -195,10 +196,10 @@ public class ProgramFactory implements IProgramFactory<Expression, Statement, Pr
|
||||
*/
|
||||
@Override
|
||||
public Expression createReadVariableExpression(String variableName, SourceLocation sourceLocation) throws ModelException {
|
||||
return new Expression() {
|
||||
return new ArgumentExpression<Object, List<Object>>(ArgumentExpression.Type.VAR) {
|
||||
@Override
|
||||
public Object execute() {
|
||||
return findVar(variableName);
|
||||
public Object execute(List<Object> o) {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -211,12 +212,7 @@ public class ProgramFactory implements IProgramFactory<Expression, Statement, Pr
|
||||
*/
|
||||
@Override
|
||||
public Expression createDoubleLiteralExpression(double value, SourceLocation location) throws ModelException {
|
||||
return new Expression<Double>() {
|
||||
@Override
|
||||
public Double execute() {
|
||||
return value;
|
||||
}
|
||||
};
|
||||
return () -> value;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -227,12 +223,7 @@ public class ProgramFactory implements IProgramFactory<Expression, Statement, Pr
|
||||
*/
|
||||
@Override
|
||||
public Expression createBooleanLiteralExpression(boolean value, SourceLocation location) throws ModelException {
|
||||
return new Expression<Boolean>() {
|
||||
@Override
|
||||
public Boolean execute() {
|
||||
return value;
|
||||
}
|
||||
};
|
||||
return () -> value;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -242,12 +233,7 @@ public class ProgramFactory implements IProgramFactory<Expression, Statement, Pr
|
||||
*/
|
||||
@Override
|
||||
public Expression createNullExpression(SourceLocation location) throws ModelException {
|
||||
return new Expression<Object>() {
|
||||
@Override
|
||||
public Object execute() {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
return () -> null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -259,10 +245,10 @@ public class ProgramFactory implements IProgramFactory<Expression, Statement, Pr
|
||||
@Override
|
||||
public Expression createSelfExpression(SourceLocation location) throws ModelException {
|
||||
|
||||
return new Expression<Worm>() {
|
||||
return new ArgumentExpression<Worm, Worm>(ArgumentExpression.Type.WORM) {
|
||||
@Override
|
||||
public Worm execute() {
|
||||
return getWorm();
|
||||
public Worm execute(Worm o) {
|
||||
return o;
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -320,7 +306,7 @@ public class ProgramFactory implements IProgramFactory<Expression, Statement, Pr
|
||||
@Override
|
||||
public Expression createEqualityExpression(Expression left, Expression right, SourceLocation location) throws ModelException {
|
||||
|
||||
return new BinaryExpression<Object, Boolean>(left, right, (l,r) -> l.equals(r));
|
||||
return new BinaryExpression<Object, Boolean>(left, right, (l, r) -> l.equals(r));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -340,7 +326,18 @@ public class ProgramFactory implements IProgramFactory<Expression, Statement, Pr
|
||||
|
||||
@Override
|
||||
public Expression createSearchObjectExpression(Expression angleDelta, SourceLocation sourceLocation) throws ModelException {
|
||||
return null;
|
||||
|
||||
return new ArgumentExpression<GameObject, Worm>(ArgumentExpression.Type.GOBJECTS) {
|
||||
@Override
|
||||
public GameObject execute(Worm o) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
public GameObject execute(Worm o, Set<GameObject> gobjects) {
|
||||
return gobjects.stream().filter(g -> g.getAngle(o) == o.getOrientation()).min(
|
||||
Comparator.comparingDouble(gameObject -> gameObject.getDistance(o))).orElse(null);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -354,7 +351,7 @@ public class ProgramFactory implements IProgramFactory<Expression, Statement, Pr
|
||||
@Override
|
||||
public Expression createSameTeamExpression(Expression entity, SourceLocation sourceLocation) throws ModelException, MustNotImplementException {
|
||||
|
||||
return new UnaryExpression<Worm, Boolean>(entity, w -> (w).getTeam().equals((w).getWorld().getActiveWorm().getTeam()));
|
||||
return new ArgumentBinaryExpression<Worm, Worm, Boolean>(entity, (w, y) -> w.getTeam().equals(y.getTeam()), ArgumentExpression.Type.WORM);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -367,7 +364,7 @@ public class ProgramFactory implements IProgramFactory<Expression, Statement, Pr
|
||||
@Override
|
||||
public Expression createDistanceExpression(Expression entity, SourceLocation sourceLocation) throws ModelException {
|
||||
|
||||
return new UnaryExpression<GameObject, Double>(entity, (l) -> (l).getWorld().getActiveWorm().getDistance(l));
|
||||
return new ArgumentBinaryExpression<GameObject, Worm, Double>(entity, (e, o) -> e.getDistance(o), ArgumentExpression.Type.WORM);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -379,6 +376,7 @@ public class ProgramFactory implements IProgramFactory<Expression, Statement, Pr
|
||||
*/
|
||||
@Override
|
||||
public Expression createIsWormExpression(Expression entity, SourceLocation sourceLocation) throws ModelException {
|
||||
return new UnaryExpression<>(entity, w -> w instanceof Worm);
|
||||
|
||||
return new UnaryExpression<Object, Boolean>(entity, w -> w instanceof Worm);
|
||||
}
|
||||
}
|
||||
|
@@ -1,8 +1,6 @@
|
||||
package worms.programs;
|
||||
|
||||
import worms.model.Program;
|
||||
|
||||
public class Statement extends Program {
|
||||
public class Statement {
|
||||
|
||||
private final Type type;
|
||||
private final Object data;
|
||||
|
@@ -2,7 +2,7 @@ package worms.programs;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
public class UnaryExpression<T, R> extends Expression<R> {
|
||||
public class UnaryExpression<T, R> implements Expression<R> {
|
||||
|
||||
protected final Expression expression;
|
||||
private final Function<T, R> function;
|
||||
|
Reference in New Issue
Block a user