Skip to content
Snippets Groups Projects
Commit 1d51f176 authored by AREZKI Celia's avatar AREZKI Celia
Browse files

Prevent firefighters from crossing a mountain.

parent 39e96532
No related branches found
No related tags found
No related merge requests found
package util; package util;
import view.ViewElement;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
public interface Strategy { public interface Strategy {
Position neighborClosestToTarget(Position position, Collection<Position> targets, Map<Position, List<Position>> neighbors); Position neighborClosestToTarget(Position position, Collection<Position> targets, Map<Position, List<Position>> neighbors, ViewElement[][] grid);
} }
package util; package util;
import util.Position; import util.Position;
import view.ViewElement;
import java.util.*; import java.util.*;
...@@ -13,7 +14,7 @@ public class TargetStrategy implements Strategy{ ...@@ -13,7 +14,7 @@ public class TargetStrategy implements Strategy{
* @return the position next to the current position that is on the path to the closest target. * @return the position next to the current position that is on the path to the closest target.
*/ */
public Position neighborClosestToTarget(Position position, Collection<Position> targets, public Position neighborClosestToTarget(Position position, Collection<Position> targets,
Map<Position, List<Position>> neighbors) { Map<Position, List<Position>> neighbors, ViewElement[][] grid) {
Set<Position> seen = new HashSet<Position>(); Set<Position> seen = new HashSet<Position>();
HashMap<Position, Position> firstMove = new HashMap<Position, Position>(); HashMap<Position, Position> firstMove = new HashMap<Position, Position>();
Queue<Position> toVisit = new LinkedList<Position>(neighbors.get(position)); Queue<Position> toVisit = new LinkedList<Position>(neighbors.get(position));
...@@ -21,6 +22,7 @@ public class TargetStrategy implements Strategy{ ...@@ -21,6 +22,7 @@ public class TargetStrategy implements Strategy{
firstMove.put(initialMove, initialMove); firstMove.put(initialMove, initialMove);
while (!toVisit.isEmpty()) { while (!toVisit.isEmpty()) {
Position current = toVisit.poll(); Position current = toVisit.poll();
if (grid[current.row()][current.column()] == ViewElement.MOUNTAIN) continue;
if (targets.contains(current)) if (targets.contains(current))
return firstMove.get(current); return firstMove.get(current);
for (Position adjacent : neighbors.get(current)) { for (Position adjacent : neighbors.get(current)) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment