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

Correction de la décomposition

parent 35c55f6c
No related branches found
No related tags found
No related merge requests found
Pipeline #40407 failed
......@@ -59,7 +59,7 @@ public class Controller {
}
private void updateBoard(){
List<Position> updatedPositions = board.updateToNextGeneration();
List<Position> updatedPositions = board.getBehavior().updateToNextGeneration();
List<Pair<Position, ViewElement>> updatedSquares = new ArrayList<>();
for(Position updatedPosition : updatedPositions){
List<ModelElement> squareState = board.getState(updatedPosition);
......@@ -67,18 +67,18 @@ public class Controller {
updatedSquares.add(new Pair<>(updatedPosition, viewElement));
}
grid.repaint(updatedSquares);
updateGenerationLabel(board.stepNumber());
updateGenerationLabel(board.getBehavior().stepNumber());
}
private void repaintGrid(){
int columnCount = board.columnCount();
int rowCount = board.rowCount();
int columnCount = board.getProperties().columnCount();
int rowCount = board.getProperties().rowCount();
ViewElement[][] viewElements = new ViewElement[rowCount][columnCount];
for(int column = 0; column < columnCount; column++)
for(int row = 0; row < rowCount; row++)
viewElements[row][column] = getViewElement(board.getState(new Position(row, column)));
grid.repaint(viewElements);
updateGenerationLabel(board.stepNumber());
updateGenerationLabel(board.getBehavior().stepNumber());
}
private ViewElement getViewElement(List<ModelElement> squareState) {
......
......@@ -6,6 +6,20 @@ import java.util.List;
public interface Board<S> {
/**
* Retourne l'état des éléments situés à une position spécifique du tableau.
*
* @param position La position sur le tableau pour laquelle récupérer l'état.
* @return L'état des éléments à la position donnée.
*/
S getState(Position position); // État des éléments sur une position donnée.
/**
* Définit l'état des éléments à une position spécifique du tableau.
*
* @param state L'état à définir pour la position donnée.
* @param position La position sur le tableau pour laquelle définir l'état.
*/
void setState(S state, Position position);
}
......
......@@ -3,7 +3,7 @@ package model;
import util.Position;
import java.util.List;
public interface BoardBehavior<S> extends Board<S>{
public interface BoardBehavior<S> {
/**
* Get the current step number or generation of the board.
*
......
......@@ -49,7 +49,7 @@ public class BoardFireFighterBehavior implements BoardBehavior{
List<Position> modifiedPositions = new ArrayList<>();
List<Position> newFirefighterPositions = new ArrayList<>();
for (Position firefighterPosition : firefighterPositions) {
Position newFirefighterPosition = targetStrategy.neighborClosestToFire(firefighterPosition, firePositions, neighbors);
Position newFirefighterPosition = targetStrategy.neighborClosestToTarget(firefighterPosition, firePositions, neighbors);
newFirefighterPositions.add(newFirefighterPosition);
extinguish(newFirefighterPosition);
modifiedPositions.add(firefighterPosition);
......@@ -86,17 +86,18 @@ public class BoardFireFighterBehavior implements BoardBehavior{
step++;
}
public int getStep() {
return step;
}
@Override
public int stepNumber() {
return 0;
return step;
}
@Override
public List<Position> updateToNextGeneration() {
return List.of();
List<Position> modifiedPositions = updateFirefighters();
modifiedPositions.addAll(updateFires());
incrementStep();
return modifiedPositions;
}
@Override
......
......@@ -10,16 +10,21 @@ public class BoardFirefighterProperties implements BoardProperties{
private final int columnCount;
private final Position[][] positions;
private final Map<Position, List<Position>> neighbors;
private final int initialFireCount;
private final int initialFirefighterCount;
public BoardFirefighterProperties(int rowCount, int columnCount, Map<Position, List<Position>> neighbors) {
public BoardFirefighterProperties(int rowCount, int columnCount, Map<Position, List<Position>> neighbors, int initialFireCount, int initialFirefighterCount) {
this.rowCount = rowCount;
this.columnCount = columnCount;
this.positions = new Position[rowCount][columnCount];
this.neighbors = neighbors;
this.initialFireCount = initialFireCount;
this.initialFirefighterCount = initialFirefighterCount;
this.positions=new Position[rowCount][columnCount];
initializePositions();
}
private void initializePositions() {
for (int row = 0; row < rowCount; row++) {
for (int column = 0; column < columnCount; column++) {
......@@ -28,13 +33,7 @@ public class BoardFirefighterProperties implements BoardProperties{
}
}
public int getRowCount() {
return rowCount;
}
public int getColumnCount() {
return columnCount;
}
public Map<Position, List<Position>> getNeighbors() {
return neighbors;
......@@ -45,21 +44,19 @@ public class BoardFirefighterProperties implements BoardProperties{
}
@Override
public int rowCount() {
return 0;
return rowCount;
}
@Override
public int columnCount() {
return 0;
return columnCount;
}
@Override
public List<ModelElement> getState(Position position) {
return List.of();
public int getInitialFireCount() {
return initialFireCount;
}
@Override
public void setState(List<ModelElement> state, Position position) {
public int getInitialFirefighterCount() {
return initialFirefighterCount;
}
}
......@@ -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> extends Board<S> {
public interface BoardProperties<S> {
/**
* Retourne le nombre de lignes du tableau.
*
......@@ -24,19 +24,7 @@ public interface BoardProperties<S> extends Board<S> {
*/
int columnCount(); // Nombre de colonnes.
/**
* Retourne l'état des éléments situés à une position spécifique du tableau.
*
* @param position La position sur le tableau pour laquelle récupérer l'état.
* @return L'état des éléments à la position donnée.
*/
S getState(Position position); // État des éléments sur une position donnée.
/**
* Définit l'état des éléments à une position spécifique du tableau.
*
* @param state L'état à définir pour la position donnée.
* @param position La position sur le tableau pour laquelle définir l'état.
*/
void setState(S state, Position position);
}
......@@ -8,19 +8,15 @@ public class FirefighterBoard implements Board<List<ModelElement>> {
private final BoardFireFighterBehavior behavior;
private final BoardFirefighterProperties properties;
private final int initialFireCount;
private final int initialFirefighterCount;
private final Random randomGenerator = new Random();
public FirefighterBoard(int columnCount, int rowCount, int initialFireCount, int initialFirefighterCount) {
Map<Position, List<Position>> neighbors = initializeNeighbors(rowCount, columnCount);
this.properties = new BoardFirefighterProperties(rowCount, columnCount, neighbors);
this.properties = new BoardFirefighterProperties(rowCount, columnCount, neighbors,initialFireCount,initialFirefighterCount);
this.behavior = new BoardFireFighterBehavior(neighbors);
this.initialFireCount = initialFireCount;
this.initialFirefighterCount = initialFirefighterCount;
initializeElements();
}
......@@ -48,10 +44,10 @@ public class FirefighterBoard implements Board<List<ModelElement>> {
}
public void initializeElements() {
behavior.initializeElements(initialFireCount, initialFirefighterCount, properties.getRowCount(), properties.getColumnCount(), randomGenerator);
behavior.initializeElements(properties.getInitialFireCount(), properties.getInitialFirefighterCount(), properties.rowCount(), properties.columnCount(), randomGenerator);
}
@Override
public List<ModelElement> getState(Position position) {
List<ModelElement> result = new ArrayList<>();
if (behavior.getFirePositions().contains(position)) {
......@@ -63,23 +59,14 @@ public class FirefighterBoard implements Board<List<ModelElement>> {
return result;
}
@Override
public int rowCount() {
return properties.getRowCount();
}
@Override
public int columnCount() {
return properties.getColumnCount();
}
@Override
public void reset() {
behavior.reset(0);
initializeElements();
}
@Override
public void setState(List<ModelElement> state, Position position) {
if (state.contains(ModelElement.FIRE)) {
behavior.getFirePositions().add(position);
......@@ -89,16 +76,11 @@ public class FirefighterBoard implements Board<List<ModelElement>> {
}
}
@Override
public List<Position> updateToNextGeneration() {
List<Position> modifiedPositions = behavior.updateFirefighters();
modifiedPositions.addAll(behavior.updateFires());
behavior.incrementStep();
return modifiedPositions;
public BoardFireFighterBehavior getBehavior() {
return behavior;
}
@Override
public int stepNumber() {
return behavior.getStep();
public BoardFirefighterProperties getProperties() {
return properties;
}
}
......@@ -23,7 +23,7 @@ public abstract class FirefighterManager implements FirefighterBehavior, Firefig
@Override
public Position moveToTarget(Position currentPosition, Collection<Position> firePositions, Map<Position, List<Position>> neighbors) {
TargetStrategy targetStrategy = new TargetStrategy();
return targetStrategy.neighborClosestToFire(currentPosition, firePositions, neighbors);
return targetStrategy.neighborClosestToTarget(currentPosition, firePositions, neighbors);
}
@Override
......
......@@ -7,5 +7,5 @@ import java.util.List;
import java.util.Map;
public interface Strategy {
Position neighborClosestToTarget(Position position, Collection<Position> targets, Map<Position, List<Position>> neighbors, ViewElement[][] grid);
Position neighborClosestToTarget(Position position, Collection<Position> targets, Map<Position, List<Position>> neighbors);
}
......@@ -14,7 +14,7 @@ public class TargetStrategy implements Strategy{
* @return the position next to the current position that is on the path to the closest target.
*/
public Position neighborClosestToTarget(Position position, Collection<Position> targets,
Map<Position, List<Position>> neighbors, ViewElement[][] grid) {
Map<Position, List<Position>> neighbors) {
Set<Position> seen = new HashSet<Position>();
HashMap<Position, Position> firstMove = new HashMap<Position, Position>();
Queue<Position> toVisit = new LinkedList<Position>(neighbors.get(position));
......@@ -22,7 +22,7 @@ public class TargetStrategy implements Strategy{
firstMove.put(initialMove, initialMove);
while (!toVisit.isEmpty()) {
Position current = toVisit.poll();
if (grid[current.row()][current.column()] == ViewElement.MOUNTAIN) continue;
// if (grid[current.row()][current.column()] == ViewElement.MOUNTAIN) continue;
if (targets.contains(current))
return firstMove.get(current);
for (Position adjacent : neighbors.get(current)) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment