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

modify the class FirefighterBoard

parent 971f1e7d
No related branches found
No related tags found
No related merge requests found
Pipeline #39369 failed
......@@ -40,7 +40,7 @@ public class Controller {
@FXML
private Grid<ViewElement> grid;
private Timeline timeline;
private Board<List<ModelElement>> board;
private FirefighterBoard board;
@FXML
private void initialize() {
......
......@@ -4,62 +4,9 @@ import util.Position;
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);
/**
* 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();
public interface Board<S> {
/**
* 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;
import util.Position;
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.
*
......
......@@ -9,7 +9,7 @@ import java.util.List;
*
* @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.
*
......
......@@ -13,6 +13,7 @@ public class FireManager implements FireBehavior, FireProperties{
// Initialize fire positions randomly
public void initializeFires(int fireCount, int rowCount, int columnCount, Random random) {
firePositions.clear();
for (int i = 0; i < fireCount; i++) {
firePositions.add(new Position(random.nextInt(rowCount), random.nextInt(columnCount)));
......
......@@ -13,8 +13,7 @@ public class FirefighterBoard implements BoardBehavior<List<ModelElement>> ,Boar
private final FireManager fireManager;
private final FirefighterManager firefighterManager;
private List<Position> firefighterPositions;
private Set<Position> firePositions;
private final Position[][] positions;
private int step = 0;
private final Map<Position, List<Position>> neighbors = new HashMap<>();
......@@ -28,7 +27,7 @@ public class FirefighterBoard implements BoardBehavior<List<ModelElement>> ,Boar
this.positions = new Position[rowCount][columnCount];
initializePositions();
initializeNeighbors();
// Initialize managers
// Initialize manager
this.fireManager = new FireManager(new HashSet<>());
this.firefighterManager = new FirefighterManager(new ArrayList<>(), fireManager);
......@@ -67,7 +66,6 @@ public class FirefighterBoard implements BoardBehavior<List<ModelElement>> ,Boar
for (int index = 0; index < initialFirefighterCount; index++) {
firefighterPositions.add(randomPosition());
}
firefighterManager.getFirefighterPositions().clear();
firefighterManager.getFirefighterPositions().addAll(firefighterPositions);
}
......@@ -78,10 +76,10 @@ public class FirefighterBoard implements BoardBehavior<List<ModelElement>> ,Boar
@Override
public List<ModelElement> getState(Position position) {
List<ModelElement> result = new ArrayList<>();
for (Position firefighterPosition : firefighterPositions)
for (Position firefighterPosition : firefighterManager.getFirefighterPositions())
if (firefighterPosition.equals(position))
result.add(ModelElement.FIREFIGHTER);
if (firePositions.contains(position))
if (fireManager.getFirePosition().contains(position))
result.add(ModelElement.FIRE);
return result;
}
......@@ -124,20 +122,20 @@ public class FirefighterBoard implements BoardBehavior<List<ModelElement>> ,Boar
}
private void extinguish(Position position) {
firePositions.remove(position);
fireManager.getFirePosition().remove(position);
}
@Override
public void setState(List<ModelElement> state, Position position) {
firePositions.remove(position);
fireManager.getFirePosition().remove(position);
for (; ; ) {
if (!firefighterPositions.remove(position)) break;
if (!firefighterManager.getFirefighterPositions().remove(position)) break;
}
for (ModelElement element : state) {
switch (element) {
case FIRE -> firePositions.add(position);
case FIREFIGHTER -> firefighterPositions.add(position);
case FIRE -> fireManager.getFirePosition().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