Skip to content
Snippets Groups Projects
Commit 6e4fed7d authored by AREZKI Celia's avatar AREZKI Celia
Browse files

modify the class FirefighterBoard

parent 971f1e7d
Branches
No related tags found
No related merge requests found
Pipeline #39369 failed
...@@ -40,7 +40,7 @@ public class Controller { ...@@ -40,7 +40,7 @@ public class Controller {
@FXML @FXML
private Grid<ViewElement> grid; private Grid<ViewElement> grid;
private Timeline timeline; private Timeline timeline;
private Board<List<ModelElement>> board; private FirefighterBoard board;
@FXML @FXML
private void initialize() { private void initialize() {
......
...@@ -4,62 +4,9 @@ import util.Position; ...@@ -4,62 +4,9 @@ import util.Position;
import java.util.List; import java.util.List;
/**
* This interface represents a generic board for modeling various state-based systems.
*
* @param <S> The type of state represented on the board.
*/
public interface Board<S> {
/**
* Get the state of the board at a specific position.
*
* @param position The position on the board for which to retrieve the state.
* @return The state at the specified position.
*/
S getState(Position position);
/**
* Set the state of a specific position on the board to the specified state.
*
* @param state The state to set for the given position.
* @param position The position on the board for which to set the state.
*/
void setState(S state, Position position);
/** public interface Board<S> {
* Get the number of rows in the board.
*
* @return The number of rows in the board.
*/
int rowCount();
/**
* Get the number of columns in the board.
*
* @return The number of columns in the board.
*/
int columnCount();
/**
* Update the board to its next generation or state. This method may modify the
* internal state of the board and return a list of positions that have changed
* during the update.
*
* @return A list of positions that have changed during the update.
*/
List<Position> updateToNextGeneration();
/**
* Reset the board to its initial state.
*/
void reset();
/**
* Get the current step number or generation of the board.
*
* @return The current step number or generation.
*/
int stepNumber();
} }
...@@ -3,7 +3,7 @@ package model; ...@@ -3,7 +3,7 @@ package model;
import util.Position; import util.Position;
import java.util.List; import java.util.List;
public interface BoardBehavior<S> { public interface BoardBehavior<S> extends Board<S>{
/** /**
* Get the current step number or generation of the board. * Get the current step number or generation of the board.
* *
......
...@@ -9,7 +9,7 @@ import java.util.List; ...@@ -9,7 +9,7 @@ import java.util.List;
* *
* @param <S> Le type d'état utilisé pour les éléments du tableau. * @param <S> Le type d'état utilisé pour les éléments du tableau.
*/ */
public interface BoardProperties<S> { public interface BoardProperties<S> extends Board<S> {
/** /**
* Retourne le nombre de lignes du tableau. * Retourne le nombre de lignes du tableau.
* *
......
...@@ -13,6 +13,7 @@ public class FireManager implements FireBehavior, FireProperties{ ...@@ -13,6 +13,7 @@ public class FireManager implements FireBehavior, FireProperties{
// Initialize fire positions randomly // Initialize fire positions randomly
public void initializeFires(int fireCount, int rowCount, int columnCount, Random random) { public void initializeFires(int fireCount, int rowCount, int columnCount, Random random) {
firePositions.clear(); firePositions.clear();
for (int i = 0; i < fireCount; i++) { for (int i = 0; i < fireCount; i++) {
firePositions.add(new Position(random.nextInt(rowCount), random.nextInt(columnCount))); firePositions.add(new Position(random.nextInt(rowCount), random.nextInt(columnCount)));
......
...@@ -13,8 +13,7 @@ public class FirefighterBoard implements BoardBehavior<List<ModelElement>> ,Boar ...@@ -13,8 +13,7 @@ public class FirefighterBoard implements BoardBehavior<List<ModelElement>> ,Boar
private final FireManager fireManager; private final FireManager fireManager;
private final FirefighterManager firefighterManager; private final FirefighterManager firefighterManager;
private List<Position> firefighterPositions;
private Set<Position> firePositions;
private final Position[][] positions; private final Position[][] positions;
private int step = 0; private int step = 0;
private final Map<Position, List<Position>> neighbors = new HashMap<>(); private final Map<Position, List<Position>> neighbors = new HashMap<>();
...@@ -28,7 +27,7 @@ public class FirefighterBoard implements BoardBehavior<List<ModelElement>> ,Boar ...@@ -28,7 +27,7 @@ public class FirefighterBoard implements BoardBehavior<List<ModelElement>> ,Boar
this.positions = new Position[rowCount][columnCount]; this.positions = new Position[rowCount][columnCount];
initializePositions(); initializePositions();
initializeNeighbors(); initializeNeighbors();
// Initialize managers // Initialize manager
this.fireManager = new FireManager(new HashSet<>()); this.fireManager = new FireManager(new HashSet<>());
this.firefighterManager = new FirefighterManager(new ArrayList<>(), fireManager); this.firefighterManager = new FirefighterManager(new ArrayList<>(), fireManager);
...@@ -67,7 +66,6 @@ public class FirefighterBoard implements BoardBehavior<List<ModelElement>> ,Boar ...@@ -67,7 +66,6 @@ public class FirefighterBoard implements BoardBehavior<List<ModelElement>> ,Boar
for (int index = 0; index < initialFirefighterCount; index++) { for (int index = 0; index < initialFirefighterCount; index++) {
firefighterPositions.add(randomPosition()); firefighterPositions.add(randomPosition());
} }
firefighterManager.getFirefighterPositions().clear();
firefighterManager.getFirefighterPositions().addAll(firefighterPositions); firefighterManager.getFirefighterPositions().addAll(firefighterPositions);
} }
...@@ -78,10 +76,10 @@ public class FirefighterBoard implements BoardBehavior<List<ModelElement>> ,Boar ...@@ -78,10 +76,10 @@ public class FirefighterBoard implements BoardBehavior<List<ModelElement>> ,Boar
@Override @Override
public List<ModelElement> getState(Position position) { public List<ModelElement> getState(Position position) {
List<ModelElement> result = new ArrayList<>(); List<ModelElement> result = new ArrayList<>();
for (Position firefighterPosition : firefighterPositions) for (Position firefighterPosition : firefighterManager.getFirefighterPositions())
if (firefighterPosition.equals(position)) if (firefighterPosition.equals(position))
result.add(ModelElement.FIREFIGHTER); result.add(ModelElement.FIREFIGHTER);
if (firePositions.contains(position)) if (fireManager.getFirePosition().contains(position))
result.add(ModelElement.FIRE); result.add(ModelElement.FIRE);
return result; return result;
} }
...@@ -124,20 +122,20 @@ public class FirefighterBoard implements BoardBehavior<List<ModelElement>> ,Boar ...@@ -124,20 +122,20 @@ public class FirefighterBoard implements BoardBehavior<List<ModelElement>> ,Boar
} }
private void extinguish(Position position) { private void extinguish(Position position) {
firePositions.remove(position); fireManager.getFirePosition().remove(position);
} }
@Override @Override
public void setState(List<ModelElement> state, Position position) { public void setState(List<ModelElement> state, Position position) {
firePositions.remove(position); fireManager.getFirePosition().remove(position);
for (; ; ) { for (; ; ) {
if (!firefighterPositions.remove(position)) break; if (!firefighterManager.getFirefighterPositions().remove(position)) break;
} }
for (ModelElement element : state) { for (ModelElement element : state) {
switch (element) { switch (element) {
case FIRE -> firePositions.add(position); case FIRE -> fireManager.getFirePosition().add(position);
case FIREFIGHTER -> firefighterPositions.add(position); case FIREFIGHTER -> firefighterManager.getFirefighterPositions().add(position);
} }
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment