Skip to content
Snippets Groups Projects
Commit d7791947 authored by CHAHINE Rami's avatar CHAHINE Rami
Browse files

fix the problem with the firefighter movment

parent e4b7841e
No related branches found
No related tags found
No related merge requests found
Pipeline #41118 failed
Showing
with 28 additions and 17 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
No preview for this file type
......@@ -45,6 +45,10 @@ public class Controller {
// Ensure initial UI state is consistent
updateGenerationLabel();
pauseToggleButton.setSelected(true);
stopSimulation();
board.reset();
grid.repaint();
updateGenerationLabel();
}
......
......@@ -124,9 +124,10 @@ public class FirefighterBoard implements Board<List<ModelElement>> {
// Update firefighter positions
FirefighterManager firefighterManager = new FirefighterManager(new SimpleFirefighterMovementStrategy());
List<Position> firefighterModified = firefighterManager.moveFireFighters(firePositions, neighbors);
List<Position> firefighterModified = firefighterManager.moveFireFighters(firePositions, neighbors, firefighterPositions);
modifiedPositions.addAll(firefighterModified);
firefighterPositions = firefighterManager.getFirefighterPositions();
this.firefighterPositions.clear();
this.firefighterPositions = firefighterModified;
// After firefighters move, extinguish fire in the neighborhood
extinguishNearbyFire(modifiedPositions);
......
......@@ -20,30 +20,24 @@ public class FirefighterManager {
}
}
public List<Position> moveFireFighters(Set<Position> firePositions, Map<Position, List<Position>> neighbors) {
public List<Position> moveFireFighters(Set<Position> firePositions, Map<Position, List<Position>> neighbors,List<Position> firefighterPositions) {
List<Position> modifiedPositions = new ArrayList<>();
List<Position> newPositions = new ArrayList<>();
for (Position firefighterPosition : firefighterPositions) {
// Move firefighters towards the nearest fire
Position newPosition = movementStrategy.moveToClosestFire(firefighterPosition, firePositions, neighbors);
System.out.println(newPosition);
System.out.println(firefighterPosition);
// Extinguish fire in the neighboring cells
List<Position> nearbyFires = neighbors.get(newPosition);
for (Position firePosition : nearbyFires) {
if (firePositions.contains(firePosition)) {
firePositions.remove(firePosition); // Extinguish the fire
modifiedPositions.add(firePosition); // Track the modified fire position
}
}
// Update the firefighter's new position
newPositions.add(newPosition);
modifiedPositions.add(firefighterPosition); // Track the original firefighter position
modifiedPositions.add(newPosition); // Track the new firefighter position
}
firefighterPositions = newPositions; // Update the firefighter positions list with new positions
firefighterPositions.clear();
firefighterPositions = modifiedPositions; // Update the firefighter positions list with new positions
return modifiedPositions;
}
......
......@@ -8,6 +8,7 @@ public class SimpleFirefighterMovementStrategy implements FirefighterMovementStr
@Override
public Position moveToClosestFire(Position current, Set<Position> firePositions, Map<Position, List<Position>> neighbors) {
Position closestFire = null;
Position newposition = current;
int minDistance = Integer.MAX_VALUE;
for (Position fire : firePositions) {
......@@ -18,8 +19,19 @@ public class SimpleFirefighterMovementStrategy implements FirefighterMovementStr
}
}
return closestFire != null && !closestFire.equals(current) ? closestFire : current;
if (closestFire != null) {
if (current.getRow()!=closestFire.getRow()){
if (current.getRow()<closestFire.getRow()) { newposition = new Position(current.getRow()+1, current.getCol()); }
else { newposition = new Position(current.getRow()-1, current.getCol()); }
}
else {
if(current.getCol()<closestFire.getCol()) { newposition = new Position(current.getRow(), current.getCol()+1);}
else { newposition = new Position(current.getRow(), current.getCol()-1); }
}
}
return newposition;
}
private int calculateDistance(Position a, Position b) {
// Use Manhattan distance
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment