|
| 1 | +package de.unistuttgart.informatik.fius.jvk.verifier; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | +import java.util.Iterator; |
| 6 | +import java.util.stream.Collectors; |
| 7 | + |
| 8 | +import de.unistuttgart.informatik.fius.icge.simulation.Position; |
| 9 | +import de.unistuttgart.informatik.fius.icge.simulation.Simulation; |
| 10 | +import de.unistuttgart.informatik.fius.icge.simulation.TaskVerifier; |
| 11 | +import de.unistuttgart.informatik.fius.icge.simulation.actions.*; |
| 12 | +import de.unistuttgart.informatik.fius.icge.ui.TaskInformation; |
| 13 | +import de.unistuttgart.informatik.fius.icge.ui.TaskVerificationStatus; |
| 14 | +import de.unistuttgart.informatik.fius.jvk.provided.BasicTaskInformation; |
| 15 | +import de.unistuttgart.informatik.fius.jvk.provided.entity.Nut; |
| 16 | + |
| 17 | + |
| 18 | +public class Sheet2Task7Verifier implements TaskVerifier { |
| 19 | + |
| 20 | + private BasicTaskInformation task; |
| 21 | + |
| 22 | + private BasicTaskInformation taskB = new BasicTaskInformation("b) Find the differences between the totoros.", "Find the differences between the totoros.", TaskVerificationStatus.SUCCESSFUL); |
| 23 | + |
| 24 | + private ActionLog actionLog; |
| 25 | + |
| 26 | + public Sheet2Task7Verifier() { |
| 27 | + List<BasicTaskInformation> subTasks = new ArrayList<>(); |
| 28 | + subTasks.add(this.taskB); |
| 29 | + this.task = new BasicTaskInformation("Sheet 2 Task 7", "Learn how differentiate objects and classes", subTasks); |
| 30 | + } |
| 31 | + |
| 32 | + @Override |
| 33 | + public void attachToSimulation(Simulation sim) { |
| 34 | + this.actionLog = sim.getActionLog(); |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + public void verify() { |
| 39 | + System.out.println("Verify"); |
| 40 | + List<EntitySpawnAction> spawnActions = this.actionLog.getActionsOfType(EntitySpawnAction.class, true); |
| 41 | + |
| 42 | + List<Position> coinPositions = spawnActions.stream() |
| 43 | + .filter((action) -> (action.getEntity() instanceof Nut)) |
| 44 | + .map((action) -> action.getPosition()) |
| 45 | + .sorted((a, b) -> { |
| 46 | + if (a.getY() < b.getY()) return -1; |
| 47 | + if (a.getY() == b.getY()) { |
| 48 | + if (a.getX() < b.getX()) return -1; |
| 49 | + if (a.getX() == b.getX()) return 0; |
| 50 | + } |
| 51 | + return 1; |
| 52 | + }) |
| 53 | + .collect(Collectors.toList()); |
| 54 | + |
| 55 | + // subtask b) |
| 56 | + int nrOfCoinsSpawned = coinPositions.size(); |
| 57 | + if (nrOfCoinsSpawned >= 5) { |
| 58 | + this.taskB = this.taskB.updateStatus(TaskVerificationStatus.SUCCESSFUL); |
| 59 | + } |
| 60 | + |
| 61 | + List<BasicTaskInformation> subTasks = new ArrayList<>(); |
| 62 | + subTasks.add(this.taskB); |
| 63 | + |
| 64 | + if (subTasks.stream().allMatch(subTask -> subTask.getTaskStatus().equals(TaskVerificationStatus.SUCCESSFUL))) { |
| 65 | + this.task = this.task.updateStatus(TaskVerificationStatus.SUCCESSFUL); |
| 66 | + } |
| 67 | + |
| 68 | + this.task = this.task.updateSubTasks(subTasks); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Check if a field with at least {@code nrOfCoins} exists. |
| 73 | + */ |
| 74 | + private boolean checkMultiCoinsField(List<Position> coinPositions, int nrOfCoins) { |
| 75 | + Position currentPos = null; |
| 76 | + int currentCount = 0; |
| 77 | + |
| 78 | + Iterator<Position> positions = coinPositions.iterator(); |
| 79 | + |
| 80 | + while (positions.hasNext()) { |
| 81 | + Position p = positions.next(); |
| 82 | + if (currentPos == null || !p.equals(currentPos)) { |
| 83 | + if (currentCount == nrOfCoins) { // found field with right coincount |
| 84 | + return true; |
| 85 | + } |
| 86 | + currentPos = p; |
| 87 | + currentCount = 0; |
| 88 | + } |
| 89 | + currentCount++; |
| 90 | + if (!positions.hasNext()) { // special check for end of list |
| 91 | + if (currentCount == nrOfCoins) { // found field with right coincount |
| 92 | + return true; |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + return false; |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Count the number of horizontal lines of length {@code lineLength}. |
| 102 | + */ |
| 103 | + private int countLines(List<Position> coinPositions, int lineLength) { |
| 104 | + if(coinPositions.isEmpty()){ |
| 105 | + return 0; |
| 106 | + } |
| 107 | + int currentX = coinPositions.get(0).getX(); |
| 108 | + int currentY = coinPositions.get(0).getY(); |
| 109 | + int rowCount = 0; |
| 110 | + int currentLength = 0; |
| 111 | + |
| 112 | + int lineCount = 0; |
| 113 | + |
| 114 | + Iterator<Position> positions = coinPositions.iterator(); |
| 115 | + |
| 116 | + while (positions.hasNext()) { |
| 117 | + Position p = positions.next(); |
| 118 | + if (currentY == p.getY()) { |
| 119 | + if (currentX + currentLength == p.getX()) { |
| 120 | + currentLength++; |
| 121 | + if (!positions.hasNext()) { |
| 122 | + // special check for end of Iterator |
| 123 | + if (currentLength == lineLength) { |
| 124 | + // only count lines of the exact length specified |
| 125 | + lineCount++; |
| 126 | + } |
| 127 | + } |
| 128 | + } else { |
| 129 | + if (currentLength == lineLength) { |
| 130 | + // only count lines of the exact length specified |
| 131 | + lineCount++; |
| 132 | + } |
| 133 | + currentX = p.getX(); |
| 134 | + currentLength = 1; |
| 135 | + } |
| 136 | + } else { |
| 137 | + if (currentLength == lineLength) { |
| 138 | + // only count lines of the exact length specified |
| 139 | + lineCount++; |
| 140 | + } |
| 141 | + // start new row |
| 142 | + currentY = p.getY(); |
| 143 | + currentX = p.getX(); |
| 144 | + currentLength = 1; |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + return lineCount; |
| 149 | + } |
| 150 | + |
| 151 | + @Override |
| 152 | + public TaskInformation getTaskInformation() { |
| 153 | + return this.task; |
| 154 | + } |
| 155 | + |
| 156 | + |
| 157 | + |
| 158 | +} |
0 commit comments