diff --git a/OGP1718-Worms/src/Main.java b/OGP1718-Worms/src/Main.java index 2c69ff7..1b59305 100644 --- a/OGP1718-Worms/src/Main.java +++ b/OGP1718-Worms/src/Main.java @@ -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)); } } diff --git a/OGP1718-Worms/src/worms/model/GameObject.java b/OGP1718-Worms/src/worms/model/GameObject.java index 81eae80..0c8707f 100644 --- a/OGP1718-Worms/src/worms/model/GameObject.java +++ b/OGP1718-Worms/src/worms/model/GameObject.java @@ -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)); + } } \ No newline at end of file diff --git a/OGP1718-Worms/src/worms/model/Program.java b/OGP1718-Worms/src/worms/model/Program.java index 8e7574d..44c009a 100644 --- a/OGP1718-Worms/src/worms/model/Program.java +++ b/OGP1718-Worms/src/worms/model/Program.java @@ -1,4 +1,15 @@ package worms.model; public class Program { + + + public Worm getWorm() { + + return null; + } + + protected Object findVar(String variableName) { + + return null; + } } diff --git a/OGP1718-Worms/src/worms/model/Team.java b/OGP1718-Worms/src/worms/model/Team.java index 651889e..9138f85 100644 --- a/OGP1718-Worms/src/worms/model/Team.java +++ b/OGP1718-Worms/src/worms/model/Team.java @@ -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 //=================================================================================== /** diff --git a/OGP1718-Worms/src/worms/programs/BinaryExpression.java b/OGP1718-Worms/src/worms/programs/BinaryExpression.java index 40118e7..a18d44b 100644 --- a/OGP1718-Worms/src/worms/programs/BinaryExpression.java +++ b/OGP1718-Worms/src/worms/programs/BinaryExpression.java @@ -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 extends Expression { - protected final Object left; - protected final Object right; + private final Expression left; + private final Expression right; + private final BiFunction function; - public BinaryExpression(Object left, Object right) { + public BinaryExpression(Expression left, Expression right, BiFunction 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) left).execute(), ((Expression) right).execute()); + } catch (ClassCastException e) { + throw new IllegalArgumentException(); + } + } } diff --git a/OGP1718-Worms/src/worms/programs/Expression.java b/OGP1718-Worms/src/worms/programs/Expression.java index 44f106d..733e272 100644 --- a/OGP1718-Worms/src/worms/programs/Expression.java +++ b/OGP1718-Worms/src/worms/programs/Expression.java @@ -1,7 +1,8 @@ package worms.programs; -public interface Expression { +import worms.model.Program; - T execute(); +public abstract class Expression extends Program { + public abstract T execute(); } diff --git a/OGP1718-Worms/src/worms/programs/Procedure.java b/OGP1718-Worms/src/worms/programs/Procedure.java new file mode 100644 index 0000000..6e0a9f2 --- /dev/null +++ b/OGP1718-Worms/src/worms/programs/Procedure.java @@ -0,0 +1,6 @@ +package worms.programs; + +import worms.model.Program; + +public abstract class Procedure extends Program { +} diff --git a/OGP1718-Worms/src/worms/programs/ProgramFactory.java b/OGP1718-Worms/src/worms/programs/ProgramFactory.java index 2b6ea58..f46b834 100644 --- a/OGP1718-Worms/src/worms/programs/ProgramFactory.java +++ b/OGP1718-Worms/src/worms/programs/ProgramFactory.java @@ -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 { /** * 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 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 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() { + @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() { + @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() { + @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() { + @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(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(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(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(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(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(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(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); } } diff --git a/OGP1718-Worms/src/worms/programs/Statement.java b/OGP1718-Worms/src/worms/programs/Statement.java new file mode 100644 index 0000000..49a2a30 --- /dev/null +++ b/OGP1718-Worms/src/worms/programs/Statement.java @@ -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 + } +} diff --git a/OGP1718-Worms/src/worms/programs/UnaryExpression.java b/OGP1718-Worms/src/worms/programs/UnaryExpression.java new file mode 100644 index 0000000..47153ee --- /dev/null +++ b/OGP1718-Worms/src/worms/programs/UnaryExpression.java @@ -0,0 +1,25 @@ +package worms.programs; + +import java.util.function.Function; + +public class UnaryExpression extends Expression { + + protected final Expression expression; + private final Function function; + + public UnaryExpression(Expression expression, Function unaryOperator) { + this.expression = expression; + this.function = unaryOperator; + } + + @Override + @SuppressWarnings("unchecked") + public R execute() { + + try { + return function.apply(((Expression) expression).execute()); + } catch (ClassCastException e) { + throw new IllegalArgumentException(); + } + } +} diff --git a/docs/TODO.md b/docs/TODO.md new file mode 100644 index 0000000..a97fbcc --- /dev/null +++ b/docs/TODO.md @@ -0,0 +1,2 @@ +#### Rename TeamComparator +Dit heeft letterlijk niks met team te maken behalve dat het daar gebruikt wordt... \ No newline at end of file diff --git a/docs/TODO.txt b/docs/TODO.txt deleted file mode 100644 index e69de29..0000000