Select Git revision
FireFighters.java
Forked from
LABOUREL Arnaud / Firefighter template
3 commits behind, 5 commits ahead of the upstream repository.
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
FireFighters.java 1.91 KiB
package model;
import util.Position;
import java.util.*;
public class FireFighters extends AbstractElements{
public List<Position> firefighterPositions;
public Fire fire;
public FireFighters(int columnCount, int rowCount, Fire fire){
firefighterPositions = new ArrayList<>();
this.columnCount = columnCount;
this.rowCount = rowCount;
this.fire = fire;
}
public void nextMove(){
List<Position> firefighterNewPositions = new ArrayList<>();
for (Position firefighterPosition : firefighterPositions) {
Position newFirefighterPosition = neighborClosestToFire(firefighterPosition);
firefighterNewPositions.add(newFirefighterPosition);
fire.extinguish(newFirefighterPosition);
}
firefighterPositions = firefighterNewPositions;
}
protected 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 (fire.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;
}
public void initializeElements(int count, int rowCount, int columnCount){
firefighterPositions = new ArrayList<>();
for(int i = 0; i < count; i++)
firefighterPositions.add(randomPosition(rowCount, columnCount));
}
}