Skip to content
Snippets Groups Projects
Fire.java 1.72 KiB
Newer Older
  • Learn to ignore specific revisions
  • import util.Neighbour;
    
    import java.util.*;
    
    BELHACHEMI Mehdi's avatar
    BELHACHEMI Mehdi committed
    
    
    public class Fire implements Element {
    
    BELHACHEMI Mehdi's avatar
    BELHACHEMI Mehdi committed
        private Position position;
    
    
        private Neighbour neighbour;
        private final ModelElement modelElement;
    
        public Fire(Position position){
    
    BELHACHEMI Mehdi's avatar
    BELHACHEMI Mehdi committed
            this.position = position;
    
    BELHACHEMI Mehdi's avatar
    BELHACHEMI Mehdi committed
    
        public Position getPosition() {
            return position;
    
        @Override
        public ModelElement getElement() {
            return modelElement;
        }
    
    
    BELHACHEMI Mehdi's avatar
    BELHACHEMI Mehdi committed
        public void setPosition(Position position) {
            this.position = position;
    
        public List<Position> update(Board<List<ModelElement>> firefighterBoard) {
    
            this.neighbour = new Neighbour(firefighterBoard);
    
    
            List<Position> modifiedPositions = new ArrayList<>();
            if (firefighterBoard.stepNumber() % 2 == 0) {
    
                Set<Position> newFirePositions = new HashSet<>();
                for (Position fire : new HashSet<>(firefighterBoard.getFires().keySet())) {
    
                    List<Position> neighboursAvailable = this.neighbour.getNeighbors().get(fire);
                    neighboursAvailable.removeAll(firefighterBoard.getObstacles().keySet());
    
                    newFirePositions.addAll(this.neighbour.getNeighbors().get(fire));
                }
    
                for (Position position : newFirePositions) {
                    if (!firefighterBoard.getFires().containsKey(position)) {
                        firefighterBoard.getFires().put(position, new Fire(position));
                        modifiedPositions.add(position);
    
                    }
                }
            }
            return modifiedPositions;
    
        }
        public boolean contains(List<Position> positions) {
            return positions.contains(this.getPosition());