Skip to content
Snippets Groups Projects
Commit ad48dbce authored by YOUSSOUF Ali moussa's avatar YOUSSOUF Ali moussa
Browse files

Modification du code en la factorisant pour qu'il respecte le principe SOLID

parent 778b8ee5
Branches
No related tags found
No related merge requests found
Pipeline #24949 failed
...@@ -12,9 +12,7 @@ import javafx.scene.control.ToggleButton; ...@@ -12,9 +12,7 @@ import javafx.scene.control.ToggleButton;
import javafx.scene.control.ToggleGroup; import javafx.scene.control.ToggleGroup;
import javafx.util.Duration; import javafx.util.Duration;
import javafx.util.Pair; import javafx.util.Pair;
import model.Board; import model.*;
import model.ModelElement;
import model.FirefighterBoard;
import util.Position; import util.Position;
import view.*; import view.*;
...@@ -80,13 +78,17 @@ public class Controller { ...@@ -80,13 +78,17 @@ public class Controller {
updateGenerationLabel(board.stepNumber()); updateGenerationLabel(board.stepNumber());
} }
private ViewElement getViewElement(List<ModelElement> squareState) { private ViewElement getViewElement(List<ModelElement> squareState) {
if(squareState.contains(ModelElement.FIREFIGHTER)){ for (ModelElement model: squareState){
if(model instanceof FireFighters){
return new FIREFIGHTER(); return new FIREFIGHTER();
} }
if (squareState.contains(ModelElement.FIRE)){ if (model instanceof Fires){
return new FIRE(); return new FIRE();
} }
}
return new EMPTY(); return new EMPTY();
} }
......
package model;
public class FireFighter {
}
package model;
import util.Position;
import java.util.*;
public class FireFighters implements ModelElement {
private List<Position> fireFightersPositions;
private int rowCount;
private int columnCount;
private final Random randomGenerator = new Random();
public FireFighters() {
}
public void add(Position position){
fireFightersPositions.add(position);
}
public List<Position> getFireFightersPositions() {
return fireFightersPositions;
}
/* public void initializeElements(int initialCount, int rowCount, int columnCount) {
this.rowCount=rowCount;
this.columnCount=columnCount;
initializeElements(initialCount);
}*/
public void initializeElements(int initialFirefighterCount, int rowCount, int columnCount) {
this.rowCount=rowCount;
this.columnCount=columnCount;
fireFightersPositions = new ArrayList<>();
for (int index = 0; index < initialFirefighterCount; index++)
fireFightersPositions.add(randomPosition());
}
@Override
public Position randomPosition() {
if (rowCount <= 0 || columnCount <= 0) {
throw new IllegalArgumentException("Les limites doivent être positives");
}
return new Position(randomGenerator.nextInt(rowCount), randomGenerator.nextInt(columnCount));
}
private List<Position> neighbors(Position position) {
List<Position> list = new ArrayList<>();
if (position.row() > 0) list.add(new Position(position.row() - 1, position.column()));
if (position.column() > 0) list.add(new Position(position.row(), position.column() - 1));
if (position.row() < rowCount - 1) list.add(new Position(position.row() + 1, position.column()));
if (position.column() < columnCount - 1) list.add(new Position(position.row(), position.column() + 1));
return list;
}
public List<Position> updateFirefighters(Set<Position> firePositions) {
List<Position> result = new ArrayList<>();
List<Position> firefighterNewPositions = new ArrayList<>();
for (Position firefighterPosition : fireFightersPositions) {
Position newFirefighterPosition = neighborClosestToFire(firefighterPosition,firePositions);
firefighterNewPositions.add(newFirefighterPosition);
extinguish(newFirefighterPosition, firePositions);
result.add(firefighterPosition);
result.add(newFirefighterPosition);
List<Position> neighborFirePositions = neighbors(newFirefighterPosition).stream()
.filter(firePositions::contains).toList();
for(Position firePosition : neighborFirePositions)
extinguish(firePosition,firePositions);
result.addAll(neighborFirePositions);
}
fireFightersPositions = firefighterNewPositions;
return result;
}
private void extinguish(Position position,Set<Position> firePositions ) {
firePositions.remove(position);
}
private Position neighborClosestToFire(Position position, Set<Position> firePositions) {
Set<Position> seen = new HashSet<>();
HashMap<Position, Position> firstMove = new HashMap<>();
Queue<Position> toVisit = new LinkedList<>(neighbors(position));
for (Position initialMove : toVisit)
firstMove.put(initialMove, initialMove);
while (!toVisit.isEmpty()) {
Position current = toVisit.poll();
if (firePositions.contains(current))
return firstMove.get(current);
for (Position adjacent : neighbors(current)) {
if (seen.contains(adjacent)) continue;
toVisit.add(adjacent);
seen.add(adjacent);
firstMove.put(adjacent, firstMove.get(current));
}
}
return position;
}
}
...@@ -10,10 +10,11 @@ public class FirefighterBoard implements Board<List<ModelElement>> { ...@@ -10,10 +10,11 @@ public class FirefighterBoard implements Board<List<ModelElement>> {
private final int rowCount; private final int rowCount;
private final int initialFireCount; private final int initialFireCount;
private final int initialFirefighterCount; private final int initialFirefighterCount;
private List<Position> firefighterPositions;
private Set<Position> firePositions; private Fires fires;
private FireFighters fireFighters;
private int step = 0; private int step = 0;
private final Random randomGenerator = new Random();
public FirefighterBoard(int columnCount, int rowCount, int initialFireCount, int initialFirefighterCount) { public FirefighterBoard(int columnCount, int rowCount, int initialFireCount, int initialFirefighterCount) {
this.columnCount = columnCount; this.columnCount = columnCount;
...@@ -24,26 +25,21 @@ public class FirefighterBoard implements Board<List<ModelElement>> { ...@@ -24,26 +25,21 @@ public class FirefighterBoard implements Board<List<ModelElement>> {
} }
public void initializeElements() { public void initializeElements() {
firefighterPositions = new ArrayList<>(); this.fires = new Fires();
firePositions = new HashSet<>(); this.fireFighters = new FireFighters();
for (int index = 0; index < initialFireCount; index++) fires.initializeElements(initialFireCount,rowCount,columnCount);
firePositions.add(randomPosition()); fireFighters.initializeElements(initialFirefighterCount,rowCount,columnCount);
for (int index = 0; index < initialFirefighterCount; index++)
firefighterPositions.add(randomPosition());
}
private Position randomPosition() {
return new Position(randomGenerator.nextInt(rowCount), randomGenerator.nextInt(columnCount));
} }
@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 : fireFighters.getFireFightersPositions())
if (firefighterPosition.equals(position)) if (firefighterPosition.equals(position))
result.add(ModelElement.FIREFIGHTER); result.add(new FireFighters());
if(firePositions.contains(position)) if(fires.getFirePositions().contains(position))
result.add(ModelElement.FIRE); result.add(new Fires());
return result; return result;
} }
...@@ -58,24 +54,11 @@ public class FirefighterBoard implements Board<List<ModelElement>> { ...@@ -58,24 +54,11 @@ public class FirefighterBoard implements Board<List<ModelElement>> {
} }
public List<Position> updateToNextGeneration() { public List<Position> updateToNextGeneration() {
List<Position> modifiedPositions = updateFirefighters(); List<Position> result = fireFighters.updateFirefighters(fires.getFirePositions());
modifiedPositions.addAll(updateFires()); result.addAll(fires.updateFires());
step++; //step ++;
return modifiedPositions; fires.incrementStep();
} return result;
private List<Position> updateFires() {
List<Position> modifiedPositions = new ArrayList<>();
if (step % 2 == 0) {
List<Position> newFirePositions = new ArrayList<>();
for (Position fire : firePositions) {
newFirePositions.addAll(neighbors(fire));
}
firePositions.addAll(newFirePositions);
modifiedPositions.addAll(newFirePositions);
}
return modifiedPositions;
} }
@Override @Override
...@@ -83,24 +66,6 @@ public class FirefighterBoard implements Board<List<ModelElement>> { ...@@ -83,24 +66,6 @@ public class FirefighterBoard implements Board<List<ModelElement>> {
return step; return step;
} }
private List<Position> updateFirefighters() {
List<Position> modifiedPosition = new ArrayList<>();
List<Position> firefighterNewPositions = new ArrayList<>();
for (Position firefighterPosition : firefighterPositions) {
Position newFirefighterPosition = neighborClosestToFire(firefighterPosition);
firefighterNewPositions.add(newFirefighterPosition);
extinguish(newFirefighterPosition);
modifiedPosition.add(firefighterPosition);
modifiedPosition.add(newFirefighterPosition);
List<Position> neighborFirePositions = neighbors(newFirefighterPosition).stream()
.filter(firePositions::contains).toList();
for(Position firePosition : neighborFirePositions)
extinguish(firePosition);
modifiedPosition.addAll(neighborFirePositions);
}
firefighterPositions = firefighterNewPositions;
return modifiedPosition;
}
@Override @Override
public void reset() { public void reset() {
...@@ -108,50 +73,21 @@ public class FirefighterBoard implements Board<List<ModelElement>> { ...@@ -108,50 +73,21 @@ public class FirefighterBoard implements Board<List<ModelElement>> {
initializeElements(); initializeElements();
} }
private void extinguish(Position position) {
firePositions.remove(position);
}
private List<Position> neighbors(Position position) {
List<Position> list = new ArrayList<>();
if (position.row() > 0) list.add(new Position(position.row() - 1, position.column()));
if (position.column() > 0) list.add(new Position(position.row(), position.column() - 1));
if (position.row() < rowCount - 1) list.add(new Position(position.row() + 1, position.column()));
if (position.column() < columnCount - 1) list.add(new Position(position.row(), position.column() + 1));
return list;
}
private Position neighborClosestToFire(Position position) {
Set<Position> seen = new HashSet<>();
HashMap<Position, Position> firstMove = new HashMap<>();
Queue<Position> toVisit = new LinkedList<>(neighbors(position));
for (Position initialMove : toVisit)
firstMove.put(initialMove, initialMove);
while (!toVisit.isEmpty()) {
Position current = toVisit.poll();
if (firePositions.contains(current))
return firstMove.get(current);
for (Position adjacent : neighbors(current)) {
if (seen.contains(adjacent)) continue;
toVisit.add(adjacent);
seen.add(adjacent);
firstMove.put(adjacent, firstMove.get(current));
}
}
return position;
}
@Override @Override
public void setState(List<ModelElement> state, Position position) { public void setState(List<ModelElement> state, Position position) {
firePositions.remove(position); fires.extinguish(position);
for (;;) { for (;;) {
if (!firefighterPositions.remove(position)) break; if (!fireFighters.getFireFightersPositions().remove(position)) break;
} }
for(ModelElement element : state){ for(ModelElement element : state){
switch (element){ if (element instanceof Fires){
case FIRE -> firePositions.add(position); fires.add(position);
case FIREFIGHTER -> firefighterPositions.add(position);
} }
if (element instanceof FireFighters){
fireFighters.add(position);
} }
} }
}
} }
\ No newline at end of file
package model;
import util.Position;
import java.util.*;
public class Fires implements ModelElement {
private Set<Position> firePositions;
private int step;
private int rowCount;
private int columnCount;
private final Random randomGenerator = new Random();
public Fires() {
}
public void add(Position newPositions){
firePositions.add(newPositions);
}
public Set<Position> getFirePositions() {
return firePositions;
}
public List<Position> updateFires() {
List<Position> result = new ArrayList<>();
if (step % 2 == 0) {
List<Position> newFirePositions = new ArrayList<>();
for (Position fire : firePositions) {
newFirePositions.addAll(neighbors(fire, fire.row(), fire.column()));
}
firePositions.addAll(newFirePositions);
result.addAll(newFirePositions);
}
return result;
}
public void incrementStep(){
this.step ++;
}
public void initializeElements(int initialFireCount, int rowCount, int columnCount) {
//this.step=step;
this.columnCount = columnCount;
this.rowCount = rowCount;
firePositions = new HashSet<>();
for (int index = 0; index < initialFireCount; index++)
firePositions.add(randomPosition());
}
@Override
public Position randomPosition() {
if (rowCount <= 0 || columnCount <= 0) {
throw new IllegalArgumentException("Les limites doivent être positives");
}
return new Position(randomGenerator.nextInt(rowCount), randomGenerator.nextInt(columnCount));
}
public void extinguish(Position position) {
firePositions.remove(position);
}
private List<Position> neighbors(Position position, int rowCount, int columnCount) {
List<Position> list = new ArrayList<>();
if (position.row() > 0) list.add(new Position(position.row() - 1, position.column()));
if (position.column() > 0) list.add(new Position(position.row(), position.column() - 1));
if (position.row() < rowCount - 1) list.add(new Position(position.row() + 1, position.column()));
if (position.column() < columnCount - 1) list.add(new Position(position.row(), position.column() + 1));
return list;
}
}
package model; package model;
public class Fire { public class ICLOUD {
} }
package model; package model;
public enum ModelElement { import util.Position;
FIREFIGHTER, FIRE
import java.util.List;
public interface ModelElement {
Position randomPosition();
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment