Skip to content
Snippets Groups Projects
Commit 7c1439de authored by Sarah CHERCHEM's avatar Sarah CHERCHEM
Browse files

Display Motorized in the grid

parent 4bf0e377
No related branches found
No related tags found
No related merge requests found
Pipeline #41509 passed
Showing with 58 additions and 29 deletions
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
...@@ -94,53 +94,71 @@ public class BoardFireFighterBehavior implements BoardBehavior{ ...@@ -94,53 +94,71 @@ public class BoardFireFighterBehavior implements BoardBehavior{
} }
public List<Position> updateMotorized() { public List<Position> updateMotorized() {
List<Position> modifiedPositions = new ArrayList<>(); List<Position> modifiedPositions = new ArrayList<>();
List<Position> newMotorizedPositions = new ArrayList<>(); List<Position> newPositions = new ArrayList<>();
// Déplace chaque pompier motorisé et éteint les feux à proximité for (Position currentPosition : motorizedFighters) {
for (Position motorizedPosition : motorizedFighters) { // Vérification de validité de la position actuelle
// Crée une instance temporaire de MotorizedFireFighter if (!neighbors.containsKey(currentPosition)) {
MotorizedFireFighter motorizedFirefighter = new MotorizedFireFighter(motorizedPosition); System.err.println("Position actuelle invalide : " + currentPosition);
newPositions.add(currentPosition);
continue;
}
// Déplace le pompier // Étape 1 : Calcul du premier déplacement
Position newPosition = motorizedFirefighter.move(firePositions, neighbors); Position firstStep = targetStrategy.neighborClosestToTarget(currentPosition, firePositions, neighbors);
if (firstStep == null || !neighbors.containsKey(firstStep)) {
// Aucun déplacement possible, rester sur place
System.out.println("Pas de première étape possible pour : " + currentPosition);
newPositions.add(currentPosition);
continue;
}
// Éteindre les feux à la nouvelle position // Étape 2 : Calcul du deuxième déplacement
extinguishFire(firePositions, newPosition, modifiedPositions); Position secondStep = targetStrategy.neighborClosestToTarget(firstStep, firePositions, neighbors);
Position finalPosition = (secondStep != null && neighbors.containsKey(secondStep)) ? secondStep : firstStep;
// Mettre à jour les positions // Ajout de la position finale aux nouvelles positions
newMotorizedPositions.add(newPosition); newPositions.add(finalPosition);
modifiedPositions.add(motorizedPosition); // Ancienne position
modifiedPositions.add(newPosition); // Nouvelle position
}
// Met à jour la liste des positions des pompiers motorisés // Mise à jour des positions modifiées
motorizedFighters = newMotorizedPositions; modifiedPositions.add(currentPosition); // Ancienne position
modifiedPositions.add(finalPosition); // Nouvelle position
return modifiedPositions; // Étape 3 : Éteindre les feux à la position finale
extinguish(finalPosition);
extinguishNearbyFires(finalPosition, modifiedPositions);
} }
// Mettre à jour les positions globales
private void extinguish(Position position) { motorizedFighters = newPositions;
firePositions.remove(position); return modifiedPositions;
} }
/** /**
* Éteint les feux autour d'une position donnée. * Éteint les feux à proximité d'une position donnée.
* *
* @param firePositions Les positions des feux actuels.
* @param firefighterPosition La position actuelle du pompier. * @param firefighterPosition La position actuelle du pompier.
* @param modifiedPositions Les positions modifiées pendant ce tour. * @param modifiedPositions Les positions modifiées pendant ce tour.
*/ */
protected void extinguishFire(Set<Position> firePositions, Position firefighterPosition, List<Position> modifiedPositions) { private void extinguishNearbyFires(Position firefighterPosition, List<Position> modifiedPositions) {
List<Position> nearbyFires = neighbors.get(firefighterPosition).stream() List<Position> nearbyFires = neighbors.getOrDefault(firefighterPosition, Collections.emptyList())
.stream()
.filter(firePositions::contains) .filter(firePositions::contains)
.toList(); .toList();
for (Position fire : nearbyFires) { for (Position fire : nearbyFires) {
firePositions.remove(fire); firePositions.remove(fire);
modifiedPositions.add(fire); modifiedPositions.add(fire);
System.out.println("Feu éteint à : " + fire);
} }
} }
private void extinguish(Position position) {
firePositions.remove(position);
}
public Set<Position> getFirePositions() { public Set<Position> getFirePositions() {
return firePositions; return firePositions;
} }
......
...@@ -5,12 +5,21 @@ import util.TargetStrategy; ...@@ -5,12 +5,21 @@ import util.TargetStrategy;
import java.util.*; import java.util.*;
public class MotorizedFireFighter extends FireFighter{ public class MotorizedFireFighter extends BoardElement{
private final TargetStrategy targetStrategy = new TargetStrategy(); private final TargetStrategy targetStrategy = new TargetStrategy();
public MotorizedFireFighter(Position position) { public MotorizedFireFighter(Position position) {
super(position); super(position);
} }
@Override
public String getType() {
return "MotorizedFireFighter";
}
@Override
public String toString() {
return null;
}
/** /**
...@@ -24,15 +33,17 @@ public class MotorizedFireFighter extends FireFighter{ ...@@ -24,15 +33,17 @@ public class MotorizedFireFighter extends FireFighter{
// Première étape // Première étape
Position firstStep = targetStrategy.neighborClosestToTarget(getPosition(), firePositions, neighbors); Position firstStep = targetStrategy.neighborClosestToTarget(getPosition(), firePositions, neighbors);
if (firstStep == null) { if (firstStep == null) {
return getPosition(); // Pas de mouvement possible return getPosition(); // Aucun déplacement possible
} }
// Deuxième étape // Deuxième étape
Position secondStep = targetStrategy.neighborClosestToTarget(firstStep, firePositions, neighbors); Position secondStep = targetStrategy.neighborClosestToTarget(firstStep, firePositions, neighbors);
Position newPosition = secondStep != null ? secondStep : firstStep;
// Mise à jour de la position // Mise à jour de la position
setPosition(newPosition); Position newPosition = (secondStep != null) ? secondStep : firstStep;
setPosition(newPosition); // Mettre à jour la position interne
return newPosition; return newPosition;
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment