Skip to content
Snippets Groups Projects
Commit 647d95cd authored by couetoux.b's avatar couetoux.b
Browse files

extract closest neighbour

parent d082e98d
No related branches found
No related tags found
No related merge requests found
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.10.2-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
......@@ -10,6 +10,7 @@ public class FirefighterBoard implements Board<List<ModelElement>> {
private final int rowCount;
private final int initialFireCount;
private final int initialFirefighterCount;
private final TargetStrategy targetStrategy = new TargetStrategy(this);
private List<Position> firefighterPositions;
private Set<Position> firePositions;
private int step = 0;
......@@ -87,7 +88,8 @@ public class FirefighterBoard implements Board<List<ModelElement>> {
List<Position> modifiedPosition = new ArrayList<>();
List<Position> firefighterNewPositions = new ArrayList<>();
for (Position firefighterPosition : firefighterPositions) {
Position newFirefighterPosition = neighborClosestToFire(firefighterPosition);
Position newFirefighterPosition =
targetStrategy.neighborClosestToFire(firefighterPosition, firePositions);
firefighterNewPositions.add(newFirefighterPosition);
extinguish(newFirefighterPosition);
modifiedPosition.add(firefighterPosition);
......@@ -112,7 +114,7 @@ public class FirefighterBoard implements Board<List<ModelElement>> {
firePositions.remove(position);
}
private List<Position> neighbors(Position position) {
public List<Position> neighbors(Position position) {
List<Position> list = new ArrayList<>();
if (position.row() > 0) list.add(new Position(position.row() - 1, position.column()));
if (position.column() > 0) list.add(new Position(position.row(), position.column() - 1));
......@@ -121,26 +123,6 @@ public class FirefighterBoard implements Board<List<ModelElement>> {
return list;
}
private Position neighborClosestToFire(Position position) {
Set<Position> seen = new HashSet<>();
HashMap<Position, Position> firstMove = new HashMap<>();
Queue<Position> toVisit = new LinkedList<>(neighbors(position));
for (Position initialMove : toVisit)
firstMove.put(initialMove, initialMove);
while (!toVisit.isEmpty()) {
Position current = toVisit.poll();
if (firePositions.contains(current))
return firstMove.get(current);
for (Position adjacent : neighbors(current)) {
if (seen.contains(adjacent)) continue;
toVisit.add(adjacent);
seen.add(adjacent);
firstMove.put(adjacent, firstMove.get(current));
}
}
return position;
}
@Override
public void setState(List<ModelElement> state, Position position) {
firePositions.remove(position);
......@@ -154,4 +136,7 @@ public class FirefighterBoard implements Board<List<ModelElement>> {
}
}
}
public List<Position> getFirePositions() {
return firefighterPositions; }
}
\ No newline at end of file
package model;
import util.Position;
import java.util.*;
public class TargetStrategy {
private final FirefighterBoard firefighterBoard;
public TargetStrategy(FirefighterBoard firefighterBoard) {
this.firefighterBoard = firefighterBoard;
}
/**
* @param position current position.
* @param targets positions that are targeted.
* @return the position next to the current position that is on the path to the closest target.
*/
Position neighborClosestToFire(Position position, Collection<Position> targets) {
Set<Position> seen = new HashSet<Position>();
HashMap<Position, Position> firstMove = new HashMap<Position, Position>();
Queue<Position> toVisit = new LinkedList<Position>(firefighterBoard.neighbors(position));
for (Position initialMove : toVisit)
firstMove.put(initialMove, initialMove);
while (!toVisit.isEmpty()) {
Position current = toVisit.poll();
if (targets.contains(current))
return firstMove.get(current);
for (Position adjacent : firefighterBoard.neighbors(current)) {
if (seen.contains(adjacent)) continue;
toVisit.add(adjacent);
seen.add(adjacent);
firstMove.put(adjacent, firstMove.get(current));
}
}
return position;
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment