Skip to content
Snippets Groups Projects
Commit 00616fce authored by MEHDI's avatar MEHDI
Browse files

mehdi and mohamed : adding neighbour class to elements

parent 1406b784
No related branches found
No related tags found
No related merge requests found
package model; package model;
import util.Neighbour;
import util.Position; import util.Position;
import java.util.*; import java.util.*;
...@@ -9,8 +10,8 @@ public class Fire implements Element { ...@@ -9,8 +10,8 @@ public class Fire implements Element {
private Position position; private Position position;
private Neighbour neighbour;
private ModelElement modelElement; private final ModelElement modelElement;
public Fire(Position position){ public Fire(Position position){
...@@ -34,17 +35,18 @@ public class Fire implements Element { ...@@ -34,17 +35,18 @@ public class Fire implements Element {
public List<Position> update(Board<List<ModelElement>> firefighterBoard) { public List<Position> update(Board<List<ModelElement>> firefighterBoard) {
this.neighbour = new Neighbour(firefighterBoard);
List<Position> modifiedPositions = new ArrayList<>(); List<Position> modifiedPositions = new ArrayList<>();
if (firefighterBoard.stepNumber() % 2 == 0) { if (firefighterBoard.stepNumber() % 2 == 0) {
Set<Position> newFirePositions = new HashSet<>(); Set<Position> newFirePositions = new HashSet<>();
for (Position fire : new HashSet<>(firefighterBoard.getFires().keySet())) { for (Position fire : new HashSet<>(firefighterBoard.getFires().keySet())) {
List<Position> neighboursAvaiable = firefighterBoard.getNeighbor().getNeighbors().get(fire); List<Position> neighboursAvailable = this.neighbour.getNeighbors().get(fire);
neighboursAvaiable.removeAll(firefighterBoard.getObstacles().keySet()); neighboursAvailable.removeAll(firefighterBoard.getObstacles().keySet());
newFirePositions.addAll(firefighterBoard.getNeighbor().getNeighbors().get(fire));
}
newFirePositions.addAll(this.neighbour.getNeighbors().get(fire));
}
for (Position position : newFirePositions) { for (Position position : newFirePositions) {
if (!firefighterBoard.getFires().containsKey(position)) { if (!firefighterBoard.getFires().containsKey(position)) {
firefighterBoard.getFires().put(position, new Fire(position)); firefighterBoard.getFires().put(position, new Fire(position));
......
package model; package model;
import util.Neighbour;
import util.Position; import util.Position;
import util.TargetStrategy; import util.TargetStrategy;
...@@ -11,7 +12,7 @@ import java.util.Map; ...@@ -11,7 +12,7 @@ import java.util.Map;
public class FireFighter extends Extinguisher { public class FireFighter extends Extinguisher {
private Neighbour neighbour;
private final TargetStrategy targetStrategy = new TargetStrategy(); private final TargetStrategy targetStrategy = new TargetStrategy();
...@@ -22,10 +23,12 @@ public class FireFighter extends Extinguisher { ...@@ -22,10 +23,12 @@ public class FireFighter extends Extinguisher {
public List<Position> update(Board<List<ModelElement>> firefighterBoard){ public List<Position> update(Board<List<ModelElement>> firefighterBoard){
this.neighbour = new Neighbour(firefighterBoard);
List<Position> modifiedPositions = new ArrayList<>(); List<Position> modifiedPositions = new ArrayList<>();
Position currentPosition = this.getPosition(); Position currentPosition = this.getPosition();
Position newPosition = targetStrategy.neighborClosestToFire(currentPosition, firefighterBoard.getFires().keySet(), firefighterBoard.getNeighbor().getNeighbors()); Position newPosition = targetStrategy.neighborClosestToFire(currentPosition, firefighterBoard.getFires().keySet(), this.neighbour.getNeighbors());
this.setPosition(newPosition); this.setPosition(newPosition);
modifiedPositions.add(currentPosition); modifiedPositions.add(currentPosition);
...@@ -33,7 +36,7 @@ public class FireFighter extends Extinguisher { ...@@ -33,7 +36,7 @@ public class FireFighter extends Extinguisher {
extinguish(firefighterBoard,newPosition); extinguish(firefighterBoard,newPosition);
for (Position neighbor : firefighterBoard.getNeighbor().getNeighbors().get(newPosition)) { for (Position neighbor : this.neighbour.getNeighbors().get(newPosition)) {
extinguish(firefighterBoard,neighbor); extinguish(firefighterBoard,neighbor);
modifiedPositions.add(neighbor); modifiedPositions.add(neighbor);
} }
......
...@@ -9,12 +9,10 @@ import java.util.*; ...@@ -9,12 +9,10 @@ import java.util.*;
public class FirefighterBoard implements Board<List<ModelElement>> { public class FirefighterBoard implements Board<List<ModelElement>> {
private final int columnCount; private final int columnCount;
private final int rowCount; private final int rowCount;
private Map<Position, Fire> fires = new HashMap<>(); private Map<Position, Fire> fires = new HashMap<>();
private Map<Position, Extinguisher> extinguishers = new HashMap<>(); private Map<Position, Extinguisher> extinguishers = new HashMap<>();
private Map<Position, Obstacle> obstacles = new HashMap<>(); private Map<Position, Obstacle> obstacles = new HashMap<>();
private final Neighbour neighbour; private final Neighbour neighbour;
private final Position[][] positions; private final Position[][] positions;
private int step = 0; private int step = 0;
private final Random randomGenerator = new Random(); private final Random randomGenerator = new Random();
...@@ -25,7 +23,6 @@ public class FirefighterBoard implements Board<List<ModelElement>> { ...@@ -25,7 +23,6 @@ public class FirefighterBoard implements Board<List<ModelElement>> {
this.positions = new Position[rowCount][columnCount]; this.positions = new Position[rowCount][columnCount];
this.neighbour = new Neighbour(this); this.neighbour = new Neighbour(this);
initializePositions(); initializePositions();
initializeElements(); initializeElements();
} }
...@@ -37,7 +34,11 @@ public class FirefighterBoard implements Board<List<ModelElement>> { ...@@ -37,7 +34,11 @@ public class FirefighterBoard implements Board<List<ModelElement>> {
} }
} }
public void initializeElements() { public void initializeElements() {
obstacles.clear();
extinguishers.clear();
fires.clear();
for(ModelElement modelElement : ModelElement.values()){ for(ModelElement modelElement : ModelElement.values()){
for(int elementCount = 0; elementCount< modelElement.getInitialNumber();elementCount++){ for(int elementCount = 0; elementCount< modelElement.getInitialNumber();elementCount++){
Position position = randomPosition(); Position position = randomPosition();
...@@ -131,8 +132,6 @@ public class FirefighterBoard implements Board<List<ModelElement>> { ...@@ -131,8 +132,6 @@ public class FirefighterBoard implements Board<List<ModelElement>> {
} }
@Override @Override
public void setState(List<ModelElement> state, Position position) { public void setState(List<ModelElement> state, Position position) {
...@@ -140,8 +139,6 @@ public class FirefighterBoard implements Board<List<ModelElement>> { ...@@ -140,8 +139,6 @@ public class FirefighterBoard implements Board<List<ModelElement>> {
extinguishers.remove(position); extinguishers.remove(position);
extinguishers.get(position).extinguish(this,position); extinguishers.get(position).extinguish(this,position);
} }
for (ModelElement element : state) { for (ModelElement element : state) {
switch (element) { switch (element) {
case FIRE -> fires.put(position, new Fire(position)); case FIRE -> fires.put(position, new Fire(position));
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment