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{
}
public List<Position> updateMotorized() {
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 motorizedPosition : motorizedFighters) {
// Crée une instance temporaire de MotorizedFireFighter
MotorizedFireFighter motorizedFirefighter = new MotorizedFireFighter(motorizedPosition);
for (Position currentPosition : motorizedFighters) {
// Vérification de validité de la position actuelle
if (!neighbors.containsKey(currentPosition)) {
System.err.println("Position actuelle invalide : " + currentPosition);
newPositions.add(currentPosition);
continue;
}
// Déplace le pompier
Position newPosition = motorizedFirefighter.move(firePositions, neighbors);
// Étape 1 : Calcul du premier déplacement
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
extinguishFire(firePositions, newPosition, modifiedPositions);
// Étape 2 : Calcul du deuxième déplacement
Position secondStep = targetStrategy.neighborClosestToTarget(firstStep, firePositions, neighbors);
Position finalPosition = (secondStep != null && neighbors.containsKey(secondStep)) ? secondStep : firstStep;
// Mettre à jour les positions
newMotorizedPositions.add(newPosition);
modifiedPositions.add(motorizedPosition); // Ancienne position
modifiedPositions.add(newPosition); // Nouvelle position
}
// Ajout de la position finale aux nouvelles positions
newPositions.add(finalPosition);
// Met à jour la liste des positions des pompiers motorisés
motorizedFighters = newMotorizedPositions;
// Mise à jour des positions modifiées
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);
}
private void extinguish(Position position) {
firePositions.remove(position);
// Mettre à jour les positions globales
motorizedFighters = newPositions;
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 modifiedPositions Les positions modifiées pendant ce tour.
*/
protected void extinguishFire(Set<Position> firePositions, Position firefighterPosition, List<Position> modifiedPositions) {
List<Position> nearbyFires = neighbors.get(firefighterPosition).stream()
private void extinguishNearbyFires(Position firefighterPosition, List<Position> modifiedPositions) {
List<Position> nearbyFires = neighbors.getOrDefault(firefighterPosition, Collections.emptyList())
.stream()
.filter(firePositions::contains)
.toList();
for (Position fire : nearbyFires) {
firePositions.remove(fire);
modifiedPositions.add(fire);
System.out.println("Feu éteint à : " + fire);
}
}
private void extinguish(Position position) {
firePositions.remove(position);
}
public Set<Position> getFirePositions() {
return firePositions;
}
......
......@@ -5,12 +5,21 @@ import util.TargetStrategy;
import java.util.*;
public class MotorizedFireFighter extends FireFighter{
public class MotorizedFireFighter extends BoardElement{
private final TargetStrategy targetStrategy = new TargetStrategy();
public MotorizedFireFighter(Position position) {
super(position);
}
@Override
public String getType() {
return "MotorizedFireFighter";
}
@Override
public String toString() {
return null;
}
/**
......@@ -24,15 +33,17 @@ public class MotorizedFireFighter extends FireFighter{
// Première étape
Position firstStep = targetStrategy.neighborClosestToTarget(getPosition(), firePositions, neighbors);
if (firstStep == null) {
return getPosition(); // Pas de mouvement possible
return getPosition(); // Aucun déplacement possible
}
// Deuxième étape
Position secondStep = targetStrategy.neighborClosestToTarget(firstStep, firePositions, neighbors);
Position newPosition = secondStep != null ? secondStep : firstStep;
// Mise à jour de la position
setPosition(newPosition);
Position newPosition = (secondStep != null) ? secondStep : firstStep;
setPosition(newPosition); // Mettre à jour la position interne
return newPosition;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment