improved Program
This commit is contained in:
@@ -3,9 +3,14 @@ import worms.facade.IFacade;
|
||||
import worms.model.Team;
|
||||
import worms.model.World;
|
||||
import worms.model.Worm;
|
||||
import worms.programs.BinaryExpression;
|
||||
import worms.programs.Expression;
|
||||
import worms.programs.UnaryExpression;
|
||||
import worms.util.Coordinate;
|
||||
|
||||
import java.io.Console;
|
||||
import java.util.function.BinaryOperator;
|
||||
import java.util.function.DoubleBinaryOperator;
|
||||
|
||||
public class Main {
|
||||
private static final double EPS = 1e-4;
|
||||
@@ -16,12 +21,28 @@ public class Main {
|
||||
{ false, false, false, false } };
|
||||
|
||||
public static void main(String[] args) {
|
||||
//
|
||||
// IFacade facade = new Facade();
|
||||
// World world = facade.createWorld(4.0, 4.0, passableMap);
|
||||
//
|
||||
// Worm worm = facade.createWorm(world, new double[] { 1, 1.5 }, Math.PI / 2, 0.5, "Test", null);
|
||||
// System.out.println(world.isPassable(worm.getLocationArray(), worm.getRadius()));
|
||||
|
||||
IFacade facade = new Facade();
|
||||
World world = facade.createWorld(4.0, 4.0, passableMap);
|
||||
|
||||
Worm worm = facade.createWorm(world, new double[] { 1, 1.5 }, Math.PI / 2, 0.5, "Test", null);
|
||||
System.out.println(world.isPassable(worm.getLocationArray(), worm.getRadius()));
|
||||
Expression test = new Expression() {
|
||||
@Override
|
||||
public Object execute() {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
private static Object l = false;
|
||||
private static Object r = true;
|
||||
|
||||
public static void test(BinaryOperator test) {
|
||||
System.out.println(test.apply(l,r));
|
||||
}
|
||||
}
|
||||
|
@@ -124,7 +124,7 @@ public abstract class GameObject {
|
||||
* the location of the worm expresses the place of the game object
|
||||
* in the play area
|
||||
*/
|
||||
Coordinate getLocation() {
|
||||
public Coordinate getLocation() {
|
||||
return this.location;
|
||||
}
|
||||
|
||||
@@ -243,4 +243,12 @@ public abstract class GameObject {
|
||||
protected void setMass(double radius, double rho) {
|
||||
this.mass = (double) round(rho * (4.0 / 3.0 * PI * pow(radius, 3)));
|
||||
}
|
||||
|
||||
|
||||
public double getDistance(GameObject o) {
|
||||
|
||||
Coordinate otherLocation = o.getLocation();
|
||||
|
||||
return Math.sqrt(Math.pow((otherLocation.getX() - location.getX()), 2) + Math.pow((otherLocation.getY() - location.getY()), 2));
|
||||
}
|
||||
}
|
@@ -1,4 +1,15 @@
|
||||
package worms.model;
|
||||
|
||||
public class Program {
|
||||
|
||||
|
||||
public Worm getWorm() {
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
protected Object findVar(String variableName) {
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
@@ -6,7 +6,9 @@ import worms.util.IllegalNameException;
|
||||
import worms.util.TeamComparator;
|
||||
|
||||
public class Team {
|
||||
// region constructor
|
||||
|
||||
|
||||
// region constructor
|
||||
//===================================================================================
|
||||
|
||||
/**
|
||||
@@ -25,8 +27,16 @@ public class Team {
|
||||
|
||||
//===================================================================================
|
||||
// endregion
|
||||
|
||||
// region changesTeam
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (!(obj instanceof Team)) return false;
|
||||
|
||||
return this.getName().equals(((Team) obj).getName());
|
||||
}
|
||||
|
||||
|
||||
// region changesTeam
|
||||
//===================================================================================
|
||||
|
||||
/**
|
||||
|
@@ -1,20 +1,27 @@
|
||||
package worms.programs;
|
||||
|
||||
import java.util.function.BinaryOperator;
|
||||
import java.util.function.DoubleBinaryOperator;
|
||||
import java.util.function.BiFunction;
|
||||
|
||||
public abstract class BinaryExpression implements Expression {
|
||||
public class BinaryExpression<T, R> extends Expression {
|
||||
|
||||
protected final Object left;
|
||||
protected final Object right;
|
||||
private final Expression left;
|
||||
private final Expression right;
|
||||
private final BiFunction<T, T, R> function;
|
||||
|
||||
public BinaryExpression(Object left, Object right) {
|
||||
public BinaryExpression(Expression left, Expression right, BiFunction<T, T, R> function) {
|
||||
this.left = left;
|
||||
this.right = right;
|
||||
this.function = function;
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
protected Object execute(BinaryOperator op) {
|
||||
return op.apply(this.left, this.right);
|
||||
public R execute() {
|
||||
try {
|
||||
return function.apply(((Expression<T>) left).execute(), ((Expression<T>) right).execute());
|
||||
} catch (ClassCastException e) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@@ -1,7 +1,8 @@
|
||||
package worms.programs;
|
||||
|
||||
public interface Expression<T> {
|
||||
import worms.model.Program;
|
||||
|
||||
T execute();
|
||||
public abstract class Expression<T> extends Program {
|
||||
|
||||
public abstract T execute();
|
||||
}
|
||||
|
6
OGP1718-Worms/src/worms/programs/Procedure.java
Normal file
6
OGP1718-Worms/src/worms/programs/Procedure.java
Normal file
@@ -0,0 +1,6 @@
|
||||
package worms.programs;
|
||||
|
||||
import worms.model.Program;
|
||||
|
||||
public abstract class Procedure extends Program {
|
||||
}
|
@@ -1,14 +1,17 @@
|
||||
package worms.programs;
|
||||
|
||||
import com.sun.org.apache.xpath.internal.operations.Bool;
|
||||
import worms.model.GameObject;
|
||||
import worms.model.Program;
|
||||
import worms.model.Worm;
|
||||
import worms.util.ModelException;
|
||||
import worms.util.MustNotImplementException;
|
||||
import worms.programs.Expression;
|
||||
|
||||
import javax.swing.plaf.nimbus.State;
|
||||
import java.util.List;
|
||||
|
||||
public class ProgramFactory implements IProgramFactory {
|
||||
public class ProgramFactory implements IProgramFactory<Expression, Statement, Procedure, Program> {
|
||||
/**
|
||||
* Create a program from the given arguments.
|
||||
*
|
||||
@@ -18,7 +21,7 @@ public class ProgramFactory implements IProgramFactory {
|
||||
* @return A new program.
|
||||
*/
|
||||
@Override
|
||||
public Object createProgram(List procs, Object main) throws ModelException {
|
||||
public Program createProgram(List<Procedure> procs, Statement main) throws ModelException {
|
||||
System.out.println("create program");
|
||||
return new Program();
|
||||
}
|
||||
@@ -44,7 +47,7 @@ public class ProgramFactory implements IProgramFactory {
|
||||
* @param sourceLocation
|
||||
*/
|
||||
@Override
|
||||
public Object createProcedureDefinition(String procedureName, Object body, SourceLocation sourceLocation) {
|
||||
public Procedure createProcedureDefinition(String procedureName, Statement body, SourceLocation sourceLocation) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -57,7 +60,7 @@ public class ProgramFactory implements IProgramFactory {
|
||||
* @param sourceLocation
|
||||
*/
|
||||
@Override
|
||||
public Object createAssignmentStatement(String variableName, Object value, SourceLocation sourceLocation) throws ModelException {
|
||||
public Statement createAssignmentStatement(String variableName, Expression value, SourceLocation sourceLocation) throws ModelException {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -69,7 +72,7 @@ public class ProgramFactory implements IProgramFactory {
|
||||
* @param sourceLocation
|
||||
*/
|
||||
@Override
|
||||
public Object createPrintStatement(Object value, SourceLocation sourceLocation) throws ModelException {
|
||||
public Statement createPrintStatement(Expression value, SourceLocation sourceLocation) throws ModelException {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -81,7 +84,7 @@ public class ProgramFactory implements IProgramFactory {
|
||||
* @param location
|
||||
*/
|
||||
@Override
|
||||
public Object createTurnStatement(Object angle, SourceLocation location) throws ModelException {
|
||||
public Statement createTurnStatement(Expression angle, SourceLocation location) throws ModelException {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -92,7 +95,7 @@ public class ProgramFactory implements IProgramFactory {
|
||||
* @param location
|
||||
*/
|
||||
@Override
|
||||
public Object createMoveStatement(SourceLocation location) throws ModelException {
|
||||
public Statement createMoveStatement(SourceLocation location) throws ModelException {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -102,7 +105,7 @@ public class ProgramFactory implements IProgramFactory {
|
||||
* @param location
|
||||
*/
|
||||
@Override
|
||||
public Object createJumpStatement(SourceLocation location) throws ModelException {
|
||||
public Statement createJumpStatement(SourceLocation location) throws ModelException {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -112,7 +115,7 @@ public class ProgramFactory implements IProgramFactory {
|
||||
* @param location
|
||||
*/
|
||||
@Override
|
||||
public Object createEatStatement(SourceLocation location) {
|
||||
public Statement createEatStatement(SourceLocation location) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -123,7 +126,7 @@ public class ProgramFactory implements IProgramFactory {
|
||||
* @param location
|
||||
*/
|
||||
@Override
|
||||
public Object createFireStatement(SourceLocation location) throws ModelException {
|
||||
public Statement createFireStatement(SourceLocation location) throws ModelException {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -134,7 +137,7 @@ public class ProgramFactory implements IProgramFactory {
|
||||
* @param sourceLocation
|
||||
*/
|
||||
@Override
|
||||
public Object createSequenceStatement(List statements, SourceLocation sourceLocation) throws ModelException {
|
||||
public Statement createSequenceStatement(List<Statement> statements, SourceLocation sourceLocation) throws ModelException {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -149,7 +152,7 @@ public class ProgramFactory implements IProgramFactory {
|
||||
* @param sourceLocation
|
||||
*/
|
||||
@Override
|
||||
public Object createIfStatement(Object condition, Object ifBody, Object elseBody, SourceLocation sourceLocation) throws ModelException {
|
||||
public Statement createIfStatement(Expression condition, Statement ifBody, Statement elseBody, SourceLocation sourceLocation) throws ModelException {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -161,7 +164,7 @@ public class ProgramFactory implements IProgramFactory {
|
||||
* @param sourceLocation
|
||||
*/
|
||||
@Override
|
||||
public Object createWhileStatement(Object condition, Object body, SourceLocation sourceLocation) throws ModelException {
|
||||
public Statement createWhileStatement(Expression condition, Statement body, SourceLocation sourceLocation) throws ModelException {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -171,7 +174,7 @@ public class ProgramFactory implements IProgramFactory {
|
||||
* @param sourceLocation
|
||||
*/
|
||||
@Override
|
||||
public Object createBreakStatement(SourceLocation sourceLocation) throws ModelException, MustNotImplementException {
|
||||
public Statement createBreakStatement(SourceLocation sourceLocation) throws ModelException, MustNotImplementException {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -182,7 +185,7 @@ public class ProgramFactory implements IProgramFactory {
|
||||
* @param sourceLocation
|
||||
*/
|
||||
@Override
|
||||
public Object createInvokeStatement(String procedureName, SourceLocation sourceLocation) throws ModelException, MustNotImplementException {
|
||||
public Statement createInvokeStatement(String procedureName, SourceLocation sourceLocation) throws ModelException, MustNotImplementException {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -194,8 +197,13 @@ public class ProgramFactory implements IProgramFactory {
|
||||
* @param sourceLocation
|
||||
*/
|
||||
@Override
|
||||
public Object createReadVariableExpression(String variableName, SourceLocation sourceLocation) throws ModelException {
|
||||
return null;
|
||||
public Expression createReadVariableExpression(String variableName, SourceLocation sourceLocation) throws ModelException {
|
||||
return new Expression() {
|
||||
@Override
|
||||
public Object execute() {
|
||||
return findVar(variableName);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -205,8 +213,13 @@ public class ProgramFactory implements IProgramFactory {
|
||||
* @param location
|
||||
*/
|
||||
@Override
|
||||
public Object createDoubleLiteralExpression(double value, SourceLocation location) throws ModelException {
|
||||
return null;
|
||||
public Expression createDoubleLiteralExpression(double value, SourceLocation location) throws ModelException {
|
||||
return new Expression<Double>() {
|
||||
@Override
|
||||
public Double execute() {
|
||||
return value;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -216,8 +229,13 @@ public class ProgramFactory implements IProgramFactory {
|
||||
* @param location
|
||||
*/
|
||||
@Override
|
||||
public Object createBooleanLiteralExpression(boolean value, SourceLocation location) throws ModelException {
|
||||
return null;
|
||||
public Expression createBooleanLiteralExpression(boolean value, SourceLocation location) throws ModelException {
|
||||
return new Expression<Boolean>() {
|
||||
@Override
|
||||
public Boolean execute() {
|
||||
return value;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -226,8 +244,13 @@ public class ProgramFactory implements IProgramFactory {
|
||||
* @param location
|
||||
*/
|
||||
@Override
|
||||
public Object createNullExpression(SourceLocation location) throws ModelException {
|
||||
return null;
|
||||
public Expression createNullExpression(SourceLocation location) throws ModelException {
|
||||
return new Expression<Object>() {
|
||||
@Override
|
||||
public Object execute() {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -237,8 +260,14 @@ public class ProgramFactory implements IProgramFactory {
|
||||
* @param location
|
||||
*/
|
||||
@Override
|
||||
public Object createSelfExpression(SourceLocation location) throws ModelException {
|
||||
return null;
|
||||
public Expression createSelfExpression(SourceLocation location) throws ModelException {
|
||||
|
||||
return new Expression<Worm>() {
|
||||
@Override
|
||||
public Worm execute() {
|
||||
return getWorm();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -250,18 +279,9 @@ public class ProgramFactory implements IProgramFactory {
|
||||
* @param location
|
||||
*/
|
||||
@Override
|
||||
public Object createAdditionExpression(Object left, Object right, SourceLocation location) throws ModelException {
|
||||
public Expression createAdditionExpression(Expression left, Expression right, SourceLocation location) throws ModelException {
|
||||
|
||||
if (!(left instanceof Double && right instanceof Double)) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
|
||||
return new BinaryExpression(left, right) {
|
||||
@Override
|
||||
public Double execute() {
|
||||
return (double) execute((l,r) -> (double) l + (double) r);
|
||||
}
|
||||
};
|
||||
return new BinaryExpression<Double, Double>(left, right, (l,r) -> l + r);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -273,17 +293,9 @@ public class ProgramFactory implements IProgramFactory {
|
||||
* @param sourceLocation
|
||||
*/
|
||||
@Override
|
||||
public Object createAndExpression(Object left, Object right, SourceLocation sourceLocation) throws ModelException {
|
||||
if (!(left instanceof Boolean && right instanceof Boolean)) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
public Expression createAndExpression(Expression left, Expression right, SourceLocation sourceLocation) throws ModelException {
|
||||
|
||||
return new BinaryExpression(left, right) {
|
||||
@Override
|
||||
public Boolean execute() {
|
||||
return (boolean) execute((l,r) -> (boolean) left && (boolean) right);
|
||||
}
|
||||
};
|
||||
return new BinaryExpression<Boolean, Boolean>(left, right, (l, r) -> l && r);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -294,8 +306,9 @@ public class ProgramFactory implements IProgramFactory {
|
||||
* @param sourceLocation
|
||||
*/
|
||||
@Override
|
||||
public Object createNotExpression(Object expression, SourceLocation sourceLocation) throws ModelException {
|
||||
return null;
|
||||
public Expression createNotExpression(Expression expression, SourceLocation sourceLocation) throws ModelException {
|
||||
|
||||
return new UnaryExpression<Boolean, Boolean>(expression, e -> !e);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -308,14 +321,9 @@ public class ProgramFactory implements IProgramFactory {
|
||||
* @param location
|
||||
*/
|
||||
@Override
|
||||
public Object createEqualityExpression(Object left, Object right, SourceLocation location) throws ModelException {
|
||||
public Expression createEqualityExpression(Expression left, Expression right, SourceLocation location) throws ModelException {
|
||||
|
||||
return new BinaryExpression(left, right) {
|
||||
@Override
|
||||
public Boolean execute() {
|
||||
return (boolean) execute((l,r) -> left.equals(right));
|
||||
}
|
||||
};
|
||||
return new BinaryExpression<Object, Boolean>(left, right, (l,r) -> l.equals(r));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -328,21 +336,13 @@ public class ProgramFactory implements IProgramFactory {
|
||||
* @param location
|
||||
*/
|
||||
@Override
|
||||
public Object createLessThanExpression(Object left, Object right, SourceLocation location) {
|
||||
if (!(left instanceof Double && right instanceof Double)) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
public Expression createLessThanExpression(Expression left, Expression right, SourceLocation location) {
|
||||
|
||||
return new BinaryExpression(left, right) {
|
||||
@Override
|
||||
public Boolean execute() {
|
||||
return (boolean) execute((l,r) -> (double) left < (double) right);
|
||||
}
|
||||
};
|
||||
return new BinaryExpression<Double, Boolean>(left, right, (l,r) -> l < r);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createSearchObjectExpression(Object angleDelta, SourceLocation sourceLocation) throws ModelException {
|
||||
public Expression createSearchObjectExpression(Expression angleDelta, SourceLocation sourceLocation) throws ModelException {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -355,8 +355,9 @@ public class ProgramFactory implements IProgramFactory {
|
||||
* @param sourceLocation
|
||||
*/
|
||||
@Override
|
||||
public Object createSameTeamExpression(Object entity, SourceLocation sourceLocation) throws ModelException, MustNotImplementException {
|
||||
return null;
|
||||
public Expression createSameTeamExpression(Expression entity, SourceLocation sourceLocation) throws ModelException, MustNotImplementException {
|
||||
|
||||
return new UnaryExpression<Worm, Boolean>(entity, w -> (w).getTeam().equals((w).getWorld().getActiveWorm().getTeam()));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -367,8 +368,9 @@ public class ProgramFactory implements IProgramFactory {
|
||||
* @param sourceLocation
|
||||
*/
|
||||
@Override
|
||||
public Object createDistanceExpression(Object entity, SourceLocation sourceLocation) throws ModelException {
|
||||
return null;
|
||||
public Expression createDistanceExpression(Expression entity, SourceLocation sourceLocation) throws ModelException {
|
||||
|
||||
return new UnaryExpression<GameObject, Double>(entity, (l) -> (l).getWorld().getActiveWorm().getDistance(l));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -379,7 +381,7 @@ public class ProgramFactory implements IProgramFactory {
|
||||
* @param sourceLocation
|
||||
*/
|
||||
@Override
|
||||
public Object createIsWormExpression(Object entity, SourceLocation sourceLocation) throws ModelException {
|
||||
return null;
|
||||
public Expression createIsWormExpression(Expression entity, SourceLocation sourceLocation) throws ModelException {
|
||||
return new UnaryExpression<>(entity, w -> w instanceof Worm);
|
||||
}
|
||||
}
|
||||
|
34
OGP1718-Worms/src/worms/programs/Statement.java
Normal file
34
OGP1718-Worms/src/worms/programs/Statement.java
Normal file
@@ -0,0 +1,34 @@
|
||||
package worms.programs;
|
||||
|
||||
import worms.model.Program;
|
||||
|
||||
public class Statement extends Program {
|
||||
|
||||
private final Type type;
|
||||
|
||||
public Statement(Type type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public Type getType() {
|
||||
return this.type;
|
||||
}
|
||||
|
||||
public enum Type {
|
||||
ASSIGN,
|
||||
PRINT,
|
||||
ACTION,
|
||||
WHILE,
|
||||
IF,
|
||||
BLOCK,
|
||||
INVOKE,
|
||||
BREAK
|
||||
}
|
||||
public enum Action {
|
||||
TURN,
|
||||
MOVE,
|
||||
JUMP,
|
||||
EAT,
|
||||
FIRE
|
||||
}
|
||||
}
|
25
OGP1718-Worms/src/worms/programs/UnaryExpression.java
Normal file
25
OGP1718-Worms/src/worms/programs/UnaryExpression.java
Normal file
@@ -0,0 +1,25 @@
|
||||
package worms.programs;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
public class UnaryExpression<T, R> extends Expression<R> {
|
||||
|
||||
protected final Expression expression;
|
||||
private final Function<T, R> function;
|
||||
|
||||
public UnaryExpression(Expression expression, Function<T, R> unaryOperator) {
|
||||
this.expression = expression;
|
||||
this.function = unaryOperator;
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public R execute() {
|
||||
|
||||
try {
|
||||
return function.apply(((Expression<T>) expression).execute());
|
||||
} catch (ClassCastException e) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
}
|
||||
}
|
2
docs/TODO.md
Normal file
2
docs/TODO.md
Normal file
@@ -0,0 +1,2 @@
|
||||
#### Rename TeamComparator
|
||||
Dit heeft letterlijk niks met team te maken behalve dat het daar gebruikt wordt...
|
Reference in New Issue
Block a user