Skip to content
Snippets Groups Projects
Select Git revision
  • ca189b2585bb0127a3146697bd050549b9dfc3b0
  • main default protected
  • variant
3 results

FireFighters.java

  • 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));
        }
    }