diff --git a/ICGE-Simulation/src/main/java/de/unistuttgart/informatik/fius/icge/simulation/Simulation.java b/ICGE-Simulation/src/main/java/de/unistuttgart/informatik/fius/icge/simulation/Simulation.java index 736866a56..e516cefc9 100644 --- a/ICGE-Simulation/src/main/java/de/unistuttgart/informatik/fius/icge/simulation/Simulation.java +++ b/ICGE-Simulation/src/main/java/de/unistuttgart/informatik/fius/icge/simulation/Simulation.java @@ -11,6 +11,7 @@ import de.unistuttgart.informatik.fius.icge.simulation.entity.program.EntityProgramRegistry; import de.unistuttgart.informatik.fius.icge.simulation.entity.program.EntityProgramRunner; +import de.unistuttgart.informatik.fius.icge.simulation.tasks.TaskRunner; import de.unistuttgart.informatik.fius.icge.ui.UiManager; @@ -55,6 +56,13 @@ public interface Simulation { */ EntityProgramRunner getEntityProgramRunner(); + /** + * Get the task runner for this simulation. + * + * @return the task runner used by this simulation + */ + TaskRunner getTaskRunner(); + /** * Initialize the simulation and all its submodules. */ diff --git a/ICGE-Simulation/src/main/java/de/unistuttgart/informatik/fius/icge/simulation/SimulationFactory.java b/ICGE-Simulation/src/main/java/de/unistuttgart/informatik/fius/icge/simulation/SimulationFactory.java index 3487cf246..db34bee9f 100644 --- a/ICGE-Simulation/src/main/java/de/unistuttgart/informatik/fius/icge/simulation/SimulationFactory.java +++ b/ICGE-Simulation/src/main/java/de/unistuttgart/informatik/fius/icge/simulation/SimulationFactory.java @@ -14,6 +14,7 @@ import de.unistuttgart.informatik.fius.icge.simulation.internal.entity.program.StandardEntityProgramRegistry; import de.unistuttgart.informatik.fius.icge.simulation.internal.entity.program.StandardEntityProgramRunner; import de.unistuttgart.informatik.fius.icge.simulation.internal.playfield.StandardPlayfield; +import de.unistuttgart.informatik.fius.icge.simulation.internal.tasks.StandardTaskRunner; import de.unistuttgart.informatik.fius.icge.ui.UiManager; import de.unistuttgart.informatik.fius.icge.ui.UiManagerFactory; @@ -36,7 +37,8 @@ public static Simulation createSimulation() { StandardSimulationClock tickManager = new StandardSimulationClock(); StandardEntityProgramRegistry entityProgramRegistry = new StandardEntityProgramRegistry(); StandardEntityProgramRunner entityProgramRunner = new StandardEntityProgramRunner(entityProgramRegistry); + StandardTaskRunner taskRunner = new StandardTaskRunner(); - return new StandardSimulation(uiManager, playfield, tickManager, entityProgramRegistry, entityProgramRunner); + return new StandardSimulation(uiManager, playfield, tickManager, entityProgramRegistry, entityProgramRunner, taskRunner); } } diff --git a/ICGE-Simulation/src/main/java/de/unistuttgart/informatik/fius/icge/simulation/internal/StandardSimulation.java b/ICGE-Simulation/src/main/java/de/unistuttgart/informatik/fius/icge/simulation/internal/StandardSimulation.java index 34f5d5192..38ae01ab9 100644 --- a/ICGE-Simulation/src/main/java/de/unistuttgart/informatik/fius/icge/simulation/internal/StandardSimulation.java +++ b/ICGE-Simulation/src/main/java/de/unistuttgart/informatik/fius/icge/simulation/internal/StandardSimulation.java @@ -17,6 +17,8 @@ import de.unistuttgart.informatik.fius.icge.simulation.internal.entity.program.StandardEntityProgramRegistry; import de.unistuttgart.informatik.fius.icge.simulation.internal.entity.program.StandardEntityProgramRunner; import de.unistuttgart.informatik.fius.icge.simulation.internal.playfield.StandardPlayfield; +import de.unistuttgart.informatik.fius.icge.simulation.internal.tasks.StandardTaskRunner; +import de.unistuttgart.informatik.fius.icge.simulation.tasks.TaskRunner; import de.unistuttgart.informatik.fius.icge.ui.UiManager; @@ -32,6 +34,7 @@ public class StandardSimulation implements Simulation { private final StandardSimulationClock tickManager; private final StandardEntityProgramRegistry entityProgramRegistry; private final StandardEntityProgramRunner entityProgramRunner; + private final StandardTaskRunner taskRunner; /** * Creates a new standard simulation with the given parameters. @@ -46,16 +49,20 @@ public class StandardSimulation implements Simulation { * The entityProgramRegistry to use * @param entityProgramRunner * The entityProgramRunner to use + * @param taskRunner + * The taskRunner to use */ public StandardSimulation( UiManager uiManager, StandardPlayfield playfield, StandardSimulationClock tickManager, - StandardEntityProgramRegistry entityProgramRegistry, StandardEntityProgramRunner entityProgramRunner + StandardEntityProgramRegistry entityProgramRegistry, StandardEntityProgramRunner entityProgramRunner, + StandardTaskRunner taskRunner ) { this.uiManager = uiManager; this.playfield = playfield; this.tickManager = tickManager; this.entityProgramRegistry = entityProgramRegistry; this.entityProgramRunner = entityProgramRunner; + this.taskRunner = taskRunner; } @Override @@ -93,4 +100,9 @@ public EntityProgramRunner getEntityProgramRunner() { return this.entityProgramRunner; } + @Override + public TaskRunner getTaskRunner() { + return this.taskRunner; + } + } diff --git a/ICGE-Simulation/src/main/java/de/unistuttgart/informatik/fius/icge/simulation/internal/tasks/StandardTaskRunner.java b/ICGE-Simulation/src/main/java/de/unistuttgart/informatik/fius/icge/simulation/internal/tasks/StandardTaskRunner.java new file mode 100644 index 000000000..d6dadd756 --- /dev/null +++ b/ICGE-Simulation/src/main/java/de/unistuttgart/informatik/fius/icge/simulation/internal/tasks/StandardTaskRunner.java @@ -0,0 +1,42 @@ +/* + * This source file is part of the FIUS ICGE project. + * For more information see github.com/FIUS/ICGE2 + * + * Copyright (c) 2019 the ICGE project authors. + * + * This software is available under the MIT license. + * SPDX-License-Identifier: MIT + */ +package de.unistuttgart.informatik.fius.icge.simulation.internal.tasks; + +import java.lang.reflect.InvocationTargetException; + +import de.unistuttgart.informatik.fius.icge.simulation.Simulation; +import de.unistuttgart.informatik.fius.icge.simulation.tasks.Task; +import de.unistuttgart.informatik.fius.icge.simulation.tasks.TaskRunner; + + +/** + * The standard implementation of {@link TaskRunner}. + * + * @author Tim Neumann + */ +public class StandardTaskRunner implements TaskRunner { + + @Override + public boolean runTask(Class taskToRun, Simulation sim) { + if (taskToRun == null || sim == null) throw new IllegalArgumentException("Argument is null."); + + try { + Task task = taskToRun.getDeclaredConstructor().newInstance(); + task.prepare(sim); + task.solve(); + return task.verify(); + } catch ( + InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException + | NoSuchMethodException | SecurityException e + ) { + throw new IllegalArgumentException("Failed to instantiate.", e); + } + } +} diff --git a/ICGE-Simulation/src/main/java/de/unistuttgart/informatik/fius/icge/simulation/tasks/Task.java b/ICGE-Simulation/src/main/java/de/unistuttgart/informatik/fius/icge/simulation/tasks/Task.java new file mode 100644 index 000000000..44c4e568d --- /dev/null +++ b/ICGE-Simulation/src/main/java/de/unistuttgart/informatik/fius/icge/simulation/tasks/Task.java @@ -0,0 +1,40 @@ +/* + * This source file is part of the FIUS ICGE project. + * For more information see github.com/FIUS/ICGE2 + * + * Copyright (c) 2019 the ICGE project authors. + * + * This software is available under the MIT license. + * SPDX-License-Identifier: MIT + */ +package de.unistuttgart.informatik.fius.icge.simulation.tasks; + +import de.unistuttgart.informatik.fius.icge.simulation.Simulation; + + +/** + * The interface for a task to be solved by students. + * + * @author Tim Neumann + */ +public interface Task { + /** + * Prepare the simulation for this task + * + * @param sim + * The simulation this task should be prepared in + */ + void prepare(Simulation sim); + + /** + * Solve the task + */ + void solve(); + + /** + * Verify that the task was solved correctly. + * + * @return true if the task was solved correctly + */ + boolean verify(); +} diff --git a/ICGE-Simulation/src/main/java/de/unistuttgart/informatik/fius/icge/simulation/tasks/TaskRunner.java b/ICGE-Simulation/src/main/java/de/unistuttgart/informatik/fius/icge/simulation/tasks/TaskRunner.java new file mode 100644 index 000000000..cc606b7b9 --- /dev/null +++ b/ICGE-Simulation/src/main/java/de/unistuttgart/informatik/fius/icge/simulation/tasks/TaskRunner.java @@ -0,0 +1,33 @@ +/* + * This source file is part of the FIUS ICGE project. + * For more information see github.com/FIUS/ICGE2 + * + * Copyright (c) 2019 the ICGE project authors. + * + * This software is available under the MIT license. + * SPDX-License-Identifier: MIT + */ +package de.unistuttgart.informatik.fius.icge.simulation.tasks; + +import de.unistuttgart.informatik.fius.icge.simulation.Simulation; + + +/** + * The interface + * + * @author Tim Neumann + */ +public interface TaskRunner { + /** + * Run the given task and verify the solution. + * + * @param taskToRun + * The task to run; must not be null; must be possible to be instantiated without an argument + * @param sim + * The simulation to run the task in; must not be null + * @return true if the task was completed successfully and the solution could be verified + * @throws IllegalArgumentException + * if the argument is null or an error occurs during instantiation + */ + boolean runTask(Class taskToRun, Simulation sim); +} diff --git a/ICGE-Simulation/src/main/java/module-info.java b/ICGE-Simulation/src/main/java/module-info.java index 04bbddab4..0d929cfd4 100644 --- a/ICGE-Simulation/src/main/java/module-info.java +++ b/ICGE-Simulation/src/main/java/module-info.java @@ -5,4 +5,5 @@ exports de.unistuttgart.informatik.fius.icge.simulation.entity; exports de.unistuttgart.informatik.fius.icge.simulation.entity.program; exports de.unistuttgart.informatik.fius.icge.simulation.exception; + exports de.unistuttgart.informatik.fius.icge.simulation.tasks; } diff --git a/examples/mario/src/main/java/de/unistuttgart/informatik/fius/icge/example/mario/Main.java b/examples/mario/src/main/java/de/unistuttgart/informatik/fius/icge/example/mario/Main.java index 7e74cd2dc..bbbe41578 100644 --- a/examples/mario/src/main/java/de/unistuttgart/informatik/fius/icge/example/mario/Main.java +++ b/examples/mario/src/main/java/de/unistuttgart/informatik/fius/icge/example/mario/Main.java @@ -9,9 +9,7 @@ */ package de.unistuttgart.informatik.fius.icge.example.mario; -import de.unistuttgart.informatik.fius.icge.example.mario.entity.Mario; -import de.unistuttgart.informatik.fius.icge.example.mario.entity.Wall; -import de.unistuttgart.informatik.fius.icge.simulation.Position; +import de.unistuttgart.informatik.fius.icge.example.mario.tasks.Solution1; import de.unistuttgart.informatik.fius.icge.simulation.Simulation; import de.unistuttgart.informatik.fius.icge.simulation.SimulationFactory; import de.unistuttgart.informatik.fius.icge.ui.TextureRegistry; @@ -35,32 +33,8 @@ public static void main(String[] args) { prepareUiManager(sim.getUiManager()); sim.initialize(); - sim.getPlayfield().addEntity(new Position(-3, -1), new Wall()); - sim.getPlayfield().addEntity(new Position(-3, 0), new Wall()); - sim.getPlayfield().addEntity(new Position(-3, 1), new Wall()); - sim.getPlayfield().addEntity(new Position(3, -1), new Wall()); - sim.getPlayfield().addEntity(new Position(3, 0), new Wall()); - sim.getPlayfield().addEntity(new Position(3, 1), new Wall()); - - Mario walkingMario = new Mario(); - - Mario spinningMario = new Mario(); - - sim.getPlayfield().addEntity(new Position(-1, 0), walkingMario); - sim.getPlayfield().addEntity(new Position(0, 0), spinningMario); - - String walkingProgramName = "Walking"; - - sim.getEntityProgramRegistry().registerEntityProgram(walkingProgramName, new WalkingProgram()); - - sim.getSimulationClock().start(); - - sim.getEntityProgramRunner().run(walkingProgramName, walkingMario); - - while (true) { - spinningMario.turnClockWise(); - } + sim.getTaskRunner().runTask(Solution1.class, sim); } private static void prepareUiManager(UiManager manager) { diff --git a/examples/mario/src/main/java/de/unistuttgart/informatik/fius/icge/example/mario/tasks/Solution1.java b/examples/mario/src/main/java/de/unistuttgart/informatik/fius/icge/example/mario/tasks/Solution1.java new file mode 100644 index 000000000..f9ee5d2ca --- /dev/null +++ b/examples/mario/src/main/java/de/unistuttgart/informatik/fius/icge/example/mario/tasks/Solution1.java @@ -0,0 +1,37 @@ +/* + * This source file is part of the FIUS ICGE project. + * For more information see github.com/FIUS/ICGE2 + * + * Copyright (c) 2019 the ICGE project authors. + * + * This software is available under the MIT license. + * SPDX-License-Identifier: MIT + */ +package de.unistuttgart.informatik.fius.icge.example.mario.tasks; + +import de.unistuttgart.informatik.fius.icge.example.mario.WalkingProgram; + + +/** + * The example solution for Task1 + * + * @author Tim Neumann + */ +public class Solution1 extends Task1 { + + @Override + public void solve() { + String walkingProgramName = "Walking"; + + this.sim.getEntityProgramRegistry().registerEntityProgram(walkingProgramName, new WalkingProgram()); + + this.sim.getSimulationClock().start(); //This can be done via UI in the future. + + this.sim.getEntityProgramRunner().run(walkingProgramName, this.walkingMario); + + while (true) { + this.spinningMario.turnClockWise(); + } + } + +} diff --git a/examples/mario/src/main/java/de/unistuttgart/informatik/fius/icge/example/mario/tasks/Task1.java b/examples/mario/src/main/java/de/unistuttgart/informatik/fius/icge/example/mario/tasks/Task1.java new file mode 100644 index 000000000..89f7907d2 --- /dev/null +++ b/examples/mario/src/main/java/de/unistuttgart/informatik/fius/icge/example/mario/tasks/Task1.java @@ -0,0 +1,64 @@ +/* + * This source file is part of the FIUS ICGE project. + * For more information see github.com/FIUS/ICGE2 + * + * Copyright (c) 2019 the ICGE project authors. + * + * This software is available under the MIT license. + * SPDX-License-Identifier: MIT + */ +package de.unistuttgart.informatik.fius.icge.example.mario.tasks; + +import de.unistuttgart.informatik.fius.icge.example.mario.entity.Mario; +import de.unistuttgart.informatik.fius.icge.example.mario.entity.Wall; +import de.unistuttgart.informatik.fius.icge.simulation.Position; +import de.unistuttgart.informatik.fius.icge.simulation.Simulation; +import de.unistuttgart.informatik.fius.icge.simulation.tasks.Task; + + +/** + * An example task + * + * @author Tim Neumann + */ +public abstract class Task1 implements Task { + + /** + * the simulation + */ + Simulation sim; + /** + * The walking mario + */ + Mario walkingMario; + /** + * The spinning mario + */ + Mario spinningMario; + + @Override + public void prepare(Simulation sim) { + this.sim = sim; + sim.getPlayfield().addEntity(new Position(-3, -1), new Wall()); + sim.getPlayfield().addEntity(new Position(-3, 0), new Wall()); + sim.getPlayfield().addEntity(new Position(-3, 1), new Wall()); + sim.getPlayfield().addEntity(new Position(3, -1), new Wall()); + sim.getPlayfield().addEntity(new Position(3, 0), new Wall()); + sim.getPlayfield().addEntity(new Position(3, 1), new Wall()); + + this.walkingMario = new Mario(); + + this.spinningMario = new Mario(); + + sim.getPlayfield().addEntity(new Position(-1, 0), this.walkingMario); + sim.getPlayfield().addEntity(new Position(0, 0), this.spinningMario); + + } + + @Override + public boolean verify() { + // TODO + return true; + } + +} diff --git a/examples/mario/src/main/java/module-info.java b/examples/mario/src/main/java/module-info.java index 05e22d6d9..dcdd59709 100644 --- a/examples/mario/src/main/java/module-info.java +++ b/examples/mario/src/main/java/module-info.java @@ -1,3 +1,3 @@ -module de.unistuttgart.informatik.fius.icge.example.mario { +open module de.unistuttgart.informatik.fius.icge.example.mario { requires de.unistuttgart.informatik.fius.icge.simulation; }