diff --git a/src/main/java/controller/Controller.java b/src/main/java/controller/Controller.java index 260a1df48eae1a5eb6b71d1df5d95b561da5e66c..81b9df828351be80fe30e3a28bb32a842ed3e72d 100644 --- a/src/main/java/controller/Controller.java +++ b/src/main/java/controller/Controller.java @@ -66,7 +66,7 @@ public class Controller { model.BoardElement[][] boardElements = new model.BoardElement[rowCount][columnCount]; for(int column = 0; column < columnCount; column++) for(int row = 0; row < rowCount; row++){ - if(board.getElementPosition().containsKey(new Position(row,column))){ + if(board.getElementPosition().containsKey(new Position(row,column)) && board.getElementPosition().get(new Position(row,column)).size()!=0){ boardElements[row][column] = board.getElementPosition().get(new Position(row,column)).get(0); }else{ boardElements[row][column]=new EmptyElement(); diff --git a/src/main/java/model/ExtinguishFire/FireFighter.java b/src/main/java/model/ExtinguishFire/FireFighter.java index 0d7aa63706ebff471c18b73527a9f708c2d18126..f575cc4e7692d09ea4ab1d55ded3e9770dfd25fe 100644 --- a/src/main/java/model/ExtinguishFire/FireFighter.java +++ b/src/main/java/model/ExtinguishFire/FireFighter.java @@ -81,32 +81,40 @@ public class FireFighter implements ExtinguishFire { } } Position newFirefighterPosition = gameBoard.neighborClosestToFire(position); - if(elementPositionCopie.containsKey(newFirefighterPosition)){ - elementPositionCopie.get(newFirefighterPosition).add(new FireFighter(Color.BLUE)); + //elementPosition.get(position).remove(this); + if(elementPosition.containsKey(newFirefighterPosition)){ + elementPosition.get(newFirefighterPosition).add(this); } else{ ArrayList<BoardElement> boardElements = new ArrayList<>(); - boardElements.add(new FireFighter(Color.BLUE)); + boardElements.add(this); elementPosition.put(newFirefighterPosition,boardElements); } gameBoard.extinguish(newFirefighterPosition); List<Position> neighborFirePositions = gameBoard.neighbors(newFirefighterPosition).stream() .filter(firePositions::contains).toList(); - for(Position newposition : neighborFirePositions){ - if(elementPositionCopie.containsKey(newposition)){ - elementPosition.get(newposition).add(new FireFighter(Color.BLUE)); - } - else{ - ArrayList<BoardElement> boardElements = new ArrayList<>(); - boardElements.add(new FireFighter(Color.BLUE)); - elementPosition.put(newposition,boardElements); - } - } - for(Position firePosition : neighborFirePositions) { gameBoard.extinguish(firePosition); } } + /*private List<Position> updateFirefighters() { + List<Position> result = new ArrayList<>(); + List<Position> firefighterNewPositions = new ArrayList<>(); + for (Position firefighterPosition : firefighterPositions) { + Position newFirefighterPosition = neighborClosestToFire(firefighterPosition); + firefighterNewPositions.add(newFirefighterPosition); + extinguish(newFirefighterPosition); + result.add(firefighterPosition); + result.add(newFirefighterPosition); + List<Position> neighborFirePositions = neighbors(newFirefighterPosition).stream() + .filter(firePositions::contains).toList(); + for(Position firePosition : neighborFirePositions) + extinguish(firePosition); + result.addAll(neighborFirePositions); + } + firefighterPositions = firefighterNewPositions; + return result; + }*/ } diff --git a/src/main/java/model/GameBoard.java b/src/main/java/model/GameBoard.java index 216b267c9659dee78e790d0bb092e3ea284f2546..eacfba7c202883afe6d2ada87190c4a9aab0d013 100644 --- a/src/main/java/model/GameBoard.java +++ b/src/main/java/model/GameBoard.java @@ -41,7 +41,7 @@ public class GameBoard implements Board{ @Override public void updateToNextGeneration() { HashMap<Position, ArrayList<BoardElement>> elementPositionCopie = new HashMap<>(); - for (Map.Entry<Position, ArrayList<BoardElement>> entry : this.elementPosition.entrySet()){ + for (Map.Entry<Position, ArrayList<BoardElement>> entry : elementPosition.entrySet()){ elementPositionCopie.put(entry.getKey(),entry.getValue()); } for (Map.Entry<Position, ArrayList<BoardElement>> entry : elementPositionCopie.entrySet()){ @@ -112,12 +112,16 @@ public class GameBoard implements Board{ } public Position neighborClosestToFire(Position position) { + HashMap<Position, ArrayList<BoardElement>> elementPositionCopie = new HashMap<>(); + for (Map.Entry<Position, ArrayList<BoardElement>> entry : elementPosition.entrySet()){ + elementPositionCopie.put(entry.getKey(),entry.getValue()); + } FireFinder fireFinder = new FireFinder(); Set<Position> firePositions = new HashSet<>(); Set<Position> seen = new HashSet<>(); HashMap<Position, Position> firstMove = new HashMap<>(); Queue<Position> toVisit = new LinkedList<>(neighbors(position)); - for (Map.Entry<Position, ArrayList<BoardElement>> entry : this.elementPosition.entrySet()){ + for (Map.Entry<Position, ArrayList<BoardElement>> entry : elementPositionCopie.entrySet()){ for(BoardElement element : entry.getValue()){ if (element.accept(fireFinder)){ firePositions.add(entry.getKey()); @@ -142,11 +146,19 @@ public class GameBoard implements Board{ public void extinguish(Position position) { FireFinder fireFinder = new FireFinder(); - for (BoardElement boardElement : elementPosition.get(position)){ - if (boardElement.accept(fireFinder)){ + HashMap<Position, ArrayList<BoardElement>> elementPositionCopie = new HashMap<>(); + for (Map.Entry<Position, ArrayList<BoardElement>> entry : this.elementPosition.entrySet()){ + elementPositionCopie.put(entry.getKey(),entry.getValue()); + } + for (BoardElement boardElement : elementPositionCopie.get(position)){ + if (boardElement.accept(fireFinder) && elementPosition.get(position).size()!=0){ this.elementPosition.get(position).remove(boardElement); break; } + if (boardElement.accept(fireFinder) && elementPosition.get(position).size()==0){ + this.elementPosition.get(position).remove(boardElement); + this.elementPosition.remove(position); + } } } } diff --git a/src/main/java/view/FirefighterGrid.java b/src/main/java/view/FirefighterGrid.java index e81ddfd50defe3083d66f20b14bbc4de5a700caf..042dc7b54ac7b2d11202a6d3b53727bf1c450baf 100644 --- a/src/main/java/view/FirefighterGrid.java +++ b/src/main/java/view/FirefighterGrid.java @@ -20,8 +20,8 @@ public class FirefighterGrid extends Canvas implements Grid<model.BoardElement>{ @Override public void repaint(List<Pair<Position, model.BoardElement>> positionedElements) { - //clear(positionedElements); - //paint(positionedElements); + clear(positionedElements); + paint(positionedElements); paintLines(); } @@ -34,7 +34,7 @@ public class FirefighterGrid extends Canvas implements Grid<model.BoardElement>{ private void paint(List<Pair<Position, BoardElement>> positionedElements) { for(Pair<Position, BoardElement> pair : positionedElements){ - //paintElementAtPosition(pair.getValue(), pair.getKey()); + paintElementAtPosition(pair.getValue(), pair.getKey()); } }