Skip to content

Commit

Permalink
Merge pull request #17 from FIUS/feature/tasks
Browse files Browse the repository at this point in the history
Add tasks
  • Loading branch information
neumantm authored Sep 18, 2019
2 parents 6a8edf0 + 4e00f36 commit 17f33e3
Show file tree
Hide file tree
Showing 11 changed files with 244 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;


Expand Down Expand Up @@ -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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;


Expand All @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -93,4 +100,9 @@ public EntityProgramRunner getEntityProgramRunner() {
return this.entityProgramRunner;
}

@Override
public TaskRunner getTaskRunner() {
return this.taskRunner;
}

}
Original file line number Diff line number Diff line change
@@ -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<? extends Task> 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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
Original file line number Diff line number Diff line change
@@ -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<? extends Task> taskToRun, Simulation sim);
}
1 change: 1 addition & 0 deletions ICGE-Simulation/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}

}
Original file line number Diff line number Diff line change
@@ -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;
}

}
2 changes: 1 addition & 1 deletion examples/mario/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 17f33e3

Please sign in to comment.