Skip to content
Snippets Groups Projects
Commit 3b908ed8 authored by ousseyn01's avatar ousseyn01
Browse files

FirefighterBoard.java is MODIFIED in order to do respect OCP

parent 5b2edb32
Branches
No related tags found
No related merge requests found
package model; package model;
import model.elements.*;
import model.update.NextGenerationUpdater; import model.update.NextGenerationUpdater;
import util.Position; import util.Position;
...@@ -25,6 +26,9 @@ public class FirefighterBoard implements Board<List<ModelElement>> { ...@@ -25,6 +26,9 @@ public class FirefighterBoard implements Board<List<ModelElement>> {
private NextGenerationUpdater nextGenerationUpdater; private NextGenerationUpdater nextGenerationUpdater;
private List<TerrainElement> terrainElements; private List<TerrainElement> terrainElements;
private final List<ElementHandler> handlers = new ArrayList<>();
public FirefighterBoard(int columnCount, int rowCount, int initialFireCount, int initialFirefighterCount, int initialCloudCount, int initialMountainCount) { public FirefighterBoard(int columnCount, int rowCount, int initialFireCount, int initialFirefighterCount, int initialCloudCount, int initialMountainCount) {
...@@ -60,7 +64,13 @@ public class FirefighterBoard implements Board<List<ModelElement>> { ...@@ -60,7 +64,13 @@ public class FirefighterBoard implements Board<List<ModelElement>> {
Position position = randomPosition(); Position position = randomPosition();
terrainElements.add(new Rocaille(position)); terrainElements.add(new Rocaille(position));
} }
}
handlers.add(new FireHandler(fire));
handlers.add(new FirefighterHandler(firefighters));
handlers.add(new CloudHandler(clouds, neighbors));
handlers.add(new MountainHandler(mountains));
handlers.add(new TerrainHandler(terrainElements, Route.class, ModelElement.ROUTE));
handlers.add(new TerrainHandler(terrainElements, Rocaille.class, ModelElement.ROCAILLE)); }
public void initializeElements() { public void initializeElements() {
firefighters = new ArrayList<>(); firefighters = new ArrayList<>();
...@@ -118,38 +128,6 @@ public class FirefighterBoard implements Board<List<ModelElement>> { ...@@ -118,38 +128,6 @@ public class FirefighterBoard implements Board<List<ModelElement>> {
return new Position(randomGenerator.nextInt(rowCount), randomGenerator.nextInt(columnCount)); return new Position(randomGenerator.nextInt(rowCount), randomGenerator.nextInt(columnCount));
} }
@Override
public List<ModelElement> getState(Position position) {
List<ModelElement> result = new ArrayList<>();
for (Cloud cloud : clouds) {
if (cloud.getPosition().equals(position)) {
result.add(ModelElement.CLOUD);
}
}
for (Mountain mountain : mountains){
if(mountain.getPosition().equals(position)){
result.add(ModelElement.MOUNTAIN);
}
}
for (Firefighter firefighter : firefighters)
if (firefighter.getPosition().equals(position))
result.add(ModelElement.FIREFIGHTER);
if (fire.getFirePositions().contains(position))
result.add(ModelElement.FIRE);
for (TerrainElement element : terrainElements) {
if (element.getPosition().equals(position)) {
if (element instanceof Route) {
result.add(ModelElement.ROUTE);
} else if (element instanceof Rocaille) {
result.add(ModelElement.ROCAILLE);
}
}
}
return result;
}
@Override @Override
public int rowCount() { public int rowCount() {
...@@ -167,7 +145,7 @@ public class FirefighterBoard implements Board<List<ModelElement>> { ...@@ -167,7 +145,7 @@ public class FirefighterBoard implements Board<List<ModelElement>> {
public List<Position> updateToNextGeneration() { public List<Position> updateToNextGeneration() {
List<Position> updatedPositions = nextGenerationUpdater.updateToNextGeneration(); List<Position> updatedPositions = nextGenerationUpdater.updateToNextGeneration();
step++; // Incrémentation du compteur de génération step++;
return updatedPositions; return updatedPositions;
//return nextGenerationUpdater.updateToNextGeneration(); //return nextGenerationUpdater.updateToNextGeneration();
} }
...@@ -178,22 +156,27 @@ public class FirefighterBoard implements Board<List<ModelElement>> { ...@@ -178,22 +156,27 @@ public class FirefighterBoard implements Board<List<ModelElement>> {
nextGenerationUpdater.resetStep(); nextGenerationUpdater.resetStep();
} }
@Override
public List<ModelElement> getState(Position position) {
List<ModelElement> result = new ArrayList<>();
for (ElementHandler handler : handlers) {
if (handler.hasElement(position)) {
result.add(handler.getModelElement());
}
}
return result;
}
@Override @Override
public void setState(List<ModelElement> state, Position position) { public void setState(List<ModelElement> state, Position position) {
fire.getFirePositions().remove(position); for (ElementHandler handler : handlers) {
firefighters.removeIf(f -> f.getPosition().equals(position)); handler.removeElement(position);
clouds.removeIf(c -> c.getPosition().equals(position)); }
for (ModelElement element : state) { for (ModelElement element : state) {
switch (element) { handlers.stream()
case FIRE -> fire.getFirePositions().add(position); .filter(handler -> handler.getModelElement() == element)
case FIREFIGHTER -> firefighters.add(new Firefighter(position)); .forEach(handler -> handler.addElement(position));
case CLOUD -> clouds.add(new Cloud(position, neighbors));
case MOUNTAIN -> mountains.add(new Mountain(position));
case ROUTE -> terrainElements.add(new Route(position));
case ROCAILLE -> terrainElements.add(new Rocaille(position));
}
} }
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment