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));
|
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;
|
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 left;
|
||||||
private final Expression right;
|
private final Expression right;
|
||||||
@@ -22,6 +22,5 @@ public class BinaryExpression<T, R> extends Expression {
|
|||||||
} catch (ClassCastException e) {
|
} catch (ClassCastException e) {
|
||||||
throw new IllegalArgumentException();
|
throw new IllegalArgumentException();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -2,7 +2,7 @@ package worms.programs;
|
|||||||
|
|
||||||
import worms.model.Program;
|
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;
|
import worms.model.Program;
|
||||||
|
|
||||||
public class Procedure extends Program {
|
public class Procedure {
|
||||||
|
|
||||||
|
|
||||||
private final String name;
|
private final String name;
|
||||||
|
@@ -5,9 +5,10 @@ import worms.model.Program;
|
|||||||
import worms.model.Worm;
|
import worms.model.Worm;
|
||||||
import worms.util.ModelException;
|
import worms.util.ModelException;
|
||||||
import worms.util.MustNotImplementException;
|
import worms.util.MustNotImplementException;
|
||||||
import worms.programs.Expression;
|
|
||||||
|
|
||||||
|
import java.util.Comparator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
public class ProgramFactory implements IProgramFactory<Expression, Statement, Procedure, Program> {
|
public class ProgramFactory implements IProgramFactory<Expression, Statement, Procedure, Program> {
|
||||||
/**
|
/**
|
||||||
@@ -195,10 +196,10 @@ public class ProgramFactory implements IProgramFactory<Expression, Statement, Pr
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public Expression createReadVariableExpression(String variableName, SourceLocation sourceLocation) throws ModelException {
|
public Expression createReadVariableExpression(String variableName, SourceLocation sourceLocation) throws ModelException {
|
||||||
return new Expression() {
|
return new ArgumentExpression<Object, List<Object>>(ArgumentExpression.Type.VAR) {
|
||||||
@Override
|
@Override
|
||||||
public Object execute() {
|
public Object execute(List<Object> o) {
|
||||||
return findVar(variableName);
|
return null;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -211,12 +212,7 @@ public class ProgramFactory implements IProgramFactory<Expression, Statement, Pr
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public Expression createDoubleLiteralExpression(double value, SourceLocation location) throws ModelException {
|
public Expression createDoubleLiteralExpression(double value, SourceLocation location) throws ModelException {
|
||||||
return new Expression<Double>() {
|
return () -> value;
|
||||||
@Override
|
|
||||||
public Double execute() {
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -227,12 +223,7 @@ public class ProgramFactory implements IProgramFactory<Expression, Statement, Pr
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public Expression createBooleanLiteralExpression(boolean value, SourceLocation location) throws ModelException {
|
public Expression createBooleanLiteralExpression(boolean value, SourceLocation location) throws ModelException {
|
||||||
return new Expression<Boolean>() {
|
return () -> value;
|
||||||
@Override
|
|
||||||
public Boolean execute() {
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -242,12 +233,7 @@ public class ProgramFactory implements IProgramFactory<Expression, Statement, Pr
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public Expression createNullExpression(SourceLocation location) throws ModelException {
|
public Expression createNullExpression(SourceLocation location) throws ModelException {
|
||||||
return new Expression<Object>() {
|
return () -> null;
|
||||||
@Override
|
|
||||||
public Object execute() {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -259,10 +245,10 @@ public class ProgramFactory implements IProgramFactory<Expression, Statement, Pr
|
|||||||
@Override
|
@Override
|
||||||
public Expression createSelfExpression(SourceLocation location) throws ModelException {
|
public Expression createSelfExpression(SourceLocation location) throws ModelException {
|
||||||
|
|
||||||
return new Expression<Worm>() {
|
return new ArgumentExpression<Worm, Worm>(ArgumentExpression.Type.WORM) {
|
||||||
@Override
|
@Override
|
||||||
public Worm execute() {
|
public Worm execute(Worm o) {
|
||||||
return getWorm();
|
return o;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -320,7 +306,7 @@ public class ProgramFactory implements IProgramFactory<Expression, Statement, Pr
|
|||||||
@Override
|
@Override
|
||||||
public Expression createEqualityExpression(Expression left, Expression right, SourceLocation location) throws ModelException {
|
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
|
@Override
|
||||||
public Expression createSearchObjectExpression(Expression angleDelta, SourceLocation sourceLocation) throws ModelException {
|
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
|
@Override
|
||||||
public Expression createSameTeamExpression(Expression entity, SourceLocation sourceLocation) throws ModelException, MustNotImplementException {
|
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
|
@Override
|
||||||
public Expression createDistanceExpression(Expression entity, SourceLocation sourceLocation) throws ModelException {
|
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
|
@Override
|
||||||
public Expression createIsWormExpression(Expression entity, SourceLocation sourceLocation) throws ModelException {
|
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;
|
package worms.programs;
|
||||||
|
|
||||||
import worms.model.Program;
|
public class Statement {
|
||||||
|
|
||||||
public class Statement extends Program {
|
|
||||||
|
|
||||||
private final Type type;
|
private final Type type;
|
||||||
private final Object data;
|
private final Object data;
|
||||||
|
@@ -2,7 +2,7 @@ package worms.programs;
|
|||||||
|
|
||||||
import java.util.function.Function;
|
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;
|
protected final Expression expression;
|
||||||
private final Function<T, R> function;
|
private final Function<T, R> function;
|
||||||
|
Reference in New Issue
Block a user