diff --git a/src/main/java/model/FireFighterScenario.java b/src/main/java/model/FireFighterScenario.java index 92440f3a819488eaf4c24ebf32277ac68a89c427..d516b7b043f260a29cd19076d2063bd1f466a9c6 100644 --- a/src/main/java/model/FireFighterScenario.java +++ b/src/main/java/model/FireFighterScenario.java @@ -1,5 +1,6 @@ package model; import java.util.ArrayList; +import java.util.Iterator; import java.util.List; import util.Matrix; @@ -8,19 +9,22 @@ import util.Position; public class FireFighterScenario implements Board<Entity>{ - private Matrix<Entity> matrix + private Matrix<Entity> matrix; + private int step; public FireFighterScenario(int boardSize){ - this.matrix = new Matrix(); + this.matrix = new Matrix<Entity>(); + this.step = 0; } public Entity getState(Position position){ - if(position.x() > matrix.size() || position.y() > matrix.get(0).size()){ + if(position.x() > matrix.size() || position.y() > matrix.size()){ throw new IllegalArgumentException("The position x:" + position.x() + " y:" + position.y() + " is out of the board."); } - return matrix.get(position.x()).get(position.y()); + return matrix.get(position.x(), position.y()); } public void setState(Entity state, Position position){ - matrix.get(position.x()).set(position.y(), state); + + matrix.set(position.x(), position.y(), state); } @@ -29,38 +33,33 @@ public class FireFighterScenario implements Board<Entity>{ } public int columnCount(){ - return matrix.get(0).size(); + return matrix.size(); // On considère que la matrice est toujours une matrice carré } - /** - * Update the board to its next generation or state. This method may modify the - * internal state of the board and return a list of positions that have changed - * during the update. - * - * @return A list of positions that have changed during the update. - */ - public List<Position> updateToNextGeneration(){ - for(ArrayList<Entity> l : matrix){ - for(Entity e : l){ + public List<Position> updateToNextGeneration() { + ArrayList<Position> changedPositions = new ArrayList<>(); + Iterator<Entity> iterator = matrix.iterator(); + + while (iterator.hasNext()) { + Entity e = iterator.next(); + Position p = new Position(e.getPosition().x(), e.getPosition().y()); e.nextTurn(this); - } + + if (!e.getPosition().equals(p)) { + changedPositions.add(p); + } } - return matrix; - } + return changedPositions; +} + + - /** - * Reset the board to its initial state. - */ public void reset(){ - throw new IllegalStateException("Method not implemented"); + matrix.clear(); } - /** - * Get the current step number or generation of the board. - * - * @return The current step number or generation. - */ + public int stepNumber(){ - return 0; + return this.step; } } diff --git a/src/main/java/util/TargetStrategy.java b/src/main/java/util/TargetStrategy.java index bdc2aae0c79f4964675d00fc0318ca32d2f8958c..47c2c26012604feb7a83be9292945369d0c7f3de 100644 --- a/src/main/java/util/TargetStrategy.java +++ b/src/main/java/util/TargetStrategy.java @@ -17,7 +17,7 @@ public class TargetStrategy { * @param targets positions that are targeted. * @return the position next to the current position that is on the path to the closest target. */ - Position neighborClosestToFire(Position position, Collection<Position> targets, + public Position neighborClosestToFire(Position position, Collection<Position> targets, Map<Position,List<Position>>neighbors) { Set<Position> seen = new HashSet<Position>(); HashMap<Position, Position> firstMove = new HashMap<Position, Position>();