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
No related branches found
No related tags found
No related merge requests found
package model;
import model.elements.*;
import model.update.NextGenerationUpdater;
import util.Position;
......@@ -25,6 +26,9 @@ public class FirefighterBoard implements Board<List<ModelElement>> {
private NextGenerationUpdater nextGenerationUpdater;
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) {
......@@ -60,7 +64,13 @@ public class FirefighterBoard implements Board<List<ModelElement>> {
Position position = randomPosition();
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() {
firefighters = new ArrayList<>();
......@@ -118,38 +128,6 @@ public class FirefighterBoard implements Board<List<ModelElement>> {
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
public int rowCount() {
......@@ -167,7 +145,7 @@ public class FirefighterBoard implements Board<List<ModelElement>> {
public List<Position> updateToNextGeneration() {
List<Position> updatedPositions = nextGenerationUpdater.updateToNextGeneration();
step++; // Incrémentation du compteur de génération
step++;
return updatedPositions;
//return nextGenerationUpdater.updateToNextGeneration();
}
......@@ -178,22 +156,27 @@ public class FirefighterBoard implements Board<List<ModelElement>> {
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
public void setState(List<ModelElement> state, Position position) {
fire.getFirePositions().remove(position);
firefighters.removeIf(f -> f.getPosition().equals(position));
clouds.removeIf(c -> c.getPosition().equals(position));
for (ElementHandler handler : handlers) {
handler.removeElement(position);
}
for (ModelElement element : state) {
switch (element) {
case FIRE -> fire.getFirePositions().add(position);
case FIREFIGHTER -> firefighters.add(new Firefighter(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));
}
handlers.stream()
.filter(handler -> handler.getModelElement() == element)
.forEach(handler -> handler.addElement(position));
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment